-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_patch.py
executable file
·41 lines (33 loc) · 1.14 KB
/
split_patch.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
#!/usr/bin/env python
#
# Takes a patch generated by format-patch as
# the first argument, and generates one patch file
# per diff inside that patch, to be applied with
# patch-am. This helps you rebase on a file-by-file
# basis if you're so inclined.
import sys
import re
patch_file = file(sys.argv[1])
patch_lines = patch_file.readlines()
per_file_patches = []
per_file_filenames = []
diff_indexes = []
for i in xrange(len(patch_lines)):
if patch_lines[i].startswith("diff "):
diff_indexes.append(i)
assert len(diff_indexes) > 0
header = "".join(patch_lines[0:diff_indexes[0]])
for i in xrange(len(diff_indexes)):
if i == len(diff_indexes) - 1:
p = "".join(patch_lines[diff_indexes[i]:])
else:
p = "".join(patch_lines[diff_indexes[i]:diff_indexes[i+1]])
file_guess = re.split("[ /]", patch_lines[diff_indexes[i]].rstrip())[-1]
p = header + p
p = re.sub("Subject: \[PATCH\]", "Subject: [PATCH] " + file_guess, p)
per_file_patches.append(p)
per_file_filenames.append(file_guess)
for i in xrange(len(per_file_patches)):
f = file("%05d-%s.patch" % (i, per_file_filenames[i]), "w")
f.write(per_file_patches[i])
print f.name