I have a Git pre-receive hook that performs several validations such as commit message and file size.
Now I want to also include a validation that blocks the push if there is a merge commit that touches a specific file. The reason is that we have a file that is supposed to be changed only by an automatic procedure related with release versioning, and it's very easy that people merges that file when they need to solve merge conflicts locally.
So, given the current pre-receive hook:
filename=<path-to-file>
while read OLD_SHA1 NEW_SHA1 REFNAME; do
...
I need to:
- Retrieve the list of commits being pushed
- For each commit, determine if the current commit refers to a merge commit
- If it's a merge commit, retrieve the changed files within that commit and check it includes the restricted file ($filename)
Optionally, it could also check the author if not a merge commit because it's expected that it matches a specific one (from the automated procedure). This would also protect from cases where the developer has done some wrong rebase or cherry-pick.
Can someone help on this? It would be helpful to have a complete solution (for the 3 steps) since it might be interesting for other people, I assume.