fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / hgfe.py
blob53caafa96d147e147ff3745a7301035c9d1fe8fc
1 from mercurial import hg, ui
2 from mercurial import revlog
3 from mercurial.node import hex
5 def data(x):
6 return 'data %i\n%s' % (len(x), x)
8 all_marks = dict()
9 def get_mark(x):
10 if not x in all_marks:
11 all_marks[x] = len(all_marks) + 1
12 return ':%d' % all_marks[x]
14 def add_mark(x, mark):
15 all_marks[x] = mark
17 def get_user(ctx):
18 user = ctx.user()
19 if user.find('<') < 0:
20 user = user + ' <' + user + '>'
21 return user
23 all_tags = dict()
24 def fast_export(path):
26 repo = hg.repository(ui.ui(), path)
27 for i in repo:
29 ctx = repo[i]
30 if '.hgtags' in ctx.files():
31 for line in ctx.filectx('.hgtags').data().splitlines():
32 tag = line.split(' ')
33 if tag[1] in all_tags and all_tags[tag[1]] == tag[0]:
34 continue
35 all_tags[tag[1]] = tag[0]
36 print 'tag', tag[1]
37 print 'from', get_mark(tag[0])
38 ts, tz = ctx.date()
39 print 'tagger', get_user(ctx), int(ts), '%+i' % tz
40 print data(ctx.description())
42 for fn in ctx.files():
43 try:
44 fctx = ctx.filectx(fn)
45 except revlog.LookupError:
46 continue
47 print 'blob'
48 print 'mark', get_mark(hex(fctx.filenode()))
49 print data(fctx.data())
51 branch = ctx.branch()
52 if not branch.startswith('refs/heads/'):
53 branch = 'refs/heads/' + branch
54 print 'commit', branch
55 print 'mark', get_mark(ctx.hex())
56 ts, tz = ctx.date()
57 print 'committer', get_user(ctx), int(ts), '%+i' % tz
58 print data(ctx.description())
60 pars = ctx.parents()
61 for i, type in ((0, 'from'), (1, 'merge')):
62 if len(pars) == i:
63 continue
64 if pars[i] != revlog.nullid and pars[i].hex() != '0000000000000000000000000000000000000000':
65 if not pars[i].hex() in all_marks:
66 raise Exception('parent not seen yet')
67 print type, get_mark(pars[i].hex())
69 deletes = dict()
70 for fn in ctx.files():
71 try:
72 fctx = ctx.filectx(fn)
73 except revlog.LookupError:
74 if not fn in deletes:
75 deletes[fn] = 'delete'
76 continue
77 renamed = fctx.renamed()
78 if renamed:
79 print 'R', renamed[0], fn
80 deletes[fn] = 'rename'
81 continue
82 flags = fctx.flags()
83 if 'x' in flags:
84 mode = '755'
85 elif 'l' in flags:
86 mode = '120000'
87 else:
88 mode = '644'
89 print 'M', mode, get_mark(hex(fctx.filenode())), fn
91 for fn in deletes:
92 if deletes[fn] == 'delete':
93 print 'D', fn
95 print ''
97 if __name__ == '__main__':
98 import sys
99 fast_export(sys.argv[1])