forked from sagemath/sagenb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapply-hg.py
37 lines (31 loc) · 835 Bytes
/
apply-hg.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
#! /usr/bin/python
# Original script by Ondrej Certik: http://git.661346.n2.nabble.com/importing-mercurial-patch-td1484627.html
import os
import sys
import re
import tempfile
def run(cmd):
print cmd
execcode=os.system(cmd)
if execcode != 0:
raise RuntimeError
patch = sys.argv[1]
args = ' '.join(sys.argv[2:])
print 'p',patch
print 'args',args
with open(patch) as f:
p = f.read()
author = re.search("# User (.+)", p).groups()[0]
p = p.split("\n")
while not p[0].startswith("# Parent"):
del p[0]
i = 1
while not p[i].startswith("diff -r "):
i += 1
commit_message = "\n".join(p[1:i])
_, filename = tempfile.mkstemp()
with open(filename, "w") as f:
f.write(commit_message)
print commit_message
run("patch %s < %s" %(args,patch))
run("git commit -a --author='%s' -F %s -s" % (author, filename) )