-
Notifications
You must be signed in to change notification settings - Fork 0
/
post-receive
executable file
·59 lines (46 loc) · 1.66 KB
/
post-receive
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import commands, subprocess, os, sys, re
refpattern = "refs/heads/master"
def print_if_present(s):
o = s.strip()
if (len(o) > 0):
print(o)
def run_cmd(cmd):
pr = subprocess.Popen(cmd, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
return pr.communicate()
def run_git_cmd(cmd):
return run_cmd("git --work-tree=%s %s" % (get_worktree_directory(),cmd))
def git_config_value(key, default=None):
(out,error) = run_cmd("git config --get %s" % (key))
out = out.strip()
if len(out) == 0:
return default
if out.lower() == "true":
return True
if out.lower() == "false":
return False
return out
def get_worktree_directory():
if git_config_value("core.bare",False):
return git_config_value("deploy.destination")
else:
return os.path.abspath(os.path.join(os.getcwd(),'..'))
def run_deploy_script(working_directory,script_filename):
del os.environ['GIT_DIR']
pr = subprocess.Popen(os.path.join(working_directory,script_filename), cwd = working_directory, shell = True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
return pr.communicate()
def run_deploy(oldrev, newrev, refname):
script = git_config_value("deploy.script",None)
print("Resetting tree to pushed version (%s)." % (newrev))
(out, error) = run_git_cmd("reset --hard %s" % (newrev))
print_if_present(error)
print_if_present(out)
if script != None:
print("Running deployment script.")
(out, error) = run_deploy_script(get_worktree_directory(),script)
print_if_present(error)
print_if_present(out)
if __name__ == '__main__':
(oldrev, newrev, refname) = sys.stdin.readlines()[0].split()
if re.match(refpattern, refname):
run_deploy(oldrev,newrev,refname)