In trying to write a pre-receive hook which rejects certain kinds of changes to certain kinds of files, I am stumped as to why the following is legal in that script:
git show <new_commit>:<file>
as is used in this example (after having teased out the affected files using diff with –name-only), but not
git diff <old_commit> <new_commit> -- <file>
I prefer the latter so that I only reject changes introduced rather than patterns already existing in the files, but it generates the error: “unknown revision or path not in the working tree” when executed from this script.
I suspect it has to do with the ordering of events, since both work at the command-line on my bare repo, but would like to understand the distinction.
As the answer you linked points out, when the
pre-receivehook is executed, no files have been received yet, only which refs should be updates (details on this and other hooks can be found on the githooks(5) manpage).Since there is no data with which your old data could be compared, no diff can be created.