forked from mozilla/moz-git-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-patch-to-hg-patch
executable file
·64 lines (47 loc) · 1.35 KB
/
git-patch-to-hg-patch
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
#!/usr/bin/python
"""Git format-patch to hg importable patch.
"""
# Original author: bholley
import sys
import re
import fileinput
def process(lines):
# Discard the first line.
lines.pop(0);
# Read the second line, and extract the author.
author = re.sub(r'^From: (.*)$', r'# User \1', lines.pop(0))
# Discard the third line.
lines.pop(0)
# Read the subject line, filtering out the gunk and unwrapping.
subject = re.sub(r'^Subject: (.*)', r'\1', lines.pop(0))
nextline = lines.pop(0)
while nextline[0] == ' ':
subject = subject.rstrip('\n') + nextline
nextline = lines.pop(0)
# Filter a version (ie, v3, v12) out of the subject.
subject = re.sub(r'\s?v\d\d?', '', subject)
# Write the header.
lines.insert(0, subject)
lines.insert(0, '\n')
lines.insert(0, author)
lines.insert(0, '# HG changeset patch\n')
if __name__ == "__main__":
# If there were no arguments, do stdin->stdout.
filelist = sys.argv[1:]
if not filelist:
lines = sys.stdin.readlines()
process(lines)
sys.stdout.writelines(lines)
sys.exit(0)
# Otherwise, we take a list of files.
for filename in filelist:
# Read the lines.
f = open(filename, 'r')
lines = f.readlines()
f.close()
# Process.
process(lines)
# Write them back to the same file.
f = open(filename, 'w')
f.writelines(lines)
f.close()