Skip to content

Instantly share code, notes, and snippets.

@YaroslavShapoval
Created September 22, 2015 08:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save YaroslavShapoval/cbab1fe98adb14ddc2cf to your computer and use it in GitHub Desktop.
Save YaroslavShapoval/cbab1fe98adb14ddc2cf to your computer and use it in GitHub Desktop.
Post-update git hook to set right owner and permissions for changed files
#!/bin/sh
# default owner user
OWNER="www-data:www-data"
# changed file permission
PERMISSION="664"
# web repository directory
REPO_DIR="/var/www/sitename"
# remote repository
REMOTE_REPO="origin"
# public branch of the remote repository
REMOTE_REPO_BRANCH="master"
cd $REPO_DIR || exit
unset GIT_DIR
files="$(git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD)"
git merge FETCH_HEAD
for file in $files
do
sudo chown $OWNER $file
sudo chmod $PERMISSION $file
done
exec git-update-server-info
@fishtankmike
Copy link

fishtankmike commented Dec 14, 2018

can you run me through what this is doing?

files="$(git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD)"

specifically the --no-commit-id HEAD@{1} HEAD

@xiebruce
Copy link

xiebruce commented Feb 14, 2022

@fishtankmike

git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD

The format of the command above is

git diff-tree <commit_ID_start> <commit_ID_end>

First of all, let's take a look at diff-tree, what does it use for? Actually diff-tree shows the differences from <commit_ID_start> to <commit_ID_end> from reflog, in this case, HEAD@{1} is the <commit_ID_start> and HEAD is the <commit_ID_end>.

Now we can change the command as below

git diff-tree HEAD@{1} HEAD

Now, run git reflog, you should know what HEAD@{1} means, actually it stands for the last reflog, while HEAD@{0} stands for the current reflog(which is HEAD)
image

So, git diff-tree HEAD@{1} HEAD is use to show the difference from the last commit HEAD@{1} and the current HEAD@{0}(HEAD) from the reflog.

As to --name-only and --no-commit-id, they are the options of diff-tree, see the command and it's output below

 > git diff-tree HEAD@{1} HEAD
:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	README.md
:000000 100644 0000000000000000000000000000000000000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 A	aaa.html
:100644 000000 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 D	test.txt

 > git diff-tree --name-only HEAD@{1} HEAD
README.md
aaa.html
test.txt

well, if --name-only can get the name, what does --no-commit-id use for? see the screenshot below(screenshot from git diff-tree --help)
image

As to -r, I think you already know, it refers to recursively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment