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
23 # If you want to switch to hg-git compatibility mode:
24 # git config --global remote-hg.hg-git-compat true
27 # Sensible defaults for git.
28 # hg bookmarks are exported as git branches, hg branches are prefixed
29 # with 'branches/', HEAD is a special case.
33 # Only hg bookmarks are exported as git branches.
34 # Commits are modified to preserve hg information and allow biridectionality.
37 NAME_RE
= re
.compile('^([^<>]+)')
38 AUTHOR_RE
= re
.compile('^([^<>]+?)? ?<([^<>]*)>$')
39 AUTHOR_HG_RE
= re
.compile('^(.*?) ?<(.*?)(?:>(.+)?)?$')
40 RAW_AUTHOR_RE
= re
.compile('^(\w+) (?:(.+)? )?<(.*)> (\d+) ([+-]\d+)')
43 sys
.stderr
.write('ERROR: %s\n' % (msg
% args
))
47 sys
.stderr
.write('WARNING: %s\n' % (msg
% args
))
50 return 'l' in flags
and '120000' or 'x' in flags
and '100755' or '100644'
53 return '%+03d%02d' % (-tz
/ 3600, -tz
% 3600 / 60)
56 m
= { '0100755': 'x', '0120000': 'l' }
57 return m
.get(mode
, '')
61 def __init__(self
, path
):
71 if not os
.path
.exists(self
.path
):
74 tmp
= json
.load(open(self
.path
))
76 self
.tips
= tmp
['tips']
77 self
.marks
= tmp
['marks']
78 self
.last_mark
= tmp
['last-mark']
80 for rev
, mark
in self
.marks
.iteritems():
81 self
.rev_marks
[mark
] = int(rev
)
84 return { 'tips': self
.tips
, 'marks': self
.marks
, 'last-mark' : self
.last_mark
}
87 json
.dump(self
.dict(), open(self
.path
, 'w'))
90 return str(self
.dict())
92 def from_rev(self
, rev
):
93 return self
.marks
[str(rev
)]
95 def to_rev(self
, mark
):
96 return self
.rev_marks
[mark
]
98 def get_mark(self
, rev
):
100 self
.marks
[str(rev
)] = self
.last_mark
101 return self
.last_mark
103 def new_mark(self
, rev
, mark
):
104 self
.marks
[str(rev
)] = mark
105 self
.rev_marks
[mark
] = rev
106 self
.last_mark
= mark
108 def is_marked(self
, rev
):
109 return self
.marks
.has_key(str(rev
))
111 def get_tip(self
, branch
):
112 return self
.tips
.get(branch
, 0)
114 def set_tip(self
, branch
, tip
):
115 self
.tips
[branch
] = tip
119 def __init__(self
, repo
):
121 self
.line
= self
.get_line()
124 return sys
.stdin
.readline().strip()
126 def __getitem__(self
, i
):
127 return self
.line
.split()[i
]
129 def check(self
, word
):
130 return self
.line
.startswith(word
)
132 def each_block(self
, separator
):
133 while self
.line
!= separator
:
135 self
.line
= self
.get_line()
138 return self
.each_block('')
141 self
.line
= self
.get_line()
142 if self
.line
== 'done':
146 i
= self
.line
.index(':') + 1
147 return int(self
.line
[i
:])
150 if not self
.check('data'):
152 i
= self
.line
.index(' ') + 1
153 size
= int(self
.line
[i
:])
154 return sys
.stdin
.read(size
)
156 def get_author(self
):
160 m
= RAW_AUTHOR_RE
.match(self
.line
)
163 _
, name
, email
, date
, tz
= m
.groups()
164 if name
and 'ext:' in name
:
165 m
= re
.match('^(.+?) ext:\((.+)\)$', name
)
168 ex
= urllib
.unquote(m
.group(2))
170 if email
!= bad_mail
:
172 user
= '%s <%s>' % (name
, email
)
174 user
= '<%s>' % (email
)
182 tz
= ((tz
/ 100) * 3600) + ((tz
% 100) * 60)
183 return (user
, int(date
), -tz
)
187 print "M %s inline %s" % (gitmode(fc
.flags()), fc
.path())
188 print "data %d" % len(d
)
191 def get_filechanges(repo
, ctx
, parent
):
197 prev
= repo
[parent
].manifest().copy()
201 if (cur
.flags(fn
) != prev
.flags(fn
) or cur
[fn
] != prev
[fn
]):
206 removed |
= set(prev
.keys())
208 return added | modified
, removed
210 def fixup_user_git(user
):
212 user
= user
.replace('"', '')
213 m
= AUTHOR_RE
.match(user
)
216 mail
= m
.group(2).strip()
218 m
= NAME_RE
.match(user
)
220 name
= m
.group(1).strip()
223 def fixup_user_hg(user
):
225 # stole this from hg-git
226 return re
.sub('[<>\n]', '?', name
.lstrip('< ').rstrip('> '))
228 m
= AUTHOR_HG_RE
.match(user
)
230 name
= sanitize(m
.group(1))
231 mail
= sanitize(m
.group(2))
234 name
+= ' ext:(' + urllib
.quote(ex
) + ')'
236 name
= sanitize(user
)
244 def fixup_user(user
):
245 global mode
, bad_mail
248 name
, mail
= fixup_user_git(user
)
250 name
, mail
= fixup_user_hg(user
)
257 return '%s <%s>' % (name
, mail
)
259 def get_repo(url
, alias
):
263 myui
.setconfig('ui', 'interactive', 'off')
266 repo
= hg
.repository(myui
, url
)
268 local_path
= os
.path
.join(dirname
, 'clone')
269 if not os
.path
.exists(local_path
):
270 peer
, dstpeer
= hg
.clone(myui
, {}, url
, local_path
, update
=False, pull
=True)
271 repo
= dstpeer
.local()
273 repo
= hg
.repository(myui
, local_path
)
274 peer
= hg
.peer(myui
, {}, url
)
275 repo
.pull(peer
, heads
=None, force
=True)
279 def rev_to_mark(rev
):
281 return marks
.from_rev(rev
)
283 def mark_to_rev(mark
):
285 return marks
.to_rev(mark
)
287 def export_ref(repo
, name
, kind
, head
):
288 global prefix
, marks
, mode
290 ename
= '%s/%s' % (kind
, name
)
291 tip
= marks
.get_tip(ename
)
293 # mercurial takes too much time checking this
294 if tip
and tip
== head
.rev():
297 revs
= repo
.revs('%u:%u' % (tip
, head
))
300 revs
= [rev
for rev
in revs
if not marks
.is_marked(rev
)]
305 (manifest
, user
, (time
, tz
), files
, desc
, extra
) = repo
.changelog
.read(c
.node())
306 rev_branch
= extra
['branch']
308 author
= "%s %d %s" % (fixup_user(user
), time
, gittz(tz
))
309 if 'committer' in extra
:
310 user
, time
, tz
= extra
['committer'].rsplit(' ', 2)
311 committer
= "%s %s %s" % (user
, time
, gittz(int(tz
)))
315 parents
= [p
for p
in repo
.changelog
.parentrevs(rev
) if p
>= 0]
317 if len(parents
) == 0:
318 modified
= c
.manifest().keys()
321 modified
, removed
= get_filechanges(repo
, c
, parents
[0])
326 if rev_branch
!= 'default':
327 extra_msg
+= 'branch : %s\n' % rev_branch
331 if f
not in c
.manifest():
333 rename
= c
.filectx(f
).renamed()
335 renames
.append((rename
[0], f
))
338 extra_msg
+= "rename : %s => %s\n" % e
340 for key
, value
in extra
.iteritems():
341 if key
in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
344 extra_msg
+= "extra : %s : %s\n" % (key
, urllib
.quote(value
))
348 desc
+= '\n--HG--\n' + extra_msg
350 if len(parents
) == 0 and rev
:
351 print 'reset %s/%s' % (prefix
, ename
)
353 print "commit %s/%s" % (prefix
, ename
)
354 print "mark :%d" % (marks
.get_mark(rev
))
355 print "author %s" % (author
)
356 print "committer %s" % (committer
)
357 print "data %d" % (len(desc
))
361 print "from :%s" % (rev_to_mark(parents
[0]))
363 print "merge :%s" % (rev_to_mark(parents
[1]))
366 export_file(c
.filectx(f
))
372 if (count
% 100 == 0):
373 print "progress revision %d '%s' (%d/%d)" % (rev
, name
, count
, len(revs
))
374 print "#############################################################"
376 # make sure the ref is updated
377 print "reset %s/%s" % (prefix
, ename
)
378 print "from :%u" % rev_to_mark(rev
)
381 marks
.set_tip(ename
, rev
)
383 def export_tag(repo
, tag
):
384 export_ref(repo
, tag
, 'tags', repo
[tag
])
386 def export_bookmark(repo
, bmark
):
388 export_ref(repo
, bmark
, 'bookmarks', head
)
390 def export_branch(repo
, branch
):
391 tip
= get_branch_tip(repo
, branch
)
393 export_ref(repo
, branch
, 'branches', head
)
395 def export_head(repo
):
397 export_ref(repo
, g_head
[0], 'bookmarks', g_head
[1])
399 def do_capabilities(parser
):
400 global prefix
, dirname
404 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
405 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
406 print "refspec refs/tags/*:%s/tags/*" % prefix
408 path
= os
.path
.join(dirname
, 'marks-git')
410 if os
.path
.exists(path
):
411 print "*import-marks %s" % path
412 print "*export-marks %s" % path
416 def get_branch_tip(repo
, branch
):
419 heads
= branches
.get(branch
, None)
423 # verify there's only one head
425 warn("Branch '%s' has more than one head, consider merging" % branch
)
426 # older versions of mercurial don't have this
427 if hasattr(repo
, "branchtip"):
428 return repo
.branchtip(branch
)
432 def list_head(repo
, cur
):
433 global g_head
, bmarks
435 head
= bookmarks
.readcurrent(repo
)
439 # fake bookmark from current branch
444 if head
== 'default':
448 print "@refs/heads/%s HEAD" % head
449 g_head
= (head
, node
)
452 global branches
, bmarks
, mode
, track_branches
455 for bmark
, node
in bookmarks
.listbookmarks(repo
).iteritems():
456 bmarks
[bmark
] = repo
[node
]
458 cur
= repo
.dirstate
.branch()
463 for branch
in repo
.branchmap():
464 heads
= repo
.branchheads(branch
)
466 branches
[branch
] = heads
468 for branch
in branches
:
469 print "? refs/heads/branches/%s" % branch
472 print "? refs/heads/%s" % bmark
474 for tag
, node
in repo
.tagslist():
477 print "? refs/tags/%s" % tag
481 def do_import(parser
):
484 path
= os
.path
.join(dirname
, 'marks-git')
487 if os
.path
.exists(path
):
488 print "feature import-marks=%s" % path
489 print "feature export-marks=%s" % path
492 tmp
= encoding
.encoding
493 encoding
.encoding
= 'utf-8'
495 # lets get all the import lines
496 while parser
.check('import'):
501 elif ref
.startswith('refs/heads/branches/'):
502 branch
= ref
[len('refs/heads/branches/'):]
503 export_branch(repo
, branch
)
504 elif ref
.startswith('refs/heads/'):
505 bmark
= ref
[len('refs/heads/'):]
506 export_bookmark(repo
, bmark
)
507 elif ref
.startswith('refs/tags/'):
508 tag
= ref
[len('refs/tags/'):]
509 export_tag(repo
, tag
)
513 encoding
.encoding
= tmp
517 def parse_blob(parser
):
521 mark
= parser
.get_mark()
523 data
= parser
.get_data()
524 blob_marks
[mark
] = data
528 def get_merge_files(repo
, p1
, p2
, files
):
529 for e
in repo
[p1
].files():
531 if e
not in repo
[p1
].manifest():
533 f
= { 'ctx' : repo
[p1
][e
] }
536 def parse_commit(parser
):
537 global marks
, blob_marks
, bmarks
, parsed_refs
540 from_mark
= merge_mark
= None
545 commit_mark
= parser
.get_mark()
547 author
= parser
.get_author()
549 committer
= parser
.get_author()
551 data
= parser
.get_data()
553 if parser
.check('from'):
554 from_mark
= parser
.get_mark()
556 if parser
.check('merge'):
557 merge_mark
= parser
.get_mark()
559 if parser
.check('merge'):
560 die('octopus merges are not supported yet')
565 if parser
.check('M'):
566 t
, m
, mark_ref
, path
= line
.split(' ')
567 mark
= int(mark_ref
[1:])
568 f
= { 'mode' : hgmode(m
), 'data' : blob_marks
[mark
] }
569 elif parser
.check('D'):
570 t
, path
= line
.split(' ')
571 f
= { 'deleted' : True }
573 die('Unknown file command: %s' % line
)
576 def getfilectx(repo
, memctx
, f
):
582 is_exec
= of
['mode'] == 'x'
583 is_link
= of
['mode'] == 'l'
584 rename
= of
.get('rename', None)
585 return context
.memfilectx(f
, of
['data'],
586 is_link
, is_exec
, rename
)
590 user
, date
, tz
= author
593 if committer
!= author
:
594 extra
['committer'] = "%s %u %u" % committer
597 p1
= repo
.changelog
.node(mark_to_rev(from_mark
))
602 p2
= repo
.changelog
.node(mark_to_rev(merge_mark
))
607 # If files changed from any of the parents, hg wants to know, but in git if
608 # nothing changed from the first parent, nothing changed.
611 get_merge_files(repo
, p1
, p2
, files
)
614 i
= data
.find('\n--HG--\n')
616 tmp
= data
[i
+ len('\n--HG--\n'):].strip()
617 for k
, v
in [e
.split(' : ') for e
in tmp
.split('\n')]:
619 old
, new
= v
.split(' => ', 1)
620 files
[new
]['rename'] = old
624 ek
, ev
= v
.split(' : ', 1)
625 extra
[ek
] = urllib
.unquote(ev
)
628 ctx
= context
.memctx(repo
, (p1
, p2
), data
,
629 files
.keys(), getfilectx
,
630 user
, (date
, tz
), extra
)
632 tmp
= encoding
.encoding
633 encoding
.encoding
= 'utf-8'
635 node
= repo
.commitctx(ctx
)
637 encoding
.encoding
= tmp
639 rev
= repo
[node
].rev()
641 parsed_refs
[ref
] = node
643 marks
.new_mark(rev
, commit_mark
)
645 def parse_reset(parser
):
649 if parser
.check('commit'):
652 if not parser
.check('from'):
654 from_mark
= parser
.get_mark()
657 node
= parser
.repo
.changelog
.node(mark_to_rev(from_mark
))
658 parsed_refs
[ref
] = node
660 def parse_tag(parser
):
663 from_mark
= parser
.get_mark()
665 tagger
= parser
.get_author()
667 data
= parser
.get_data()
672 def do_export(parser
):
673 global parsed_refs
, bmarks
, peer
677 for line
in parser
.each_block('done'):
678 if parser
.check('blob'):
680 elif parser
.check('commit'):
682 elif parser
.check('reset'):
684 elif parser
.check('tag'):
686 elif parser
.check('feature'):
689 die('unhandled export command: %s' % line
)
691 for ref
, node
in parsed_refs
.iteritems():
692 if ref
.startswith('refs/heads/branches'):
694 elif ref
.startswith('refs/heads/'):
695 bmark
= ref
[len('refs/heads/'):]
697 old
= bmarks
[bmark
].hex()
700 if not bookmarks
.pushbookmark(parser
.repo
, bmark
, old
, node
):
702 elif ref
.startswith('refs/tags/'):
703 tag
= ref
[len('refs/tags/'):]
704 parser
.repo
.tag([tag
], node
, None, True, None, {})
710 parser
.repo
.push(peer
, force
=False)
713 global prefix
, dirname
, branches
, bmarks
714 global marks
, blob_marks
, parsed_refs
715 global peer
, mode
, bad_mail
, bad_name
716 global track_branches
722 hg_git_compat
= False
723 track_branches
= True
725 cmd
= ['git', 'config', '--get', 'remote-hg.hg-git-compat']
726 if subprocess
.check_output(cmd
) == 'true\n':
728 track_branches
= False
729 cmd
= ['git', 'config', '--get', 'remote-hg.track-branches']
730 if subprocess
.check_output(cmd
) == 'false\n':
731 track_branches
= False
732 except subprocess
.CalledProcessError
:
737 bad_mail
= 'none@none'
746 alias
= util
.sha1(alias
).hexdigest()
750 gitdir
= os
.environ
['GIT_DIR']
751 dirname
= os
.path
.join(gitdir
, 'hg', alias
)
757 repo
= get_repo(url
, alias
)
758 prefix
= 'refs/hg/%s' % alias
760 if not os
.path
.exists(dirname
):
763 marks_path
= os
.path
.join(dirname
, 'marks-hg')
764 marks
= Marks(marks_path
)
766 parser
= Parser(repo
)
768 if parser
.check('capabilities'):
769 do_capabilities(parser
)
770 elif parser
.check('list'):
772 elif parser
.check('import'):
774 elif parser
.check('export'):
777 die('unhandled command: %s' % line
)
783 shutil
.rmtree(dirname
)
785 sys
.exit(main(sys
.argv
))