3

I have ready many articles about protecting remote branches...

I however would like to capture in a git hook the following LOCAL repo command:

git branch -d abranchthatshouldnotbedeleted

I would like to hook that command and analyze it against an branch list file of "protected branches" that I made and do a simple check to allow or deny the delete.

Certain branches of mine are now locked in a stated where they are managed now and must be protected.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1832576
  • 51
  • 1
  • 2
  • What do you mean by "Certain branches of mine are now locked in a stated where they are managed now and must be protected."? Are you still able to make commits to the branches? – adamdunson Jan 11 '13 at 05:08

2 Answers2

1

Since GitHub doesn't allow pre-receive hook (only post-receive ones), I would recommend pushing to an intermediate local repo, protected by Gitolite (an authorization layer, through ssh or http access to your git repo).

Gitolite can help code all kind of access rules, including protecting a branch against deletion.

If the push is allowed, then a post-commit hook can push that automatically to GitHub.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
1

Git does not (currently) have a hook that you can use to do what you want. See git help hooks for a list of available hooks.

You may want to consider a different approach. For example, you could wrap git in a wrapper script or shell function that does its own parsing to prevent you from deleting the branch:

git() {
    [ "${1}" != branch ] ||
    { [ "$2" != -d ] && [ "$2" != -D ]; } ||
    case $3 in
        abranchthatshouldnotbedeleted) false;;
        *) true;;
    esac ||
    { printf %s\\n "ERROR: branch $3 must not be deleted" >&2; exit 1; }
    command git "$@"
}

The above shell function is quite primitive and doesn't handle invocations like git --git-dir=/foo.git branch -d abranchthatshouldnotbedeleted, but you get the point. Perhaps you can use git rev-parse --parseopt to make it more robust.

Richard Hansen
  • 51,690
  • 20
  • 90
  • 97