-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhookkit.py
executable file
·123 lines (87 loc) · 3.43 KB
/
hookkit.py
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
import os
import sys
# Needed to pick up the right libhookkit when soft-linked
sys.path.insert(0, os.path.dirname(__file__))
import libhookkit
from libhookkit import LibHookKit
scripts_to_run_each_commit = []
scripts_to_run_on_last_commit = []
def exit_with_error_message(message):
print message
exit(1)
def get_active_hook():
return os.path.basename(__file__)
def load_scripts():
active_hook = get_active_hook()
config = libhookkit.LibHookKitConfiguration()
scripts_to_execute = config.load_entries_for_hook(active_hook)
if len(scripts_to_execute) == 0:
print "Unable to load any scripts, aborting."
return False
for script in scripts_to_execute:
if not script.setup():
print "Failed to setup hook-script: " + script.file_name
return False
if script.frequency == libhookkit.HookScriptFrequency.EACH_COMMIT:
scripts_to_run_each_commit.append(script)
else:
scripts_to_run_on_last_commit.append(script)
return True
def trigger_scripts(old_sha1, new_sha1, ref):
# We only care about branches
# TBD: Add hook support for things other than branches
if not ref.startswith('refs/heads'):
return
# Delete branch
if new_sha1 == '0000000000000000000000000000000000000000':
return
# New branch
if old_sha1 == '0000000000000000000000000000000000000000':
# FIXME: This feels really wrong, but I'm not sure what I should do instead.
# I'll do this for now, until I can think of something better.
# (or it starts causing problems)
old_sha1 = LibHookKit.run_git_command(['merge-base', new_sha1,
'master'])
old_sha1 = old_sha1.rstrip()
failed = False
script_names = []
for script in scripts_to_run_each_commit:
script_names.append(script.label)
for script in scripts_to_run_on_last_commit:
script_names.append(script.label)
script_names = ', '.join(script_names)
print '* Checks: ' + script_names + '\n'
sys.stdout.flush()
for sha1 in LibHookKit.get_sha1_list_between_commits(old_sha1, new_sha1):
for script in scripts_to_run_each_commit:
if not script.run(sha1):
print '\t!!! Fail: ' + sha1 + ' - ' + script.error_message
sys.stdout.flush()
failed = True
#should pass in a dict of args
#FIXME: This really needs tests!
for script in scripts_to_run_on_last_commit:
if not script.run(old_sha1, new_sha1, ref):
print '!!! Fail: ' + script.error_message
sys.stdout.flush()
failed = True
if failed:
exit_with_error_message('\n!!! PUSH ABORTED !!!\n')
def main():
print '\n* Hook: ' + get_active_hook()
if not load_scripts():
exit_with_error_message('\n!!! ABORTED !!!\n')
active_hook = get_active_hook()
if active_hook == 'update':
trigger_scripts(sys.argv[2], sys.argv[3], sys.argv[1])
if active_hook == 'commit-msg' or active_hook == 'post-commit':
sha = libhookkit.LibHookKit.get_latest_sha()
branch = libhookkit.LibHookKit.get_current_branch()
trigger_scripts(sha, sha, "refs/heads/%s" % branch)
else:
for line in sys.stdin.xreadlines():
arg_array = line.strip().split(' ')
trigger_scripts(arg_array[0], arg_array[1], arg_array[2])
if __name__ == "__main__":
main()