-
Notifications
You must be signed in to change notification settings - Fork 4
/
patchr.py
executable file
·96 lines (90 loc) · 2.7 KB
/
patchr.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
#!/usr/bin/python
#
# Hadoop JIRAs generally require a patch file to be attached.
# This script creates such a patch file from a git repo.
#
import glob
import os
import re
import subprocess
import sys
from subprocess import PIPE,Popen
def run_check_output(what):
proc = Popen(what, stdout=PIPE)
return (proc.communicate()[0].strip())
listdir = os.path.join(os.environ["HOME"], "list")
jira = sys.argv[1]
branch = sys.argv[2]
branch_suffix = None
if (branch == "trunk"):
branch_suffix = ""
elif (branch == "master"):
branch_suffix = ""
elif (branch == "branch-1"):
branch_suffix = "-b1"
elif (branch == "branch-2"):
branch_suffix = "-b2"
elif (branch == "branch-2.1-beta"):
branch_suffix = "-b2.1"
elif (branch == "HDFS-4949"):
branch_suffix = "-caching"
elif (branch == "HADOOP-10388"):
branch_suffix = "-pnative"
elif (branch == "HDFS-6994"):
branch_suffix = "-pnative"
elif (branch == "fs-encryption"):
branch_suffix = "-fs-enc"
else:
raise RuntimeError("can't understand branch %s" % branch)
overwrite = False
mkdirs = False
if (len(sys.argv) >= 4):
remaining = sys.argv[3:]
while len(remaining) > 0:
if remaining[0] == "-f":
overwrite = True
elif remaining[0] == "-p":
mkdirs = True
else:
raise RuntimeError("can't understand option %s" % remaining[0])
remaining = remaining[1:]
gpat = listdir + "/" + jira + "_*/"
print "gpat = " + gpat
paths = glob.glob(gpat)
if (len(paths) == 0):
if not mkdirs:
raise RuntimeError("No directory for " + jira)
paths.append(listdir + "/" + jira + "_jira");
os.makedirs(paths[0]);
for p in paths[1:]:
if os.path.isdir(p):
raise RuntimeError("more than one directory for " + jira)
dir_path = paths[0]
patch_name_re = re.compile(jira + branch_suffix + \
"\.(?P<patch_num>[0123456789][0123456789][0123456789])\.patch")
highest_patch_num = 0
for p in os.listdir(dir_path):
match = patch_name_re.match(p)
if (not match):
continue
patch_num = int(match.group('patch_num'))
if (patch_num > highest_patch_num):
highest_patch_num = patch_num
if (overwrite):
if (highest_patch_num == 0):
raise RuntimeError("there is no existing patch to overwrite")
else:
next_patch_num = highest_patch_num
else:
next_patch_num = highest_patch_num + 1
commit = run_check_output([ "git", "merge-base", "HEAD", branch])
cmd = [ "git", "diff", "--binary", commit, "HEAD" ]
outfile = "%s/%s%s.%03d.patch" % (dir_path, jira, branch_suffix, next_patch_num)
print " ".join(cmd) + " > " + outfile
patch = run_check_output(cmd)
f = open(outfile, "w")
try:
f.write(patch + "\n")
finally:
f.close()
os.system("less '" + outfile + "'")