This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.py
101 lines (77 loc) · 2.59 KB
/
utils.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
"""
utils module for gitzilla
"""
import os
import sys
import subprocess
sNoCommitRev = "0000000000000000000000000000000000000000"
def execute(asCommand, bSplitLines=False, bIgnoreErrors=False):
"""
Utility function to execute a command and return the output.
"""
p = subprocess.Popen(asCommand,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
shell=False,
close_fds=True,
universal_newlines=True,
env=None)
if bSplitLines:
data = p.stdout.readlines()
else:
data = p.stdout.read()
iRetCode = p.wait()
if iRetCode and not bIgnoreErrors:
print('Failed to execute command: %s\n%s' % (asCommand, data), file=sys.stderr)
sys.exit(-1)
return data
def get_changes(sOldRev, sNewRev, sFormatSpec, sSeparator, bIncludeDiffStat, sRefName, sRefPrefix):
"""
returns an array of chronological changes, between sOldRev and sNewRev,
according to the format spec sFormatSpec.
Gets changes which are only on the specified ref, excluding changes
also present on other refs starting with sRefPrefix.
"""
if sOldRev == sNoCommitRev:
sCommitRange = sNewRev
elif sNewRev == sNoCommitRev:
sCommitRange = sOldRev
else:
sCommitRange = "%s..%s" % (sOldRev, sNewRev)
sFormatSpec = sFormatSpec.strip("\n").replace("\n", "%n")
if bIncludeDiffStat:
sCommand = "whatchanged"
else:
sCommand = "log"
asCommand = ['git', sCommand,
"--format=format:%s%s" % (sSeparator, sFormatSpec)]
# exclude all changes which are also found on other refs
# and hence have already been processed.
if sRefName is not None:
asAllRefs = execute(
['git', 'for-each-ref', '--format=%(refname)', sRefPrefix],
bSplitLines=True)
asAllRefs = [x.strip() for x in asAllRefs]
asOtherRefs = [x for x in asAllRefs if x != sRefName]
asNotOtherRefs = execute(
['git', 'rev-parse', '--not'] + asOtherRefs,
bSplitLines=True)
asNotOtherRefs = [x.strip() for x in asNotOtherRefs]
asCommand += asNotOtherRefs
asCommand.append(sCommitRange)
sChangeLog = execute(asCommand)
asChangeLogs = sChangeLog.split(sSeparator)
asChangeLogs.reverse()
return asChangeLogs[:-1]
def notify_and_exit(sMsg):
"""
notifies the error and exits.
"""
print("""
======================================================================
Cannot accept commit.
%s
======================================================================
""" % (sMsg,))
sys.exit(1)