3 # Copyright (c) 2012 Felipe Contreras
6 # Inspired by Rocco Rutte's hg-fast-export
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/
12 # For remote repositories a local clone is stored in
13 # "$GIT_DIR/hg/origin/clone/.hg/".
15 from mercurial import hg, ui, bookmarks, context, util, encoding, node, error, extensions
28 # If you want to switch to hg-git compatibility mode:
29 # git config --global remote-hg.hg-git-compat true
31 # If you are not in hg-git-compat mode and want to disable the tracking of
33 # git config --global remote-hg.track-branches false
35 # If you don't want to force pushes (and thus risk creating new remote heads):
36 # git config --global remote-hg.force-push false
38 # If you want the equivalent of hg's clone/pull--insecure option:
39 # git config remote-hg.insecure true
42 # Sensible defaults for git.
43 # hg bookmarks are exported as git branches, hg branches are prefixed
44 # with 'branches/', HEAD is a special case.
48 # Only hg bookmarks are exported as git branches.
49 # Commits are modified to preserve hg information and allow bidirectionality.
52 NAME_RE = re.compile('^([^<>]+)')
53 AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]*)>$')
54 EMAIL_RE = re.compile('^([^<>]+[^ \\\t<>])?\\b(?:[ \\t<>]*?)\\b([^ \\t<>]+@[^ \\t<>]+)')
55 AUTHOR_HG_RE = re.compile('^(.*?) ?<(.*?)(?:>(.+)?)?$')
56 RAW_AUTHOR_RE = re.compile('^(\w+) (?:(.+)? )?<(.*)> (\d+) ([+-]\d+)')
59 sys.stderr.write('ERROR: %s\n' % (msg % args))
63 sys.stderr.write('WARNING: %s\n' % (msg % args))
66 return 'l' in flags and '120000' or 'x' in flags and '100755' or '100644'
69 return '%+03d%02d' % (-tz / 3600, -tz % 3600 / 60)
72 m = { '100755': 'x', '120000': 'l' }
73 return m.get(mode, '')
76 return hg.node.hex(node)
78 def get_config(config):
79 cmd = ['git', 'config', '--get', config]
80 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
81 output, _ = process.communicate()
86 def __init__(self, path):
96 if not os.path.exists(self.path):
99 tmp = json.load(open(self.path))
101 self.tips = tmp['tips']
102 self.marks = tmp['marks']
103 self.last_mark = tmp['last-mark']
105 for rev, mark in self.marks.iteritems():
106 self.rev_marks[mark] = int(rev)
109 return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
112 json.dump(self.dict(), open(self.path, 'w'))
115 return str(self.dict())
117 def from_rev(self, rev):
118 return self.marks[str(rev)]
120 def to_rev(self, mark):
121 return self.rev_marks[mark]
123 def get_mark(self, rev):
125 self.marks[str(rev)] = self.last_mark
126 return self.last_mark
128 def new_mark(self, rev, mark):
129 self.marks[str(rev)] = mark
130 self.rev_marks[mark] = rev
131 self.last_mark = mark
133 def is_marked(self, rev):
134 return str(rev) in self.marks
136 def get_tip(self, branch):
137 return self.tips.get(branch, 0)
139 def set_tip(self, branch, tip):
140 self.tips[branch] = tip
144 def __init__(self, repo):
146 self.line = self.get_line()
149 return sys.stdin.readline().strip()
151 def __getitem__(self, i):
152 return self.line.split()[i]
154 def check(self, word):
155 return self.line.startswith(word)
157 def each_block(self, separator):
158 while self.line != separator:
160 self.line = self.get_line()
163 return self.each_block('')
166 self.line = self.get_line()
167 if self.line == 'done':
171 i = self.line.index(':') + 1
172 return int(self.line[i:])
175 if not self.check('data'):
177 i = self.line.index(' ') + 1
178 size = int(self.line[i:])
179 return sys.stdin.read(size)
181 def get_author(self):
185 m = RAW_AUTHOR_RE.match(self.line)
188 _, name, email, date, tz = m.groups()
189 if name and 'ext:' in name:
190 m = re.match('^(.+?) ext:\((.+)\)$', name)
193 ex = urllib.unquote(m.group(2))
195 if email != bad_mail:
197 user = '%s <%s>' % (name, email)
199 user = '<%s>' % (email)
207 tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
208 return (user, int(date), -tz)
210 def fix_file_path(path):
211 if not os.path.isabs(path):
213 return os.path.relpath(path, '/')
217 path = fix_file_path(fc.path())
218 print "M %s inline %s" % (gitmode(fc.flags()), path)
219 print "data %d" % len(d)
222 def get_filechanges(repo, ctx, parent):
228 prev = repo[parent].manifest().copy()
232 if (cur.flags(fn) != prev.flags(fn) or cur[fn] != prev[fn]):
237 removed |= set(prev.keys())
239 return added | modified, removed
241 def fixup_user_git(user):
243 user = user.replace('"', '')
244 m = AUTHOR_RE.match(user)
247 mail = m.group(2).strip()
249 m = EMAIL_RE.match(user)
254 m = NAME_RE.match(user)
256 name = m.group(1).strip()
259 def fixup_user_hg(user):
261 # stole this from hg-git
262 return re.sub('[<>\n]', '?', name.lstrip('< ').rstrip('> '))
264 m = AUTHOR_HG_RE.match(user)
266 name = sanitize(m.group(1))
267 mail = sanitize(m.group(2))
270 name += ' ext:(' + urllib.quote(ex) + ')'
272 name = sanitize(user)
280 def fixup_user(user):
281 global mode, bad_mail
284 name, mail = fixup_user_git(user)
286 name, mail = fixup_user_hg(user)
293 return '%s <%s>' % (name, mail)
295 def get_repo(url, alias):
299 myui.setconfig('ui', 'interactive', 'off')
300 myui.fout = sys.stderr
303 if get_config('remote-hg.insecure') == 'true\n':
304 myui.setconfig('web', 'cacerts', '')
305 except subprocess.CalledProcessError:
309 mod = extensions.load(myui, 'hgext.schemes', None)
315 repo = hg.repository(myui, url)
317 local_path = os.path.join(dirname, 'clone')
318 if not os.path.exists(local_path):
320 peer, dstpeer = hg.clone(myui, {}, url, local_path, update=True, pull=True)
322 die('Repository error')
323 repo = dstpeer.local()
325 repo = hg.repository(myui, local_path)
327 peer = hg.peer(myui, {}, url)
329 die('Repository error')
330 repo.pull(peer, heads=None, force=True)
334 def rev_to_mark(rev):
336 return marks.from_rev(rev)
338 def mark_to_rev(mark):
340 return marks.to_rev(mark)
342 def export_ref(repo, name, kind, head):
343 global prefix, marks, mode
345 ename = '%s/%s' % (kind, name)
346 tip = marks.get_tip(ename)
348 # mercurial takes too much time checking this
349 if tip and tip == head.rev():
352 revs = xrange(tip, head.rev() + 1)
355 revs = [rev for rev in revs if not marks.is_marked(rev)]
360 (manifest, user, (time, tz), files, desc, extra) = repo.changelog.read(c.node())
361 rev_branch = extra['branch']
363 author = "%s %d %s" % (fixup_user(user), time, gittz(tz))
364 if 'committer' in extra:
365 user, time, tz = extra['committer'].rsplit(' ', 2)
366 committer = "%s %s %s" % (user, time, gittz(int(tz)))
370 parents = [p for p in repo.changelog.parentrevs(rev) if p >= 0]
372 if len(parents) == 0:
373 modified = c.manifest().keys()
376 modified, removed = get_filechanges(repo, c, parents[0])
383 if rev_branch != 'default':
384 extra_msg += 'branch : %s\n' % rev_branch
388 if f not in c.manifest():
390 rename = c.filectx(f).renamed()
392 renames.append((rename[0], f))
395 extra_msg += "rename : %s => %s\n" % e
397 for key, value in extra.iteritems():
398 if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
401 extra_msg += "extra : %s : %s\n" % (key, urllib.quote(value))
404 desc += '\n--HG--\n' + extra_msg
406 if len(parents) == 0 and rev:
407 print 'reset %s/%s' % (prefix, ename)
409 print "commit %s/%s" % (prefix, ename)
410 print "mark :%d" % (marks.get_mark(rev))
411 print "author %s" % (author)
412 print "committer %s" % (committer)
413 print "data %d" % (len(desc))
417 print "from :%s" % (rev_to_mark(parents[0]))
419 print "merge :%s" % (rev_to_mark(parents[1]))
422 export_file(c.filectx(f))
424 print "D %s" % (fix_file_path(f))
428 if (count % 100 == 0):
429 print "progress revision %d '%s' (%d/%d)" % (rev, name, count, len(revs))
430 print "#############################################################"
432 # make sure the ref is updated
433 print "reset %s/%s" % (prefix, ename)
434 print "from :%u" % rev_to_mark(rev)
437 marks.set_tip(ename, rev)
439 def export_tag(repo, tag):
440 export_ref(repo, tag, 'tags', repo[tag])
442 def export_bookmark(repo, bmark):
444 export_ref(repo, bmark, 'bookmarks', head)
446 def export_branch(repo, branch):
447 tip = get_branch_tip(repo, branch)
449 export_ref(repo, branch, 'branches', head)
451 def export_head(repo):
453 export_ref(repo, g_head[0], 'bookmarks', g_head[1])
455 def do_capabilities(parser):
456 global prefix, dirname
460 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
461 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
462 print "refspec refs/tags/*:%s/tags/*" % prefix
464 path = os.path.join(dirname, 'marks-git')
466 if os.path.exists(path):
467 print "*import-marks %s" % path
468 print "*export-marks %s" % path
472 def branch_tip(repo, branch):
473 # older versions of mercurial don't have this
474 if hasattr(repo, 'branchtip'):
475 return repo.branchtip(branch)
477 return repo.branchtags()[branch]
479 def get_branch_tip(repo, branch):
482 heads = branches.get(branch, None)
486 # verify there's only one head
488 warn("Branch '%s' has more than one head, consider merging" % branch)
489 return branch_tip(repo, branch)
493 def list_head(repo, cur):
494 global g_head, bmarks
496 head = bookmarks.readcurrent(repo)
500 # fake bookmark from current branch
507 if head == 'default':
511 print "@refs/heads/%s HEAD" % head
512 g_head = (head, node)
515 global branches, bmarks, mode, track_branches
518 for bmark, node in bookmarks.listbookmarks(repo).iteritems():
519 bmarks[bmark] = repo[node]
521 cur = repo.dirstate.branch()
526 for branch in repo.branchmap():
527 heads = repo.branchheads(branch)
529 branches[branch] = heads
531 for branch in branches:
532 print "? refs/heads/branches/%s" % branch
535 print "? refs/heads/%s" % bmark
537 for tag, node in repo.tagslist():
540 print "? refs/tags/%s" % tag
544 def do_import(parser):
547 path = os.path.join(dirname, 'marks-git')
550 if os.path.exists(path):
551 print "feature import-marks=%s" % path
552 print "feature export-marks=%s" % path
555 tmp = encoding.encoding
556 encoding.encoding = 'utf-8'
558 # lets get all the import lines
559 while parser.check('import'):
564 elif ref.startswith('refs/heads/branches/'):
565 branch = ref[len('refs/heads/branches/'):]
566 export_branch(repo, branch)
567 elif ref.startswith('refs/heads/'):
568 bmark = ref[len('refs/heads/'):]
569 export_bookmark(repo, bmark)
570 elif ref.startswith('refs/tags/'):
571 tag = ref[len('refs/tags/'):]
572 export_tag(repo, tag)
576 encoding.encoding = tmp
580 def parse_blob(parser):
584 mark = parser.get_mark()
586 data = parser.get_data()
587 blob_marks[mark] = data
590 def get_merge_files(repo, p1, p2, files):
591 for e in repo[p1].files():
593 if e not in repo[p1].manifest():
595 f = { 'ctx' : repo[p1][e] }
598 def parse_commit(parser):
599 global marks, blob_marks, parsed_refs
602 from_mark = merge_mark = None
607 commit_mark = parser.get_mark()
609 author = parser.get_author()
611 committer = parser.get_author()
613 data = parser.get_data()
615 if parser.check('from'):
616 from_mark = parser.get_mark()
618 if parser.check('merge'):
619 merge_mark = parser.get_mark()
621 if parser.check('merge'):
622 die('octopus merges are not supported yet')
627 if parser.check('M'):
628 t, m, mark_ref, path = line.split(' ', 3)
629 mark = int(mark_ref[1:])
630 f = { 'mode' : hgmode(m), 'data' : blob_marks[mark] }
631 elif parser.check('D'):
632 t, path = line.split(' ', 1)
633 f = { 'deleted' : True }
635 die('Unknown file command: %s' % line)
638 def getfilectx(repo, memctx, f):
644 is_exec = of['mode'] == 'x'
645 is_link = of['mode'] == 'l'
646 rename = of.get('rename', None)
647 return context.memfilectx(f, of['data'],
648 is_link, is_exec, rename)
652 user, date, tz = author
655 if committer != author:
656 extra['committer'] = "%s %u %u" % committer
659 p1 = repo.changelog.node(mark_to_rev(from_mark))
664 p2 = repo.changelog.node(mark_to_rev(merge_mark))
669 # If files changed from any of the parents, hg wants to know, but in git if
670 # nothing changed from the first parent, nothing changed.
673 get_merge_files(repo, p1, p2, files)
675 # Check if the ref is supposed to be a named branch
676 if ref.startswith('refs/heads/branches/'):
677 extra['branch'] = ref[len('refs/heads/branches/'):]
680 i = data.find('\n--HG--\n')
682 tmp = data[i + len('\n--HG--\n'):].strip()
683 for k, v in [e.split(' : ', 1) for e in tmp.split('\n')]:
685 old, new = v.split(' => ', 1)
686 files[new]['rename'] = old
690 ek, ev = v.split(' : ', 1)
691 extra[ek] = urllib.unquote(ev)
694 ctx = context.memctx(repo, (p1, p2), data,
695 files.keys(), getfilectx,
696 user, (date, tz), extra)
698 tmp = encoding.encoding
699 encoding.encoding = 'utf-8'
701 node = repo.commitctx(ctx)
703 encoding.encoding = tmp
705 rev = repo[node].rev()
707 parsed_refs[ref] = node
708 marks.new_mark(rev, commit_mark)
710 def parse_reset(parser):
716 if parser.check('commit'):
719 if not parser.check('from'):
721 from_mark = parser.get_mark()
724 node = parser.repo.changelog.node(mark_to_rev(from_mark))
725 parsed_refs[ref] = node
727 def parse_tag(parser):
730 from_mark = parser.get_mark()
732 tagger = parser.get_author()
734 data = parser.get_data()
737 parsed_tags[name] = (tagger, data)
739 def write_tag(repo, tag, node, msg, author):
740 branch = repo[node].branch()
741 tip = branch_tip(repo, branch)
744 def getfilectx(repo, memctx, f):
746 fctx = tip.filectx(f)
748 except error.ManifestLookupError:
750 content = data + "%s %s\n" % (hghex(node), tag)
751 return context.memfilectx(f, content, False, False, None)
756 author = (None, 0, 0)
757 user, date, tz = author
759 ctx = context.memctx(repo, (p1, p2), msg,
760 ['.hgtags'], getfilectx,
761 user, (date, tz), {'branch' : branch})
763 tmp = encoding.encoding
764 encoding.encoding = 'utf-8'
766 tagnode = repo.commitctx(ctx)
768 encoding.encoding = tmp
772 def do_export(parser):
773 global parsed_refs, bmarks, peer
779 for line in parser.each_block('done'):
780 if parser.check('blob'):
782 elif parser.check('commit'):
784 elif parser.check('reset'):
786 elif parser.check('tag'):
788 elif parser.check('feature'):
791 die('unhandled export command: %s' % line)
793 for ref, node in parsed_refs.iteritems():
794 if ref.startswith('refs/heads/branches'):
795 branch = ref[len('refs/heads/branches/'):]
796 if branch in branches and node in branches[branch]:
800 elif ref.startswith('refs/heads/'):
801 bmark = ref[len('refs/heads/'):]
802 p_bmarks.append((bmark, node))
804 elif ref.startswith('refs/tags/'):
805 tag = ref[len('refs/tags/'):]
806 author, msg = parsed_tags.get(tag, (None, None))
809 msg = 'Added tag %s for changeset %s' % (tag, hghex(node[:6]));
810 write_tag(parser.repo, tag, node, msg, author)
812 fp = parser.repo.opener('localtags', 'a')
813 fp.write('%s %s\n' % (hghex(node), tag))
817 # transport-helper/fast-export bugs
821 parser.repo.push(peer, force=force_push)
824 for bmark, node in p_bmarks:
825 ref = 'refs/heads/' + bmark
829 old = bmarks[bmark].hex()
833 if bmark == 'master' and 'master' not in parser.repo._bookmarks:
836 elif bookmarks.pushbookmark(parser.repo, bmark, old, new):
840 print "error %s" % ref
844 rb = peer.listkeys('bookmarks')
845 old = rb.get(bmark, '')
846 if not peer.pushkey('bookmarks', bmark, old, new):
847 print "error %s" % ref
854 def fix_path(alias, repo, orig_url):
855 url = urlparse.urlparse(orig_url, 'file')
856 if url.scheme != 'file' or os.path.isabs(url.path):
858 abs_url = urlparse.urljoin("%s/" % os.getcwd(), orig_url)
859 cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % abs_url]
863 global prefix, dirname, branches, bmarks
864 global marks, blob_marks, parsed_refs
865 global peer, mode, bad_mail, bad_name
866 global track_branches, force_push, is_tmp
873 hg_git_compat = False
874 track_branches = True
878 if get_config('remote-hg.hg-git-compat') == 'true\n':
880 track_branches = False
881 if get_config('remote-hg.track-branches') == 'false\n':
882 track_branches = False
883 if get_config('remote-hg.force-push') == 'false\n':
885 except subprocess.CalledProcessError:
890 bad_mail = 'none@none'
899 alias = util.sha1(alias).hexdigest()
903 gitdir = os.environ['GIT_DIR']
904 dirname = os.path.join(gitdir, 'hg', alias)
912 repo = get_repo(url, alias)
913 prefix = 'refs/hg/%s' % alias
916 fix_path(alias, peer or repo, url)
918 if not os.path.exists(dirname):
921 marks_path = os.path.join(dirname, 'marks-hg')
922 marks = Marks(marks_path)
924 parser = Parser(repo)
926 if parser.check('capabilities'):
927 do_capabilities(parser)
928 elif parser.check('list'):
930 elif parser.check('import'):
932 elif parser.check('export'):
935 die('unhandled command: %s' % line)
944 shutil.rmtree(dirname)
947 sys.exit(main(sys.argv))