Tom
Tom

Reputation: 16198

Git setting to allow force update of certain branches

On our git server we have a git config of

[receive]
    denyDeletes = true
    denynonfastforwards = true

This is a very sensible setting as it stops mistaken force updates on the important branches. However there are many branches that it would be useful to force update.

Rather than having this global setting is there an easy way to achieve this functionality.

I assume it possible through a git pre-recieve hook, however I am not sure how I would check whether the push is a forced non fast forward.

I'm aware there are complete solutions through Git hosting software such as gitolite, but I'm looking to achieve this on a simple ssh based server.

Upvotes: 2

Views: 85

Answers (1)

Wolf
Wolf

Reputation: 4462

You can do this with an update hook. It is called once for each reference being updated with three parameters: (1) the ref name, (2) the pre-update commit-id of that branch (or '0'*40 if you're creating the branch), and (3) the post-update commit-id of that branch (or '0'*40 if you're deleting that branch). If it's not a create or destroy, then you can run git merge-base old-rev new-rev. If the result is the same as the old-rev, the update is a fast-forward. Otherwise, the update is a force push of something not descended from the previous head.

In my update hook, I allow force updates for branches that start with the user's name, e.g., wolf can force push any branch in wolf/*, but only a whitelist of users can update those branches without a prefix, e.g., master.

Here's a link to an example update hook that does exactly that, as used in the Allura project at SourceForge: https://forge-allura.apache.org/p/allura/git/ci/master/tree/scripts/git-hooks/for-the-remote-repo/updateupdate

Upvotes: 2

Related Questions