-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
112 lines (93 loc) · 3.34 KB
/
convert.py
File metadata and controls
112 lines (93 loc) · 3.34 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from mercurial import ui, hg
import datetime,os,re,sys
def getgitenv(user, date):
env = ''
elems = re.compile('(.*?)\s+<(.*)>').match(user)
if elems:
env += 'export GIT_AUTHOR_NAME="%s" ;' % elems.group(1)
env += 'export GIT_COMMITER_NAME="%s" ;' % elems.group(1)
env += 'export GIT_AUTHOR_EMAIL="%s" ;' % elems.group(2)
env += 'export GIT_COMMITER_EMAIL="%s" ;' % elems.group(2)
else:
env += 'export GIT_AUTHOR_NAME="%s" ;' % user
env += 'export GIT_COMMITER_NAME="%s" ;' % user
env += 'export GIT_AUTHOR_EMAIL= ;'
env += 'export GIT_COMMITER_EMAIL= ;'
env += 'export GIT_AUTHOR_DATE="%s" ;' % date
env += 'export GIT_COMMITTER_DATE="%s" ;' % date
return env
def oscmd(cmd):
print 'OS -> ',cmd
os.system(cmd)
hgprj = sys.argv[1]
os.chdir(hgprj)
# Maps hg version -> git version
hgvers = {}
# List of children for each hg revision
hgchildren = {}
# Current branch for each hg revision
current_branch = ''
repo = hg.repository(ui.ui(), '.')
gitbranches = ['master']
print 'creating repository'
oscmd('git-init-db')
for change in repo.changelog:
ctx = repo.changectx(change)
date = datetime.datetime.fromtimestamp(ctx.date()[0])
parents = map(lambda x: x.rev(), ctx.parents())
branch = 'master' if ctx.branch()=='default' else ctx.branch()
print '-----------------------------------------'
print 'cset:', change
print 'branch:', branch
print 'user:', ctx.user()
print 'date:', date
print 'comment:', ctx.description()
print 'parent:', parents
print 'tag:', ctx.tags()
print '-----------------------------------------'
if branch != current_branch:
try:
gitbranches.index(branch)
except:
print 'creating new branch', branch
oscmd('git-checkout -b %s %s' % (branch, change))
current_branch = branch
gitbranches.append( current_branch )
else:
print 'checking out branch', branch
oscmd('git-checkout %s' % branch)
# merge
otherbranch = ''
if len(parents) > 1:
for parent in parents:
otherbranch = 'master' if repo.changectx(parent).branch()=='default' else repo.changectx(parent).branch()
if otherbranch != branch:
print 'merging', otherbranch , 'into', branch
oscmd(getgitenv(ctx.user(), date) + 'git-merge --no-commit -s ours %s %s' % (branch, otherbranch))
# remove everything except .git and .hg directories
oscmd('find . \( -path "./.hg" -o -path "./.git" \) -prune -o ! -name "." -print | xargs rm -rf')
# repopulate with checkouted files
oscmd('hg update -C %d' % change)
# add new files
oscmd('git-ls-files -x .hg --others | git-update-index --add --stdin')
# delete removed files
oscmd('git-ls-files -x .hg --deleted | git-update-index --remove --stdin')
# commit
oscmd(getgitenv(ctx.user(), date) + 'git-commit -a -m "%s"' % ctx.description())
# if change == 0 > create the branch
if change == 0:
oscmd('git-checkout -b %s' % branch)
# tag
for tag in ctx.tags():
if tag != 'tip':
oscmd(getgitenv(ctx.user(), date) + 'git-tag "%s"' % tag.replace(' ','_'))
# delete branch if not used anymore...
if otherbranch != '':
print "Deleting unused branch:", otherbranch
oscmd('git-branch -d "%s"' % otherbranch)
# retrieve and record the version
vvv = os.popen('git-show | head -1').read()
vvv = vvv[vvv.index(' ') + 1 : ].strip()
print 'record', change, '->', vvv
hgvers[change] = vvv
oscmd('git-repack -a -d')