Created
June 25, 2019 11:57
-
-
Save 300481/f4b4fe1b38b54edf11bc34355a3fa385 to your computer and use it in GitHub Desktop.
pre-commit and post-checkout hook to save permissions (https://stackoverflow.com/questions/3207728/retaining-file-permissions-with-git)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
SELF_DIR=`git rev-parse --show-toplevel` | |
DATABASE=$SELF_DIR/.permissions | |
echo -n "Restoring file permissions..." | |
IFS_OLD=$IFS; IFS=$'\n' | |
while read -r LINE || [[ -n "$LINE" ]]; | |
do | |
FILE=`echo $LINE | cut -d ";" -f 1` | |
PERMISSIONS=`echo $LINE | cut -d ";" -f 2` | |
USER=`echo $LINE | cut -d ";" -f 3` | |
GROUP=`echo $LINE | cut -d ";" -f 4` | |
# Set the file permissions | |
chmod $PERMISSIONS $FILE | |
# Set the file owner and groups | |
chown $USER:$GROUP $FILE | |
done < $DATABASE | |
IFS=$IFS_OLD | |
echo "OK" | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# A hook script called by "git commit" with no arguments. The hook should | |
# exit with non-zero status after issuing an appropriate message if it wants | |
# to stop the commit. | |
SELF_DIR=`git rev-parse --show-toplevel` | |
DATABASE=$SELF_DIR/.permissions | |
# Clear the permissions database file | |
> $DATABASE | |
echo -n "Backing-up file permissions..." | |
IFS_OLD=$IFS; IFS=$'\n' | |
for FILE in `git ls-files` | |
do | |
# Save the permissions of all the files in the index | |
echo $FILE";"`stat -c "%a;%U;%G" $FILE` >> $DATABASE | |
done | |
IFS=$IFS_OLD | |
# Add the permissions database file to the index | |
git add $DATABASE | |
echo "OK" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment