forked from cmccabe/cmccabe-hbin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patchr.py
executable file
·69 lines (65 loc) · 1.96 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
#!/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
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 == "branch-1"):
branch_suffix = "-b1"
elif (branch == "branch-2"):
branch_suffix = "-b2"
else:
raise RuntimeError("can't understand branch %s" % branch)
overwrite = False
if (len(sys.argv) >= 4):
if (sys.argv[3] == "-f"):
overwrite = True
else:
raise RuntimeError("can't understand option %s" % sys.argv[3])
gpat = listdir + "/" + jira + "_*/"
print "gpat = " + gpat
paths = glob.glob(gpat)
if (len(paths) == 0):
raise RuntimeError("No directory for " + jira)
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 = subprocess.check_output([ "git", "merge-base", "HEAD", branch]).rstrip()
cmd = [ "git", "diff", "--binary", "--no-prefix", commit, "HEAD" ]
outfile = "%s/%s%s.%03d.patch" % (dir_path, jira, branch_suffix, next_patch_num)
print " ".join(cmd) + " > " + outfile
patch = subprocess.check_output(cmd)
f = open(outfile, "w")
try:
f.write(patch)
finally:
f.close()
os.system("less '" + outfile + "'")