Skip to content

Instantly share code, notes, and snippets.

@mitsuse
Last active November 17, 2015 01:53
Show Gist options
  • Save mitsuse/39f37d4102218dc0ddab to your computer and use it in GitHub Desktop.
Save mitsuse/39f37d4102218dc0ddab to your computer and use it in GitHub Desktop.
Forbid to push remote branches specified with an environment variable "GIT_PROTECTED_BRANCHES".
#!/usr/bin/env python3
# coding: utf-8
def main():
import sys
local_ref, local_sha1, remote_ref, remote_sha1 = sys.stdin.readline().split()
remote_branch = extract_branch(remote_ref)
if remote_branch in list_protected_branches():
message = 'Cannot push to protected branch.'
print(message, end = '\n\n', file = sys.stderr)
sys.exit(1)
def extract_branch(remote_ref):
return remote_ref.split('refs/heads/', 1).pop()
def list_protected_branches():
import os
return os.environ.get('GIT_PROTECTED_BRANCHES', '').split()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment