bashgitgit-post-receive

Why is the output of git diff-tree empty in my post-receive hook?


I have a bare remote repository with source files in which I want to build only the changed files after it has been pushed to. I thought the best way to detect which files have been changed would be by putting the command changed_files=$(git diff-tree --no-commit-id --name-only -r HEAD) into a post-receive hook.

However, the variable ends up empty as I have verified by echoing it into a file. If I put HEAD^ instead of HEAD, it does show the changed files of the second to most recent commit. However, it doesn't show the most recent changes when I put HEAD but just shows nothing.

Can anyone help me? Or is there a smarter approach to my problem altogether?

I would definitely prefer a lean approach like automatically triggering a build with a push over one that would have to e. g. periodically check for changes.


Solution

  • OK, I've figured it out: I was getting a

    remote: fatal: ambiguous argument 'HEAD': both revision and filename

    error in the push command which I had not noticed. After changing

    changed_files=$(git diff-tree --no-commit-id --name-only -r HEAD)

    to

    changed_files=$(git diff-tree --no-commit-id --name-only -r HEAD --)

    everything is working fine. Apparently, this is caused by the hook being executed in the .git directory of the remote repository, and there is a file called HEAD in that directory, which makes referring to the HEAD revision as HEAD ambiguous.