Skip to content

Instantly share code, notes, and snippets.

@inoshiro
Created February 9, 2013 08:01
Show Gist options
  • Save inoshiro/4744543 to your computer and use it in GitHub Desktop.
Save inoshiro/4744543 to your computer and use it in GitHub Desktop.
git pushにhookを提供する。リポジトリの .git/hooks に pre-push か post-push が実行ファイルとして置いてあれば実行する
#!/usr/bin/env python
#coding: utf-8
import os
import subprocess
import sys
PRE_PUSH_SCRIPT = 'pre-push'
POST_PUSH_SCRIPT = 'post-push'
def _get_toplevel():
cmd = 'git rev-parse --show-toplevel'.split(' ')
proc = subprocess.Popen(cmd, stdout=-1, stderr=-1)
stdout, stderr = proc.communicate()
if stderr:
print stderr
sys.exit(1)
return stdout[:-1]
def _hook_exec(filepath):
if os.path.exists(filepath):
proc = subprocess.Popen(filepath)
proc.communicate()
return proc.returncode
return 0
def pre_process():
toplevel_dir = _get_toplevel()
script_path = os.path.join(str(toplevel_dir), '.git', 'hooks', PRE_PUSH_SCRIPT)
return _hook_exec(script_path)
def post_process():
toplevel_dir = _get_toplevel()
script_path = os.path.join(str(toplevel_dir), '.git', 'hooks', PRE_PUSH_SCRIPT)
return _hook_exec(script_path)
def git_push():
cmd = 'git push'.split(' ')
proc = subprocess.Popen(cmd)
proc.communicate()
return proc.returncode
def main():
if 0 != pre_process():
print 'pre-push script failed.'
sys.exit(1)
returncode = git_push()
if 0 != returncode:
sys.exit(returncode)
if 0 != post_process:
print 'post-push script failed.'
sys.exit(1)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment