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
27 # If you want to switch to hg-git compatibility mode:
28 # git config --global remote-hg.hg-git-compat true
30 # If you are not in hg-git-compat mode and want to disable the tracking of
32 # git config --global remote-hg.track-branches false
34 # If you don't want to force pushes (and thus risk creating new remote heads):
35 # git config --global remote-hg.force-push false
37 # If you want the equivalent of hg's clone/pull--insecure option:
38 # git config remote-hg.insecure true
41 # Sensible defaults for git.
42 # hg bookmarks are exported as git branches, hg branches are prefixed
43 # with 'branches/', HEAD is a special case.
47 # Only hg bookmarks are exported as git branches.
48 # Commits are modified to preserve hg information and allow bidirectionality.
51 NAME_RE = re.compile('^([^<>]+)')
52 AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]*)>$')
53 AUTHOR_HG_RE = re.compile('^(.*?) ?<(.*?)(?:>(.+)?)?$')
54 RAW_AUTHOR_RE = re.compile('^(\w+) (?:(.+)? )?<(.*)> (\d+) ([+-]\d+)')
57 sys.stderr.write('ERROR: %s\n' % (msg % args))
61 sys.stderr.write('WARNING: %s\n' % (msg % args))
64 return 'l' in flags and '120000' or 'x' in flags and '100755' or '100644'
67 return '%+03d%02d' % (-tz / 3600, -tz % 3600 / 60)
70 m = { '100755': 'x', '120000': 'l' }
71 return m.get(mode, '')
74 return hg.node.hex(node)
76 def get_config(config):
77 cmd = ['git', 'config', '--get', config]
78 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
79 output, _ = process.communicate()
84 def __init__(self, path):
94 if not os.path.exists(self.path):
97 tmp = json.load(open(self.path))
99 self.tips = tmp['tips']
100 self.marks = tmp['marks']
101 self.last_mark = tmp['last-mark']
103 for rev, mark in self.marks.iteritems():
104 self.rev_marks[mark] = int(rev)
107 return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
110 json.dump(self.dict(), open(self.path, 'w'))
113 return str(self.dict())
115 def from_rev(self, rev):
116 return self.marks[str(rev)]
118 def to_rev(self, mark):
119 return self.rev_marks[mark]
121 def get_mark(self, rev):
123 self.marks[str(rev)] = self.last_mark
124 return self.last_mark
126 def new_mark(self, rev, mark):
127 self.marks[str(rev)] = mark
128 self.rev_marks[mark] = rev
129 self.last_mark = mark
131 def is_marked(self, rev):
132 return self.marks.has_key(str(rev))
134 def get_tip(self, branch):
135 return self.tips.get(branch, 0)
137 def set_tip(self, branch, tip):
138 self.tips[branch] = tip
142 def __init__(self, repo):
144 self.line = self.get_line()
147 return sys.stdin.readline().strip()
149 def __getitem__(self, i):
150 return self.line.split()[i]
152 def check(self, word):
153 return self.line.startswith(word)
155 def each_block(self, separator):
156 while self.line != separator:
158 self.line = self.get_line()
161 return self.each_block('')
164 self.line = self.get_line()
165 if self.line == 'done':
169 i = self.line.index(':') + 1
170 return int(self.line[i:])
173 if not self.check('data'):
175 i = self.line.index(' ') + 1
176 size = int(self.line[i:])
177 return sys.stdin.read(size)
179 def get_author(self):
183 m = RAW_AUTHOR_RE.match(self.line)
186 _, name, email, date, tz = m.groups()
187 if name and 'ext:' in name:
188 m = re.match('^(.+?) ext:\((.+)\)$', name)
191 ex = urllib.unquote(m.group(2))
193 if email != bad_mail:
195 user = '%s <%s>' % (name, email)
197 user = '<%s>' % (email)
205 tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
206 return (user, int(date), -tz)
208 def fix_file_path(path):
209 if not os.path.isabs(path):
211 return os.path.relpath(path, '/')
215 path = fix_file_path(fc.path())
216 print "M %s inline %s" % (gitmode(fc.flags()), path)
217 print "data %d" % len(d)
220 def get_filechanges(repo, ctx, parent):
226 prev = repo[parent].manifest().copy()
230 if (cur.flags(fn) != prev.flags(fn) or cur[fn] != prev[fn]):
235 removed |= set(prev.keys())
237 return added | modified, removed
239 def fixup_user_git(user):
241 user = user.replace('"', '')
242 m = AUTHOR_RE.match(user)
245 mail = m.group(2).strip()
247 m = NAME_RE.match(user)
249 name = m.group(1).strip()
252 def fixup_user_hg(user):
254 # stole this from hg-git
255 return re.sub('[<>\n]', '?', name.lstrip('< ').rstrip('> '))
257 m = AUTHOR_HG_RE.match(user)
259 name = sanitize(m.group(1))
260 mail = sanitize(m.group(2))
263 name += ' ext:(' + urllib.quote(ex) + ')'
265 name = sanitize(user)
273 def fixup_user(user):
274 global mode, bad_mail
277 name, mail = fixup_user_git(user)
279 name, mail = fixup_user_hg(user)
286 return '%s <%s>' % (name, mail)
288 def get_repo(url, alias):
292 myui.setconfig('ui', 'interactive', 'off')
293 myui.fout = sys.stderr
296 if get_config('remote-hg.insecure') == 'true\n':
297 myui.setconfig('web', 'cacerts', '')
298 except subprocess.CalledProcessError:
302 repo = hg.repository(myui, url)
304 local_path = os.path.join(dirname, 'clone')
305 if not os.path.exists(local_path):
307 peer, dstpeer = hg.clone(myui, {}, url, local_path, update=True, pull=True)
309 die('Repository error')
310 repo = dstpeer.local()
312 repo = hg.repository(myui, local_path)
314 peer = hg.peer(myui, {}, url)
316 die('Repository error')
317 repo.pull(peer, heads=None, force=True)
321 def rev_to_mark(rev):
323 return marks.from_rev(rev)
325 def mark_to_rev(mark):
327 return marks.to_rev(mark)
329 def export_ref(repo, name, kind, head):
330 global prefix, marks, mode
332 ename = '%s/%s' % (kind, name)
333 tip = marks.get_tip(ename)
335 # mercurial takes too much time checking this
336 if tip and tip == head.rev():
339 revs = xrange(tip, head.rev() + 1)
342 revs = [rev for rev in revs if not marks.is_marked(rev)]
347 (manifest, user, (time, tz), files, desc, extra) = repo.changelog.read(c.node())
348 rev_branch = extra['branch']
350 author = "%s %d %s" % (fixup_user(user), time, gittz(tz))
351 if 'committer' in extra:
352 user, time, tz = extra['committer'].rsplit(' ', 2)
353 committer = "%s %s %s" % (user, time, gittz(int(tz)))
357 parents = [p for p in repo.changelog.parentrevs(rev) if p >= 0]
359 if len(parents) == 0:
360 modified = c.manifest().keys()
363 modified, removed = get_filechanges(repo, c, parents[0])
370 if rev_branch != 'default':
371 extra_msg += 'branch : %s\n' % rev_branch
375 if f not in c.manifest():
377 rename = c.filectx(f).renamed()
379 renames.append((rename[0], f))
382 extra_msg += "rename : %s => %s\n" % e
384 for key, value in extra.iteritems():
385 if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
388 extra_msg += "extra : %s : %s\n" % (key, urllib.quote(value))
391 desc += '\n--HG--\n' + extra_msg
393 if len(parents) == 0 and rev:
394 print 'reset %s/%s' % (prefix, ename)
396 print "commit %s/%s" % (prefix, ename)
397 print "mark :%d" % (marks.get_mark(rev))
398 print "author %s" % (author)
399 print "committer %s" % (committer)
400 print "data %d" % (len(desc))
404 print "from :%s" % (rev_to_mark(parents[0]))
406 print "merge :%s" % (rev_to_mark(parents[1]))
409 export_file(c.filectx(f))
411 print "D %s" % (fix_file_path(f))
415 if (count % 100 == 0):
416 print "progress revision %d '%s' (%d/%d)" % (rev, name, count, len(revs))
417 print "#############################################################"
419 # make sure the ref is updated
420 print "reset %s/%s" % (prefix, ename)
421 print "from :%u" % rev_to_mark(rev)
424 marks.set_tip(ename, rev)
426 def export_tag(repo, tag):
427 export_ref(repo, tag, 'tags', repo[tag])
429 def export_bookmark(repo, bmark):
431 export_ref(repo, bmark, 'bookmarks', head)
433 def export_branch(repo, branch):
434 tip = get_branch_tip(repo, branch)
436 export_ref(repo, branch, 'branches', head)
438 def export_head(repo):
440 export_ref(repo, g_head[0], 'bookmarks', g_head[1])
442 def do_capabilities(parser):
443 global prefix, dirname
447 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
448 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
449 print "refspec refs/tags/*:%s/tags/*" % prefix
451 path = os.path.join(dirname, 'marks-git')
453 if os.path.exists(path):
454 print "*import-marks %s" % path
455 print "*export-marks %s" % path
459 def get_branch_tip(repo, branch):
462 heads = branches.get(branch, None)
466 # verify there's only one head
468 warn("Branch '%s' has more than one head, consider merging" % branch)
469 # older versions of mercurial don't have this
470 if hasattr(repo, "branchtip"):
471 return repo.branchtip(branch)
475 def list_head(repo, cur):
476 global g_head, bmarks
478 head = bookmarks.readcurrent(repo)
482 # fake bookmark from current branch
489 if head == 'default':
493 print "@refs/heads/%s HEAD" % head
494 g_head = (head, node)
497 global branches, bmarks, mode, track_branches
500 for bmark, node in bookmarks.listbookmarks(repo).iteritems():
501 bmarks[bmark] = repo[node]
503 cur = repo.dirstate.branch()
508 for branch in repo.branchmap():
509 heads = repo.branchheads(branch)
511 branches[branch] = heads
513 for branch in branches:
514 print "? refs/heads/branches/%s" % branch
517 print "? refs/heads/%s" % bmark
519 for tag, node in repo.tagslist():
522 print "? refs/tags/%s" % tag
526 def do_import(parser):
529 path = os.path.join(dirname, 'marks-git')
532 if os.path.exists(path):
533 print "feature import-marks=%s" % path
534 print "feature export-marks=%s" % path
537 tmp = encoding.encoding
538 encoding.encoding = 'utf-8'
540 # lets get all the import lines
541 while parser.check('import'):
546 elif ref.startswith('refs/heads/branches/'):
547 branch = ref[len('refs/heads/branches/'):]
548 export_branch(repo, branch)
549 elif ref.startswith('refs/heads/'):
550 bmark = ref[len('refs/heads/'):]
551 export_bookmark(repo, bmark)
552 elif ref.startswith('refs/tags/'):
553 tag = ref[len('refs/tags/'):]
554 export_tag(repo, tag)
558 encoding.encoding = tmp
562 def parse_blob(parser):
566 mark = parser.get_mark()
568 data = parser.get_data()
569 blob_marks[mark] = data
572 def get_merge_files(repo, p1, p2, files):
573 for e in repo[p1].files():
575 if e not in repo[p1].manifest():
577 f = { 'ctx' : repo[p1][e] }
580 def parse_commit(parser):
581 global marks, blob_marks, parsed_refs
584 from_mark = merge_mark = None
589 commit_mark = parser.get_mark()
591 author = parser.get_author()
593 committer = parser.get_author()
595 data = parser.get_data()
597 if parser.check('from'):
598 from_mark = parser.get_mark()
600 if parser.check('merge'):
601 merge_mark = parser.get_mark()
603 if parser.check('merge'):
604 die('octopus merges are not supported yet')
609 if parser.check('M'):
610 t, m, mark_ref, path = line.split(' ', 3)
611 mark = int(mark_ref[1:])
612 f = { 'mode' : hgmode(m), 'data' : blob_marks[mark] }
613 elif parser.check('D'):
614 t, path = line.split(' ', 1)
615 f = { 'deleted' : True }
617 die('Unknown file command: %s' % line)
620 def getfilectx(repo, memctx, f):
626 is_exec = of['mode'] == 'x'
627 is_link = of['mode'] == 'l'
628 rename = of.get('rename', None)
629 return context.memfilectx(f, of['data'],
630 is_link, is_exec, rename)
634 user, date, tz = author
637 if committer != author:
638 extra['committer'] = "%s %u %u" % committer
641 p1 = repo.changelog.node(mark_to_rev(from_mark))
646 p2 = repo.changelog.node(mark_to_rev(merge_mark))
651 # If files changed from any of the parents, hg wants to know, but in git if
652 # nothing changed from the first parent, nothing changed.
655 get_merge_files(repo, p1, p2, files)
657 # Check if the ref is supposed to be a named branch
658 if ref.startswith('refs/heads/branches/'):
659 extra['branch'] = ref[len('refs/heads/branches/'):]
662 i = data.find('\n--HG--\n')
664 tmp = data[i + len('\n--HG--\n'):].strip()
665 for k, v in [e.split(' : ', 1) for e in tmp.split('\n')]:
667 old, new = v.split(' => ', 1)
668 files[new]['rename'] = old
672 ek, ev = v.split(' : ', 1)
673 extra[ek] = urllib.unquote(ev)
676 ctx = context.memctx(repo, (p1, p2), data,
677 files.keys(), getfilectx,
678 user, (date, tz), extra)
680 tmp = encoding.encoding
681 encoding.encoding = 'utf-8'
683 node = repo.commitctx(ctx)
685 encoding.encoding = tmp
687 rev = repo[node].rev()
689 parsed_refs[ref] = node
690 marks.new_mark(rev, commit_mark)
692 def parse_reset(parser):
698 if parser.check('commit'):
701 if not parser.check('from'):
703 from_mark = parser.get_mark()
706 node = parser.repo.changelog.node(mark_to_rev(from_mark))
707 parsed_refs[ref] = node
709 def parse_tag(parser):
712 from_mark = parser.get_mark()
714 tagger = parser.get_author()
716 data = parser.get_data()
721 def do_export(parser):
722 global parsed_refs, bmarks, peer
728 for line in parser.each_block('done'):
729 if parser.check('blob'):
731 elif parser.check('commit'):
733 elif parser.check('reset'):
735 elif parser.check('tag'):
737 elif parser.check('feature'):
740 die('unhandled export command: %s' % line)
742 for ref, node in parsed_refs.iteritems():
743 if ref.startswith('refs/heads/branches'):
745 elif ref.startswith('refs/heads/'):
746 bmark = ref[len('refs/heads/'):]
747 p_bmarks.append((bmark, node))
749 elif ref.startswith('refs/tags/'):
750 tag = ref[len('refs/tags/'):]
752 msg = 'Added tag %s for changeset %s' % (tag, hghex(node[:6]));
753 parser.repo.tag([tag], node, msg, False, None, {})
755 parser.repo.tag([tag], node, None, True, None, {})
758 # transport-helper/fast-export bugs
762 parser.repo.push(peer, force=force_push)
765 for bmark, node in p_bmarks:
766 ref = 'refs/heads/' + bmark
770 old = bmarks[bmark].hex()
774 if bmark == 'master' and 'master' not in parser.repo._bookmarks:
777 elif bookmarks.pushbookmark(parser.repo, bmark, old, new):
781 print "error %s" % ref
785 if not peer.pushkey('bookmarks', bmark, old, new):
786 print "error %s" % ref
793 def fix_path(alias, repo, orig_url):
794 repo_url = util.url(repo.url())
795 url = util.url(orig_url)
796 if str(url) == str(repo_url):
798 cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % repo_url]
802 global prefix, dirname, branches, bmarks
803 global marks, blob_marks, parsed_refs
804 global peer, mode, bad_mail, bad_name
805 global track_branches, force_push, is_tmp
811 hg_git_compat = False
812 track_branches = True
816 if get_config('remote-hg.hg-git-compat') == 'true\n':
818 track_branches = False
819 if get_config('remote-hg.track-branches') == 'false\n':
820 track_branches = False
821 if get_config('remote-hg.force-push') == 'false\n':
823 except subprocess.CalledProcessError:
828 bad_mail = 'none@none'
837 alias = util.sha1(alias).hexdigest()
841 gitdir = os.environ['GIT_DIR']
842 dirname = os.path.join(gitdir, 'hg', alias)
849 repo = get_repo(url, alias)
850 prefix = 'refs/hg/%s' % alias
853 fix_path(alias, peer or repo, url)
855 if not os.path.exists(dirname):
858 marks_path = os.path.join(dirname, 'marks-hg')
859 marks = Marks(marks_path)
861 parser = Parser(repo)
863 if parser.check('capabilities'):
864 do_capabilities(parser)
865 elif parser.check('list'):
867 elif parser.check('import'):
869 elif parser.check('export'):
872 die('unhandled command: %s' % line)
881 shutil.rmtree(dirname)
884 sys.exit(main(sys.argv))