I have a problem with the 'update' hook. In the case of a new branch, it gets a 0000000000000000000000000000000000000000 as the 'oldrev'. And I don't know how to handle that case.
We have the requirement, that every commit message references a valid Jira issue. So I have installed an "update" hook on our central repository. That hook gets an "oldrev" and a "newrev". I then pass those to "git rev-list" like this:
git rev-list $oldrev..$newrev
This gives me the list of all revs, which I can then iterate through, and do whatever I need to do.
The problem is, when the user pushes a new branch, the hook gets 0000000000000000000000000000000000000000 as the oldrev. And "git rev-list" simply complains with:
fatal: Invalid revision range 0000000000000000000000000000000000000000..21bac83b2
So how do I get the list of all the revs that are on that new branch? I have searched the net for quite some time now, and found nothing. The example hooks I found either
None of these sound particularly exciting.
So does someone have any idea how to get the right answer in that case? I was thinking about querying git for "give me all revs that are reachable from newrev, but not from any of the other branches (=all branches except the new one)". But even that would give the wrong answer if there had been a merge from the new branch to any of the old ones.
No. Hooks are per-repository and are never pushed.
Git hooks are scripts that run automatically every time a particular event occurs in a Git repository. They let you customize Git's internal behavior and trigger customizable actions at key points in the development life cycle.
In order to share the custom hooks that you created, with your team; you can create a separate folder inside your project and add all the hooks there first. In my case, I created a folder called . githooks in my project root directory.
No, there isn't any clone hook.
The term "right answer" is a bit ambiguous in this case. I actually think that "all revs reachable from newrev but nowhere else" is completely correct. This is true even if there was a merge - in that case, you should see the commits unique to the new ref, and the merge commit, but not the commits that were merged.
So, I would say, check if the "oldrev" is all zeroes, and if it is, act accordingly:
if [ "$oldrev" -eq 0 ]; then # list everything reachable from newrev but not any heads git rev-list $(git for-each-ref --format='%(refname)' refs/heads/* | sed 's/^/\^/') "$newrev" else git rev-list "$oldrev..$newrev" fi
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