1#!/usr/bin/env python
2#
3# Copyright (c) 2012 Felipe Contreras
4#
5
6# Inspired by Rocco Rutte's hg-fast-export
7
8# Just copy to your ~/bin, or anywhere in your $PATH.
9# Then you can clone with:
10# git clone hg::/path/to/mercurial/repo/
11
12from mercurial import hg, ui, bookmarks, context, util, encoding, node
13
14import re
15import sys
16import os
17import json
18import shutil
19import subprocess
20import urllib
21
22#
23# If you want to switch to hg-git compatibility mode:
24# git config --global remote-hg.hg-git-compat true
25#
26# If you are not in hg-git-compat mode and want to disable the tracking of
27# named branches:
28# git config --global remote-hg.track-branches false
29#
30# git:
31# Sensible defaults for git.
32# hg bookmarks are exported as git branches, hg branches are prefixed
33# with 'branches/', HEAD is a special case.
34#
35# hg:
36# Emulate hg-git.
37# Only hg bookmarks are exported as git branches.
38# Commits are modified to preserve hg information and allow bidirectionality.
39#
40
41NAME_RE = re.compile('^([^<>]+)')
42AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]*)>$')
43AUTHOR_HG_RE = re.compile('^(.*?) ?<(.*?)(?:>(.+)?)?$')
44RAW_AUTHOR_RE = re.compile('^(\w+) (?:(.+)? )?<(.*)> (\d+) ([+-]\d+)')
45
46def die(msg, *args):
47 sys.stderr.write('ERROR: %s\n' % (msg % args))
48 sys.exit(1)
49
50def warn(msg, *args):
51 sys.stderr.write('WARNING: %s\n' % (msg % args))
52
53def gitmode(flags):
54 return 'l' in flags and '120000' or 'x' in flags and '100755' or '100644'
55
56def gittz(tz):
57 return '%+03d%02d' % (-tz / 3600, -tz % 3600 / 60)
58
59def hgmode(mode):
60 m = { '100755': 'x', '120000': 'l' }
61 return m.get(mode, '')
62
63def hghex(node):
64 return hg.node.hex(node)
65
66def get_config(config):
67 cmd = ['git', 'config', '--get', config]
68 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
69 output, _ = process.communicate()
70 return output
71
72class Marks:
73
74 def __init__(self, path):
75 self.path = path
76 self.tips = {}
77 self.marks = {}
78 self.rev_marks = {}
79 self.last_mark = 0
80
81 self.load()
82
83 def load(self):
84 if not os.path.exists(self.path):
85 return
86
87 tmp = json.load(open(self.path))
88
89 self.tips = tmp['tips']
90 self.marks = tmp['marks']
91 self.last_mark = tmp['last-mark']
92
93 for rev, mark in self.marks.iteritems():
94 self.rev_marks[mark] = int(rev)
95
96 def dict(self):
97 return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
98
99 def store(self):
100 json.dump(self.dict(), open(self.path, 'w'))
101
102 def __str__(self):
103 return str(self.dict())
104
105 def from_rev(self, rev):
106 return self.marks[str(rev)]
107
108 def to_rev(self, mark):
109 return self.rev_marks[mark]
110
111 def get_mark(self, rev):
112 self.last_mark += 1
113 self.marks[str(rev)] = self.last_mark
114 return self.last_mark
115
116 def new_mark(self, rev, mark):
117 self.marks[str(rev)] = mark
118 self.rev_marks[mark] = rev
119 self.last_mark = mark
120
121 def is_marked(self, rev):
122 return self.marks.has_key(str(rev))
123
124 def get_tip(self, branch):
125 return self.tips.get(branch, 0)
126
127 def set_tip(self, branch, tip):
128 self.tips[branch] = tip
129
130class Parser:
131
132 def __init__(self, repo):
133 self.repo = repo
134 self.line = self.get_line()
135
136 def get_line(self):
137 return sys.stdin.readline().strip()
138
139 def __getitem__(self, i):
140 return self.line.split()[i]
141
142 def check(self, word):
143 return self.line.startswith(word)
144
145 def each_block(self, separator):
146 while self.line != separator:
147 yield self.line
148 self.line = self.get_line()
149
150 def __iter__(self):
151 return self.each_block('')
152
153 def next(self):
154 self.line = self.get_line()
155 if self.line == 'done':
156 self.line = None
157
158 def get_mark(self):
159 i = self.line.index(':') + 1
160 return int(self.line[i:])
161
162 def get_data(self):
163 if not self.check('data'):
164 return None
165 i = self.line.index(' ') + 1
166 size = int(self.line[i:])
167 return sys.stdin.read(size)
168
169 def get_author(self):
170 global bad_mail
171
172 ex = None
173 m = RAW_AUTHOR_RE.match(self.line)
174 if not m:
175 return None
176 _, name, email, date, tz = m.groups()
177 if name and 'ext:' in name:
178 m = re.match('^(.+?) ext:\((.+)\)$', name)
179 if m:
180 name = m.group(1)
181 ex = urllib.unquote(m.group(2))
182
183 if email != bad_mail:
184 if name:
185 user = '%s <%s>' % (name, email)
186 else:
187 user = '<%s>' % (email)
188 else:
189 user = name
190
191 if ex:
192 user += ex
193
194 tz = int(tz)
195 tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
196 return (user, int(date), -tz)
197
198def export_file(fc):
199 d = fc.data()
200 print "M %s inline %s" % (gitmode(fc.flags()), fc.path())
201 print "data %d" % len(d)
202 print d
203
204def get_filechanges(repo, ctx, parent):
205 modified = set()
206 added = set()
207 removed = set()
208
209 cur = ctx.manifest()
210 prev = repo[parent].manifest().copy()
211
212 for fn in cur:
213 if fn in prev:
214 if (cur.flags(fn) != prev.flags(fn) or cur[fn] != prev[fn]):
215 modified.add(fn)
216 del prev[fn]
217 else:
218 added.add(fn)
219 removed |= set(prev.keys())
220
221 return added | modified, removed
222
223def fixup_user_git(user):
224 name = mail = None
225 user = user.replace('"', '')
226 m = AUTHOR_RE.match(user)
227 if m:
228 name = m.group(1)
229 mail = m.group(2).strip()
230 else:
231 m = NAME_RE.match(user)
232 if m:
233 name = m.group(1).strip()
234 return (name, mail)
235
236def fixup_user_hg(user):
237 def sanitize(name):
238 # stole this from hg-git
239 return re.sub('[<>\n]', '?', name.lstrip('< ').rstrip('> '))
240
241 m = AUTHOR_HG_RE.match(user)
242 if m:
243 name = sanitize(m.group(1))
244 mail = sanitize(m.group(2))
245 ex = m.group(3)
246 if ex:
247 name += ' ext:(' + urllib.quote(ex) + ')'
248 else:
249 name = sanitize(user)
250 if '@' in user:
251 mail = name
252 else:
253 mail = None
254
255 return (name, mail)
256
257def fixup_user(user):
258 global mode, bad_mail
259
260 if mode == 'git':
261 name, mail = fixup_user_git(user)
262 else:
263 name, mail = fixup_user_hg(user)
264
265 if not name:
266 name = bad_name
267 if not mail:
268 mail = bad_mail
269
270 return '%s <%s>' % (name, mail)
271
272def get_repo(url, alias):
273 global dirname, peer
274
275 myui = ui.ui()
276 myui.setconfig('ui', 'interactive', 'off')
277 myui.fout = sys.stderr
278
279 if hg.islocal(url):
280 repo = hg.repository(myui, url)
281 else:
282 local_path = os.path.join(dirname, 'clone')
283 if not os.path.exists(local_path):
284 peer, dstpeer = hg.clone(myui, {}, url, local_path, update=False, pull=True)
285 repo = dstpeer.local()
286 else:
287 repo = hg.repository(myui, local_path)
288 peer = hg.peer(myui, {}, url)
289 repo.pull(peer, heads=None, force=True)
290
291 return repo
292
293def rev_to_mark(rev):
294 global marks
295 return marks.from_rev(rev)
296
297def mark_to_rev(mark):
298 global marks
299 return marks.to_rev(mark)
300
301def export_ref(repo, name, kind, head):
302 global prefix, marks, mode
303
304 ename = '%s/%s' % (kind, name)
305 tip = marks.get_tip(ename)
306
307 # mercurial takes too much time checking this
308 if tip and tip == head.rev():
309 # nothing to do
310 return
311 revs = xrange(tip, head.rev() + 1)
312 count = 0
313
314 revs = [rev for rev in revs if not marks.is_marked(rev)]
315
316 for rev in revs:
317
318 c = repo[rev]
319 (manifest, user, (time, tz), files, desc, extra) = repo.changelog.read(c.node())
320 rev_branch = extra['branch']
321
322 author = "%s %d %s" % (fixup_user(user), time, gittz(tz))
323 if 'committer' in extra:
324 user, time, tz = extra['committer'].rsplit(' ', 2)
325 committer = "%s %s %s" % (user, time, gittz(int(tz)))
326 else:
327 committer = author
328
329 parents = [p for p in repo.changelog.parentrevs(rev) if p >= 0]
330
331 if len(parents) == 0:
332 modified = c.manifest().keys()
333 removed = []
334 else:
335 modified, removed = get_filechanges(repo, c, parents[0])
336
337 if mode == 'hg':
338 extra_msg = ''
339
340 if rev_branch != 'default':
341 extra_msg += 'branch : %s\n' % rev_branch
342
343 renames = []
344 for f in c.files():
345 if f not in c.manifest():
346 continue
347 rename = c.filectx(f).renamed()
348 if rename:
349 renames.append((rename[0], f))
350
351 for e in renames:
352 extra_msg += "rename : %s => %s\n" % e
353
354 for key, value in extra.iteritems():
355 if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
356 continue
357 else:
358 extra_msg += "extra : %s : %s\n" % (key, urllib.quote(value))
359
360 desc += '\n'
361 if extra_msg:
362 desc += '\n--HG--\n' + extra_msg
363
364 if len(parents) == 0 and rev:
365 print 'reset %s/%s' % (prefix, ename)
366
367 print "commit %s/%s" % (prefix, ename)
368 print "mark :%d" % (marks.get_mark(rev))
369 print "author %s" % (author)
370 print "committer %s" % (committer)
371 print "data %d" % (len(desc))
372 print desc
373
374 if len(parents) > 0:
375 print "from :%s" % (rev_to_mark(parents[0]))
376 if len(parents) > 1:
377 print "merge :%s" % (rev_to_mark(parents[1]))
378
379 for f in modified:
380 export_file(c.filectx(f))
381 for f in removed:
382 print "D %s" % (f)
383 print
384
385 count += 1
386 if (count % 100 == 0):
387 print "progress revision %d '%s' (%d/%d)" % (rev, name, count, len(revs))
388 print "#############################################################"
389
390 # make sure the ref is updated
391 print "reset %s/%s" % (prefix, ename)
392 print "from :%u" % rev_to_mark(rev)
393 print
394
395 marks.set_tip(ename, rev)
396
397def export_tag(repo, tag):
398 export_ref(repo, tag, 'tags', repo[tag])
399
400def export_bookmark(repo, bmark):
401 head = bmarks[bmark]
402 export_ref(repo, bmark, 'bookmarks', head)
403
404def export_branch(repo, branch):
405 tip = get_branch_tip(repo, branch)
406 head = repo[tip]
407 export_ref(repo, branch, 'branches', head)
408
409def export_head(repo):
410 global g_head
411 export_ref(repo, g_head[0], 'bookmarks', g_head[1])
412
413def do_capabilities(parser):
414 global prefix, dirname
415
416 print "import"
417 print "export"
418 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
419 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
420 print "refspec refs/tags/*:%s/tags/*" % prefix
421
422 path = os.path.join(dirname, 'marks-git')
423
424 if os.path.exists(path):
425 print "*import-marks %s" % path
426 print "*export-marks %s" % path
427
428 print
429
430def get_branch_tip(repo, branch):
431 global branches
432
433 heads = branches.get(branch, None)
434 if not heads:
435 return None
436
437 # verify there's only one head
438 if (len(heads) > 1):
439 warn("Branch '%s' has more than one head, consider merging" % branch)
440 # older versions of mercurial don't have this
441 if hasattr(repo, "branchtip"):
442 return repo.branchtip(branch)
443
444 return heads[0]
445
446def list_head(repo, cur):
447 global g_head, bmarks
448
449 head = bookmarks.readcurrent(repo)
450 if head:
451 node = repo[head]
452 else:
453 # fake bookmark from current branch
454 head = cur
455 node = repo['.']
456 if not node:
457 node = repo['tip']
458 if not node:
459 return
460 if head == 'default':
461 head = 'master'
462 bmarks[head] = node
463
464 print "@refs/heads/%s HEAD" % head
465 g_head = (head, node)
466
467def do_list(parser):
468 global branches, bmarks, mode, track_branches
469
470 repo = parser.repo
471 for bmark, node in bookmarks.listbookmarks(repo).iteritems():
472 bmarks[bmark] = repo[node]
473
474 cur = repo.dirstate.branch()
475
476 list_head(repo, cur)
477
478 if track_branches:
479 for branch in repo.branchmap():
480 heads = repo.branchheads(branch)
481 if len(heads):
482 branches[branch] = heads
483
484 for branch in branches:
485 print "? refs/heads/branches/%s" % branch
486
487 for bmark in bmarks:
488 print "? refs/heads/%s" % bmark
489
490 for tag, node in repo.tagslist():
491 if tag == 'tip':
492 continue
493 print "? refs/tags/%s" % tag
494
495 print
496
497def do_import(parser):
498 repo = parser.repo
499
500 path = os.path.join(dirname, 'marks-git')
501
502 print "feature done"
503 if os.path.exists(path):
504 print "feature import-marks=%s" % path
505 print "feature export-marks=%s" % path
506 sys.stdout.flush()
507
508 tmp = encoding.encoding
509 encoding.encoding = 'utf-8'
510
511 # lets get all the import lines
512 while parser.check('import'):
513 ref = parser[1]
514
515 if (ref == 'HEAD'):
516 export_head(repo)
517 elif ref.startswith('refs/heads/branches/'):
518 branch = ref[len('refs/heads/branches/'):]
519 export_branch(repo, branch)
520 elif ref.startswith('refs/heads/'):
521 bmark = ref[len('refs/heads/'):]
522 export_bookmark(repo, bmark)
523 elif ref.startswith('refs/tags/'):
524 tag = ref[len('refs/tags/'):]
525 export_tag(repo, tag)
526
527 parser.next()
528
529 encoding.encoding = tmp
530
531 print 'done'
532
533def parse_blob(parser):
534 global blob_marks
535
536 parser.next()
537 mark = parser.get_mark()
538 parser.next()
539 data = parser.get_data()
540 blob_marks[mark] = data
541 parser.next()
542
543def get_merge_files(repo, p1, p2, files):
544 for e in repo[p1].files():
545 if e not in files:
546 if e not in repo[p1].manifest():
547 continue
548 f = { 'ctx' : repo[p1][e] }
549 files[e] = f
550
551def parse_commit(parser):
552 global marks, blob_marks, parsed_refs
553 global mode
554
555 from_mark = merge_mark = None
556
557 ref = parser[1]
558 parser.next()
559
560 commit_mark = parser.get_mark()
561 parser.next()
562 author = parser.get_author()
563 parser.next()
564 committer = parser.get_author()
565 parser.next()
566 data = parser.get_data()
567 parser.next()
568 if parser.check('from'):
569 from_mark = parser.get_mark()
570 parser.next()
571 if parser.check('merge'):
572 merge_mark = parser.get_mark()
573 parser.next()
574 if parser.check('merge'):
575 die('octopus merges are not supported yet')
576
577 files = {}
578
579 for line in parser:
580 if parser.check('M'):
581 t, m, mark_ref, path = line.split(' ', 3)
582 mark = int(mark_ref[1:])
583 f = { 'mode' : hgmode(m), 'data' : blob_marks[mark] }
584 elif parser.check('D'):
585 t, path = line.split(' ', 1)
586 f = { 'deleted' : True }
587 else:
588 die('Unknown file command: %s' % line)
589 files[path] = f
590
591 def getfilectx(repo, memctx, f):
592 of = files[f]
593 if 'deleted' in of:
594 raise IOError
595 if 'ctx' in of:
596 return of['ctx']
597 is_exec = of['mode'] == 'x'
598 is_link = of['mode'] == 'l'
599 rename = of.get('rename', None)
600 return context.memfilectx(f, of['data'],
601 is_link, is_exec, rename)
602
603 repo = parser.repo
604
605 user, date, tz = author
606 extra = {}
607
608 if committer != author:
609 extra['committer'] = "%s %u %u" % committer
610
611 if from_mark:
612 p1 = repo.changelog.node(mark_to_rev(from_mark))
613 else:
614 p1 = '\0' * 20
615
616 if merge_mark:
617 p2 = repo.changelog.node(mark_to_rev(merge_mark))
618 else:
619 p2 = '\0' * 20
620
621 #
622 # If files changed from any of the parents, hg wants to know, but in git if
623 # nothing changed from the first parent, nothing changed.
624 #
625 if merge_mark:
626 get_merge_files(repo, p1, p2, files)
627
628 # Check if the ref is supposed to be a named branch
629 if ref.startswith('refs/heads/branches/'):
630 extra['branch'] = ref[len('refs/heads/branches/'):]
631
632 if mode == 'hg':
633 i = data.find('\n--HG--\n')
634 if i >= 0:
635 tmp = data[i + len('\n--HG--\n'):].strip()
636 for k, v in [e.split(' : ', 1) for e in tmp.split('\n')]:
637 if k == 'rename':
638 old, new = v.split(' => ', 1)
639 files[new]['rename'] = old
640 elif k == 'branch':
641 extra[k] = v
642 elif k == 'extra':
643 ek, ev = v.split(' : ', 1)
644 extra[ek] = urllib.unquote(ev)
645 data = data[:i]
646
647 ctx = context.memctx(repo, (p1, p2), data,
648 files.keys(), getfilectx,
649 user, (date, tz), extra)
650
651 tmp = encoding.encoding
652 encoding.encoding = 'utf-8'
653
654 node = repo.commitctx(ctx)
655
656 encoding.encoding = tmp
657
658 rev = repo[node].rev()
659
660 parsed_refs[ref] = node
661 marks.new_mark(rev, commit_mark)
662
663def parse_reset(parser):
664 global parsed_refs
665
666 ref = parser[1]
667 parser.next()
668 # ugh
669 if parser.check('commit'):
670 parse_commit(parser)
671 return
672 if not parser.check('from'):
673 return
674 from_mark = parser.get_mark()
675 parser.next()
676
677 node = parser.repo.changelog.node(mark_to_rev(from_mark))
678 parsed_refs[ref] = node
679
680def parse_tag(parser):
681 name = parser[1]
682 parser.next()
683 from_mark = parser.get_mark()
684 parser.next()
685 tagger = parser.get_author()
686 parser.next()
687 data = parser.get_data()
688 parser.next()
689
690 # nothing to do
691
692def do_export(parser):
693 global parsed_refs, bmarks, peer
694
695 p_bmarks = []
696
697 parser.next()
698
699 for line in parser.each_block('done'):
700 if parser.check('blob'):
701 parse_blob(parser)
702 elif parser.check('commit'):
703 parse_commit(parser)
704 elif parser.check('reset'):
705 parse_reset(parser)
706 elif parser.check('tag'):
707 parse_tag(parser)
708 elif parser.check('feature'):
709 pass
710 else:
711 die('unhandled export command: %s' % line)
712
713 for ref, node in parsed_refs.iteritems():
714 if ref.startswith('refs/heads/branches'):
715 print "ok %s" % ref
716 elif ref.startswith('refs/heads/'):
717 bmark = ref[len('refs/heads/'):]
718 p_bmarks.append((bmark, node))
719 continue
720 elif ref.startswith('refs/tags/'):
721 tag = ref[len('refs/tags/'):]
722 if mode == 'git':
723 msg = 'Added tag %s for changeset %s' % (tag, hghex(node[:6]));
724 parser.repo.tag([tag], node, msg, False, None, {})
725 else:
726 parser.repo.tag([tag], node, None, True, None, {})
727 print "ok %s" % ref
728 else:
729 # transport-helper/fast-export bugs
730 continue
731
732 if peer:
733 parser.repo.push(peer, force=False)
734
735 # handle bookmarks
736 for bmark, node in p_bmarks:
737 ref = 'refs/heads/' + bmark
738 new = hghex(node)
739
740 if bmark in bmarks:
741 old = bmarks[bmark].hex()
742 else:
743 old = ''
744
745 if bmark == 'master' and 'master' not in parser.repo._bookmarks:
746 # fake bookmark
747 pass
748 elif bookmarks.pushbookmark(parser.repo, bmark, old, new):
749 # updated locally
750 pass
751 else:
752 print "error %s" % ref
753 continue
754
755 if peer:
756 if not peer.pushkey('bookmarks', bmark, old, new):
757 print "error %s" % ref
758 continue
759
760 print "ok %s" % ref
761
762 print
763
764def fix_path(alias, repo, orig_url):
765 repo_url = util.url(repo.url())
766 url = util.url(orig_url)
767 if str(url) == str(repo_url):
768 return
769 cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % repo_url]
770 subprocess.call(cmd)
771
772def main(args):
773 global prefix, dirname, branches, bmarks
774 global marks, blob_marks, parsed_refs
775 global peer, mode, bad_mail, bad_name
776 global track_branches
777
778 alias = args[1]
779 url = args[2]
780 peer = None
781
782 hg_git_compat = False
783 track_branches = True
784 try:
785 if get_config('remote-hg.hg-git-compat') == 'true\n':
786 hg_git_compat = True
787 track_branches = False
788 if get_config('remote-hg.track-branches') == 'false\n':
789 track_branches = False
790 except subprocess.CalledProcessError:
791 pass
792
793 if hg_git_compat:
794 mode = 'hg'
795 bad_mail = 'none@none'
796 bad_name = ''
797 else:
798 mode = 'git'
799 bad_mail = 'unknown'
800 bad_name = 'Unknown'
801
802 if alias[4:] == url:
803 is_tmp = True
804 alias = util.sha1(alias).hexdigest()
805 else:
806 is_tmp = False
807
808 gitdir = os.environ['GIT_DIR']
809 dirname = os.path.join(gitdir, 'hg', alias)
810 branches = {}
811 bmarks = {}
812 blob_marks = {}
813 parsed_refs = {}
814
815 repo = get_repo(url, alias)
816 prefix = 'refs/hg/%s' % alias
817
818 if not is_tmp:
819 fix_path(alias, peer or repo, url)
820
821 if not os.path.exists(dirname):
822 os.makedirs(dirname)
823
824 marks_path = os.path.join(dirname, 'marks-hg')
825 marks = Marks(marks_path)
826
827 parser = Parser(repo)
828 for line in parser:
829 if parser.check('capabilities'):
830 do_capabilities(parser)
831 elif parser.check('list'):
832 do_list(parser)
833 elif parser.check('import'):
834 do_import(parser)
835 elif parser.check('export'):
836 do_export(parser)
837 else:
838 die('unhandled command: %s' % line)
839 sys.stdout.flush()
840
841 if not is_tmp:
842 marks.store()
843 else:
844 shutil.rmtree(dirname)
845
846sys.exit(main(sys.argv))