In a pre-commit hook, I want to make sure that nobody (except 1 person) is able to change restricted_file.txt
The hook needs to handle the case when someone performs 'git pull' or 'git merge some_branch', obtains the latest restricted_file.txt but something_else.txt causes a conflict.
He resolves the conflict and keep "theirs" version of restricted_file.txt, perform 'git add' and 'git commit'.
At this point, pre-commit hook is executed and it identifies that restricted_file.txt will be committed. We want to allow the commit only if the file is identical to what it came from 'git pull' or 'git merge' (in other words, if he kept "theirs" version).
Does anybody know if it is possible to determine if a file is identical to "theirs" during a conflict resolution?
Something equivalent to the following non-existent command:
git diff --cached --against_theirs -- restricted_file.txt
git diff --cached MERGE_HEAD -- restricted_file.txt will do the job. This works because (and hence, also, if and only if) MERGE_HEAD contains the SHA-1 ID of the commit being merged-in, i.e., the "theirs" version.
(Upon stopping for a conflicted or --no-commit merge, git merge writes that SHA-1 to the special MERGE_HEAD reference.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With