Skip to content

Instantly share code, notes, and snippets.

@westhood
Created November 26, 2013 16:06
Show Gist options
  • Select an option

  • Save westhood/7661024 to your computer and use it in GitHub Desktop.

Select an option

Save westhood/7661024 to your computer and use it in GitHub Desktop.
git hook script in update phrase to ensure only specific users can commit to given branches
#!/usr/bin/env python
import sys
import re
import pwd
import os
ptn = re.compile("refs/heads/(.*)")
allows_only = {
"master": ["yuanye4"]
}
def check():
uname = pwd.getpwuid(os.getuid())[0]
refname, old, new = sys.argv[1:]
m = ptn.match(refname)
if m:
branch = m.groups()[0]
if branch in allows_only:
users = allows_only[branch]
if uname not in users:
print ":(, sorry %s!" % uname
print "branch *%s* allows only users below to update:" % branch
print ", ".join(users)
return 1
return 0
sys.exit(check())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment