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 from mercurial import hg, ui, bookmarks, context, util, encoding, node, error
23 # If you want to switch to hg-git compatibility mode:
24 # git config --global remote-hg.hg-git-compat true
26 # If you are not in hg-git-compat mode and want to disable the tracking of
28 # git config --global remote-hg.track-branches false
30 # If you don't want to force pushes (and thus risk creating new remote heads):
31 # git config --global remote-hg.force-push false
34 # Sensible defaults for git.
35 # hg bookmarks are exported as git branches, hg branches are prefixed
36 # with 'branches/', HEAD is a special case.
40 # Only hg bookmarks are exported as git branches.
41 # Commits are modified to preserve hg information and allow bidirectionality.
44 NAME_RE = re.compile('^([^<>]+)')
45 AUTHOR_RE = re.compile('^([^<>]+?)? ?<([^<>]*)>$')
46 AUTHOR_HG_RE = re.compile('^(.*?) ?<(.*?)(?:>(.+)?)?$')
47 RAW_AUTHOR_RE = re.compile('^(\w+) (?:(.+)? )?<(.*)> (\d+) ([+-]\d+)')
50 sys.stderr.write('ERROR: %s\n' % (msg % args))
54 sys.stderr.write('WARNING: %s\n' % (msg % args))
57 return 'l' in flags and '120000' or 'x' in flags and '100755' or '100644'
60 return '%+03d%02d' % (-tz / 3600, -tz % 3600 / 60)
63 m = { '100755': 'x', '120000': 'l' }
64 return m.get(mode, '')
67 return hg.node.hex(node)
69 def get_config(config):
70 cmd = ['git', 'config', '--get', config]
71 process = subprocess.Popen(cmd, stdout=subprocess.PIPE)
72 output, _ = process.communicate()
77 def __init__(self, path):
87 if not os.path.exists(self.path):
90 tmp = json.load(open(self.path))
92 self.tips = tmp['tips']
93 self.marks = tmp['marks']
94 self.last_mark = tmp['last-mark']
96 for rev, mark in self.marks.iteritems():
97 self.rev_marks[mark] = int(rev)
100 return { 'tips': self.tips, 'marks': self.marks, 'last-mark' : self.last_mark }
103 json.dump(self.dict(), open(self.path, 'w'))
106 return str(self.dict())
108 def from_rev(self, rev):
109 return self.marks[str(rev)]
111 def to_rev(self, mark):
112 return self.rev_marks[mark]
114 def get_mark(self, rev):
116 self.marks[str(rev)] = self.last_mark
117 return self.last_mark
119 def new_mark(self, rev, mark):
120 self.marks[str(rev)] = mark
121 self.rev_marks[mark] = rev
122 self.last_mark = mark
124 def is_marked(self, rev):
125 return self.marks.has_key(str(rev))
127 def get_tip(self, branch):
128 return self.tips.get(branch, 0)
130 def set_tip(self, branch, tip):
131 self.tips[branch] = tip
135 def __init__(self, repo):
137 self.line = self.get_line()
140 return sys.stdin.readline().strip()
142 def __getitem__(self, i):
143 return self.line.split()[i]
145 def check(self, word):
146 return self.line.startswith(word)
148 def each_block(self, separator):
149 while self.line != separator:
151 self.line = self.get_line()
154 return self.each_block('')
157 self.line = self.get_line()
158 if self.line == 'done':
162 i = self.line.index(':') + 1
163 return int(self.line[i:])
166 if not self.check('data'):
168 i = self.line.index(' ') + 1
169 size = int(self.line[i:])
170 return sys.stdin.read(size)
172 def get_author(self):
176 m = RAW_AUTHOR_RE.match(self.line)
179 _, name, email, date, tz = m.groups()
180 if name and 'ext:' in name:
181 m = re.match('^(.+?) ext:\((.+)\)$', name)
184 ex = urllib.unquote(m.group(2))
186 if email != bad_mail:
188 user = '%s <%s>' % (name, email)
190 user = '<%s>' % (email)
198 tz = ((tz / 100) * 3600) + ((tz % 100) * 60)
199 return (user, int(date), -tz)
203 print "M %s inline %s" % (gitmode(fc.flags()), fc.path())
204 print "data %d" % len(d)
207 def get_filechanges(repo, ctx, parent):
213 prev = repo[parent].manifest().copy()
217 if (cur.flags(fn) != prev.flags(fn) or cur[fn] != prev[fn]):
222 removed |= set(prev.keys())
224 return added | modified, removed
226 def fixup_user_git(user):
228 user = user.replace('"', '')
229 m = AUTHOR_RE.match(user)
232 mail = m.group(2).strip()
234 m = NAME_RE.match(user)
236 name = m.group(1).strip()
239 def fixup_user_hg(user):
241 # stole this from hg-git
242 return re.sub('[<>\n]', '?', name.lstrip('< ').rstrip('> '))
244 m = AUTHOR_HG_RE.match(user)
246 name = sanitize(m.group(1))
247 mail = sanitize(m.group(2))
250 name += ' ext:(' + urllib.quote(ex) + ')'
252 name = sanitize(user)
260 def fixup_user(user):
261 global mode, bad_mail
264 name, mail = fixup_user_git(user)
266 name, mail = fixup_user_hg(user)
273 return '%s <%s>' % (name, mail)
275 def get_repo(url, alias):
279 myui.setconfig('ui', 'interactive', 'off')
280 myui.fout = sys.stderr
283 repo = hg.repository(myui, url)
285 local_path = os.path.join(dirname, 'clone')
286 if not os.path.exists(local_path):
288 peer, dstpeer = hg.clone(myui, {}, url, local_path, update=True, pull=True)
290 die('Repository error')
291 repo = dstpeer.local()
293 repo = hg.repository(myui, local_path)
295 peer = hg.peer(myui, {}, url)
297 die('Repository error')
298 repo.pull(peer, heads=None, force=True)
302 def rev_to_mark(rev):
304 return marks.from_rev(rev)
306 def mark_to_rev(mark):
308 return marks.to_rev(mark)
310 def export_ref(repo, name, kind, head):
311 global prefix, marks, mode
313 ename = '%s/%s' % (kind, name)
314 tip = marks.get_tip(ename)
316 # mercurial takes too much time checking this
317 if tip and tip == head.rev():
320 revs = xrange(tip, head.rev() + 1)
323 revs = [rev for rev in revs if not marks.is_marked(rev)]
328 (manifest, user, (time, tz), files, desc, extra) = repo.changelog.read(c.node())
329 rev_branch = extra['branch']
331 author = "%s %d %s" % (fixup_user(user), time, gittz(tz))
332 if 'committer' in extra:
333 user, time, tz = extra['committer'].rsplit(' ', 2)
334 committer = "%s %s %s" % (user, time, gittz(int(tz)))
338 parents = [p for p in repo.changelog.parentrevs(rev) if p >= 0]
340 if len(parents) == 0:
341 modified = c.manifest().keys()
344 modified, removed = get_filechanges(repo, c, parents[0])
349 if rev_branch != 'default':
350 extra_msg += 'branch : %s\n' % rev_branch
354 if f not in c.manifest():
356 rename = c.filectx(f).renamed()
358 renames.append((rename[0], f))
361 extra_msg += "rename : %s => %s\n" % e
363 for key, value in extra.iteritems():
364 if key in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
367 extra_msg += "extra : %s : %s\n" % (key, urllib.quote(value))
371 desc += '\n--HG--\n' + extra_msg
373 if len(parents) == 0 and rev:
374 print 'reset %s/%s' % (prefix, ename)
376 print "commit %s/%s" % (prefix, ename)
377 print "mark :%d" % (marks.get_mark(rev))
378 print "author %s" % (author)
379 print "committer %s" % (committer)
380 print "data %d" % (len(desc))
384 print "from :%s" % (rev_to_mark(parents[0]))
386 print "merge :%s" % (rev_to_mark(parents[1]))
389 export_file(c.filectx(f))
395 if (count % 100 == 0):
396 print "progress revision %d '%s' (%d/%d)" % (rev, name, count, len(revs))
397 print "#############################################################"
399 # make sure the ref is updated
400 print "reset %s/%s" % (prefix, ename)
401 print "from :%u" % rev_to_mark(rev)
404 marks.set_tip(ename, rev)
406 def export_tag(repo, tag):
407 export_ref(repo, tag, 'tags', repo[tag])
409 def export_bookmark(repo, bmark):
411 export_ref(repo, bmark, 'bookmarks', head)
413 def export_branch(repo, branch):
414 tip = get_branch_tip(repo, branch)
416 export_ref(repo, branch, 'branches', head)
418 def export_head(repo):
420 export_ref(repo, g_head[0], 'bookmarks', g_head[1])
422 def do_capabilities(parser):
423 global prefix, dirname
427 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
428 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
429 print "refspec refs/tags/*:%s/tags/*" % prefix
431 path = os.path.join(dirname, 'marks-git')
433 if os.path.exists(path):
434 print "*import-marks %s" % path
435 print "*export-marks %s" % path
439 def get_branch_tip(repo, branch):
442 heads = branches.get(branch, None)
446 # verify there's only one head
448 warn("Branch '%s' has more than one head, consider merging" % branch)
449 # older versions of mercurial don't have this
450 if hasattr(repo, "branchtip"):
451 return repo.branchtip(branch)
455 def list_head(repo, cur):
456 global g_head, bmarks
458 head = bookmarks.readcurrent(repo)
462 # fake bookmark from current branch
469 if head == 'default':
473 print "@refs/heads/%s HEAD" % head
474 g_head = (head, node)
477 global branches, bmarks, mode, track_branches
480 for bmark, node in bookmarks.listbookmarks(repo).iteritems():
481 bmarks[bmark] = repo[node]
483 cur = repo.dirstate.branch()
488 for branch in repo.branchmap():
489 heads = repo.branchheads(branch)
491 branches[branch] = heads
493 for branch in branches:
494 print "? refs/heads/branches/%s" % branch
497 print "? refs/heads/%s" % bmark
499 for tag, node in repo.tagslist():
502 print "? refs/tags/%s" % tag
506 def do_import(parser):
509 path = os.path.join(dirname, 'marks-git')
512 if os.path.exists(path):
513 print "feature import-marks=%s" % path
514 print "feature export-marks=%s" % path
517 tmp = encoding.encoding
518 encoding.encoding = 'utf-8'
520 # lets get all the import lines
521 while parser.check('import'):
526 elif ref.startswith('refs/heads/branches/'):
527 branch = ref[len('refs/heads/branches/'):]
528 export_branch(repo, branch)
529 elif ref.startswith('refs/heads/'):
530 bmark = ref[len('refs/heads/'):]
531 export_bookmark(repo, bmark)
532 elif ref.startswith('refs/tags/'):
533 tag = ref[len('refs/tags/'):]
534 export_tag(repo, tag)
538 encoding.encoding = tmp
542 def parse_blob(parser):
546 mark = parser.get_mark()
548 data = parser.get_data()
549 blob_marks[mark] = data
552 def get_merge_files(repo, p1, p2, files):
553 for e in repo[p1].files():
555 if e not in repo[p1].manifest():
557 f = { 'ctx' : repo[p1][e] }
560 def parse_commit(parser):
561 global marks, blob_marks, parsed_refs
564 from_mark = merge_mark = None
569 commit_mark = parser.get_mark()
571 author = parser.get_author()
573 committer = parser.get_author()
575 data = parser.get_data()
577 if parser.check('from'):
578 from_mark = parser.get_mark()
580 if parser.check('merge'):
581 merge_mark = parser.get_mark()
583 if parser.check('merge'):
584 die('octopus merges are not supported yet')
589 if parser.check('M'):
590 t, m, mark_ref, path = line.split(' ', 3)
591 mark = int(mark_ref[1:])
592 f = { 'mode' : hgmode(m), 'data' : blob_marks[mark] }
593 elif parser.check('D'):
594 t, path = line.split(' ', 1)
595 f = { 'deleted' : True }
597 die('Unknown file command: %s' % line)
600 def getfilectx(repo, memctx, f):
606 is_exec = of['mode'] == 'x'
607 is_link = of['mode'] == 'l'
608 rename = of.get('rename', None)
609 return context.memfilectx(f, of['data'],
610 is_link, is_exec, rename)
614 user, date, tz = author
617 if committer != author:
618 extra['committer'] = "%s %u %u" % committer
621 p1 = repo.changelog.node(mark_to_rev(from_mark))
626 p2 = repo.changelog.node(mark_to_rev(merge_mark))
631 # If files changed from any of the parents, hg wants to know, but in git if
632 # nothing changed from the first parent, nothing changed.
635 get_merge_files(repo, p1, p2, files)
637 # Check if the ref is supposed to be a named branch
638 if ref.startswith('refs/heads/branches/'):
639 extra['branch'] = ref[len('refs/heads/branches/'):]
642 i = data.find('\n--HG--\n')
644 tmp = data[i + len('\n--HG--\n'):].strip()
645 for k, v in [e.split(' : ', 1) for e in tmp.split('\n')]:
647 old, new = v.split(' => ', 1)
648 files[new]['rename'] = old
652 ek, ev = v.split(' : ', 1)
653 extra[ek] = urllib.unquote(ev)
656 ctx = context.memctx(repo, (p1, p2), data,
657 files.keys(), getfilectx,
658 user, (date, tz), extra)
660 tmp = encoding.encoding
661 encoding.encoding = 'utf-8'
663 node = repo.commitctx(ctx)
665 encoding.encoding = tmp
667 rev = repo[node].rev()
669 parsed_refs[ref] = node
670 marks.new_mark(rev, commit_mark)
672 def parse_reset(parser):
678 if parser.check('commit'):
681 if not parser.check('from'):
683 from_mark = parser.get_mark()
686 node = parser.repo.changelog.node(mark_to_rev(from_mark))
687 parsed_refs[ref] = node
689 def parse_tag(parser):
692 from_mark = parser.get_mark()
694 tagger = parser.get_author()
696 data = parser.get_data()
701 def do_export(parser):
702 global parsed_refs, bmarks, peer
708 for line in parser.each_block('done'):
709 if parser.check('blob'):
711 elif parser.check('commit'):
713 elif parser.check('reset'):
715 elif parser.check('tag'):
717 elif parser.check('feature'):
720 die('unhandled export command: %s' % line)
722 for ref, node in parsed_refs.iteritems():
723 if ref.startswith('refs/heads/branches'):
725 elif ref.startswith('refs/heads/'):
726 bmark = ref[len('refs/heads/'):]
727 p_bmarks.append((bmark, node))
729 elif ref.startswith('refs/tags/'):
730 tag = ref[len('refs/tags/'):]
732 msg = 'Added tag %s for changeset %s' % (tag, hghex(node[:6]));
733 parser.repo.tag([tag], node, msg, False, None, {})
735 parser.repo.tag([tag], node, None, True, None, {})
738 # transport-helper/fast-export bugs
742 parser.repo.push(peer, force=force_push)
745 for bmark, node in p_bmarks:
746 ref = 'refs/heads/' + bmark
750 old = bmarks[bmark].hex()
754 if bmark == 'master' and 'master' not in parser.repo._bookmarks:
757 elif bookmarks.pushbookmark(parser.repo, bmark, old, new):
761 print "error %s" % ref
765 if not peer.pushkey('bookmarks', bmark, old, new):
766 print "error %s" % ref
773 def fix_path(alias, repo, orig_url):
774 repo_url = util.url(repo.url())
775 url = util.url(orig_url)
776 if str(url) == str(repo_url):
778 cmd = ['git', 'config', 'remote.%s.url' % alias, "hg::%s" % repo_url]
782 global prefix, dirname, branches, bmarks
783 global marks, blob_marks, parsed_refs
784 global peer, mode, bad_mail, bad_name
785 global track_branches, force_push
791 hg_git_compat = False
792 track_branches = True
796 if get_config('remote-hg.hg-git-compat') == 'true\n':
798 track_branches = False
799 if get_config('remote-hg.track-branches') == 'false\n':
800 track_branches = False
801 if get_config('remote-hg.force-push') == 'false\n':
803 except subprocess.CalledProcessError:
808 bad_mail = 'none@none'
817 alias = util.sha1(alias).hexdigest()
821 gitdir = os.environ['GIT_DIR']
822 dirname = os.path.join(gitdir, 'hg', alias)
828 repo = get_repo(url, alias)
829 prefix = 'refs/hg/%s' % alias
832 fix_path(alias, peer or repo, url)
834 if not os.path.exists(dirname):
837 marks_path = os.path.join(dirname, 'marks-hg')
838 marks = Marks(marks_path)
840 parser = Parser(repo)
842 if parser.check('capabilities'):
843 do_capabilities(parser)
844 elif parser.check('list'):
846 elif parser.check('import'):
848 elif parser.check('export'):
851 die('unhandled command: %s' % line)
857 shutil.rmtree(dirname)
859 sys.exit(main(sys.argv))