-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauto-commit.py
executable file
·110 lines (92 loc) · 4.3 KB
/
auto-commit.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
#!/usr/bin/python
#
# Automatically commit and upload to chromium code reviews.
# Need to be run under the root of a v8 repo, and generateCommitMessage.py
from __future__ import print_function
import subprocess, sys, os
import re
def commitMessage(commitHash):
originalHash = subprocess.check_output(["git", "log", str(commitHash), "--pretty=format:%H", '-1'] ).strip()
commitTitle = subprocess.check_output(["git", "log", str(commitHash), "--pretty=format:%s", '-1'] ).strip()
originalCommiterEmail= subprocess.check_output(["git", "log", str(commitHash), "--pretty=format:%ae", '-1'] ).strip()
portUploaderEmail= subprocess.check_output(["git", "config", "user.email"]).strip()
originalCommitMessage = subprocess.check_output(["git", "log", str(commitHash), "--pretty=format:\%B", "-1"]).split("\n")
# Parse the originalCommitMessage Tags
originalCommitMessage = filter(lambda text : (len(text) == 0 or not (text[0] == '#' or text.isspace())), originalCommitMessage)
if portUploaderEmail in reviewerList:
reviewerList.remove(portUploaderEmail)
commitInfo = {}
for line in originalCommitMessage:
if re.match(r'[A-Za-z_-]*=',line):
i = line.find('=') + 1
commitInfo[line[:i]] = line[i:]
if re.match(r'[A-Za-z_-]*: ',line):
i = line.find(': ') + 2
commitInfo[line[:i]] = line[i:]
textFilter = lambda text : not (text.find(commitTitle) != -1 or re.match(r'^[A-Za-z_-]*(=|: )',text))
commitMessageBody = filter(textFilter, originalCommitMessage)
if len(commitMessageBody) != 0:
while len(commitMessageBody) != 0 and commitMessageBody[0] == '':
commitMessageBody.pop(0)
while len(commitMessageBody) != 0 and commitMessageBody[-1] == '':
commitMessageBody.pop(-1)
message = "PPC/s390: " + commitTitle + '\n'
message += "\nPort "+originalHash+"\n"
if len([line for line in commitMessageBody if not(line.isspace() or len(line) == 0)]) != 0:
message += '\nOriginal Commit Message:\n\n'
for line in commitMessageBody:
message += " " + line + "\n"
message += "\n"
message += "R="+originalCommiterEmail+", "+(", ").join(reviewerList)+"\n"
if "BUG=" in commitInfo.keys():
message += "BUG="+commitInfo["BUG="]+"\n"
else:
message += "BUG=\n"
message += "LOG=N"
return message
def printError(exitCode, *args, **kwargs):
print(__file__, end = ':')
print(*args, file = sys.stderr, **kwargs)
if exitCode:
print("(last executed command exit code = " + str(exitCode) + ")")
def runCommand(command, stdout = None, stderr = None,\
errorMessage = None, workingDirectory = os.getcwd()):
if not errorMessage:
errorMessage = " ".join(command) + " failed."
try:
print(" ".join(command))
if not DRY_RUN:
subprocess.check_call(command,stdout = stdout, stderr = stderr, cwd = workingDirectory)
except subprocess.CalledProcessError as e:
printError(e.returncode,errorMessage)
exit(1)
def runLintTest():
runCommand(["python", "tools/presubmit.py"], errorMessage = "Lint check failed.")
def commitAndUpload(gitAddArgList, portHash):
runCommand(["git","add"] + gitAddArgList)
cm = commitMessage(portHash)
print("Commit message is:\n", cm)
print("git","commit","--file","-")
if not DRY_RUN:
p = subprocess.Popen(["git","commit", "--file", "-"], stdin=subprocess.PIPE)
p.communicate(input = cm)
runCommand(["git","cl","upload"])
def Main():
import argparse
parser = argparse.ArgumentParser(description = "A script for automatically performing cl upload.")
parser.add_argument('portHash', type = str, help = "The commit hash to be ported")
parser.add_argument('git_add_args', nargs = '+', help = \
"Arguments to be appended to command git add. if the argument start with a dash ('-'), the arguments must start with --.")
parser.add_argument('-D','--dryrun', action = 'store_true', help = "Do not run the commands, just output them")
parser.add_argument('-C','--commit_message_only', action = 'store_true', help = "Just print the commit message")
args = parser.parse_args()
global DRY_RUN
DRY_RUN = args.dryrun
if args.commit_message_only:
print(commitMessage(args.portHash))
exit(0)
runLintTest()
commitAndUpload(args.git_add_args,args.portHash)
if __name__=="__main__":
Main()