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
, encoding
, node
, error
, extensions
, discovery
, util
25 import urlparse
, hashlib
28 # If you are not in hg-git-compat mode and want to disable the tracking of
30 # git config --global remote-hg.track-branches false
32 # If you don't want to force pushes (and thus risk creating new remote heads):
33 # git config --global remote-hg.force-push false
35 # If you want the equivalent of hg's clone/pull--insecure option:
36 # git config --global remote-hg.insecure true
38 # If you want to switch to hg-git compatibility mode:
39 # git config --global remote-hg.hg-git-compat 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+)')
61 sys
.stderr
.write('ERROR: %s\n' % (msg
% args
))
65 sys
.stderr
.write('WARNING: %s\n' % (msg
% args
))
68 return 'l' in flags
and '120000' or 'x' in flags
and '100755' or '100644'
71 return '%+03d%02d' % (-tz
/ 3600, -tz
% 3600 / 60)
74 m
= { '100755': 'x', '120000': 'l' }
75 return m
.get(mode
, '')
84 return ref
.replace('___', ' ')
87 return ref
.replace(' ', '___')
89 def check_version(*check
):
92 return hg_version
>= check
94 def get_config(config
):
95 cmd
= ['git', 'config', '--get', config
]
96 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
97 output
, _
= process
.communicate()
100 def get_config_bool(config
, default
=False):
101 value
= get_config(config
).rstrip('\n')
104 elif value
== "false":
111 def __init__(self
, path
, repo
):
117 if self
.version
< VERSION
:
118 if self
.version
== 1:
122 if self
.version
< VERSION
:
124 self
.version
= VERSION
134 if not os
.path
.exists(self
.path
):
137 tmp
= json
.load(open(self
.path
))
139 self
.tips
= tmp
['tips']
140 self
.marks
= tmp
['marks']
141 self
.last_mark
= tmp
['last-mark']
142 self
.version
= tmp
.get('version', 1)
144 for rev
, mark
in self
.marks
.iteritems():
145 self
.rev_marks
[mark
] = rev
147 def upgrade_one(self
):
149 return hghex(self
.repo
.changelog
.node(int(rev
)))
150 self
.tips
= dict((name
, get_id(rev
)) for name
, rev
in self
.tips
.iteritems())
151 self
.marks
= dict((get_id(rev
), mark
) for rev
, mark
in self
.marks
.iteritems())
152 self
.rev_marks
= dict((mark
, get_id(rev
)) for mark
, rev
in self
.rev_marks
.iteritems())
156 return { 'tips': self
.tips
, 'marks': self
.marks
, 'last-mark' : self
.last_mark
, 'version' : self
.version
}
159 json
.dump(self
.dict(), open(self
.path
, 'w'))
162 return str(self
.dict())
164 def from_rev(self
, rev
):
165 return self
.marks
[rev
]
167 def to_rev(self
, mark
):
168 return str(self
.rev_marks
[mark
])
172 return self
.last_mark
174 def get_mark(self
, rev
):
176 self
.marks
[rev
] = self
.last_mark
177 return self
.last_mark
179 def new_mark(self
, rev
, mark
):
180 self
.marks
[rev
] = mark
181 self
.rev_marks
[mark
] = rev
182 self
.last_mark
= mark
184 def is_marked(self
, rev
):
185 return rev
in self
.marks
187 def get_tip(self
, branch
):
188 return str(self
.tips
[branch
])
190 def set_tip(self
, branch
, tip
):
191 self
.tips
[branch
] = tip
195 def __init__(self
, repo
):
197 self
.line
= self
.get_line()
200 return sys
.stdin
.readline().strip()
202 def __getitem__(self
, i
):
203 return self
.line
.split()[i
]
205 def check(self
, word
):
206 return self
.line
.startswith(word
)
208 def each_block(self
, separator
):
209 while self
.line
!= separator
:
211 self
.line
= self
.get_line()
214 return self
.each_block('')
217 self
.line
= self
.get_line()
218 if self
.line
== 'done':
222 i
= self
.line
.index(':') + 1
223 return int(self
.line
[i
:])
226 if not self
.check('data'):
228 i
= self
.line
.index(' ') + 1
229 size
= int(self
.line
[i
:])
230 return sys
.stdin
.read(size
)
232 def get_author(self
):
236 m
= RAW_AUTHOR_RE
.match(self
.line
)
239 _
, name
, email
, date
, tz
= m
.groups()
240 if name
and 'ext:' in name
:
241 m
= re
.match('^(.+?) ext:\((.+)\)$', name
)
244 ex
= urllib
.unquote(m
.group(2))
246 if email
!= bad_mail
:
248 user
= '%s <%s>' % (name
, email
)
250 user
= '<%s>' % (email
)
258 tz
= ((tz
/ 100) * 3600) + ((tz
% 100) * 60)
259 return (user
, int(date
), -tz
)
261 def fix_file_path(path
):
262 if not os
.path
.isabs(path
):
264 return os
.path
.relpath(path
, '/')
266 def export_files(files
):
267 global marks
, filenodes
271 fid
= node
.hex(f
.filenode())
274 mark
= filenodes
[fid
]
276 mark
= marks
.next_mark()
277 filenodes
[fid
] = mark
281 print "mark :%u" % mark
282 print "data %d" % len(d
)
285 path
= fix_file_path(f
.path())
286 final
.append((gitmode(f
.flags()), mark
, path
))
290 def get_filechanges(repo
, ctx
, parent
):
295 # load earliest manifest first for caching reasons
296 prev
= parent
.manifest().copy()
301 if (cur
.flags(fn
) != prev
.flags(fn
) or cur
[fn
] != prev
[fn
]):
306 removed |
= set(prev
.keys())
308 return added | modified
, removed
310 def fixup_user_git(user
):
312 user
= user
.replace('"', '')
313 m
= AUTHOR_RE
.match(user
)
316 mail
= m
.group(2).strip()
318 m
= EMAIL_RE
.match(user
)
323 m
= NAME_RE
.match(user
)
325 name
= m
.group(1).strip()
328 def fixup_user_hg(user
):
330 # stole this from hg-git
331 return re
.sub('[<>\n]', '?', name
.lstrip('< ').rstrip('> '))
333 m
= AUTHOR_HG_RE
.match(user
)
335 name
= sanitize(m
.group(1))
336 mail
= sanitize(m
.group(2))
339 name
+= ' ext:(' + urllib
.quote(ex
) + ')'
341 name
= sanitize(user
)
349 def fixup_user(user
):
350 global mode
, bad_mail
353 name
, mail
= fixup_user_git(user
)
355 name
, mail
= fixup_user_hg(user
)
362 return '%s <%s>' % (name
, mail
)
364 def updatebookmarks(repo
, peer
):
365 remotemarks
= peer
.listkeys('bookmarks')
366 localmarks
= repo
._bookmarks
371 for k
, v
in remotemarks
.iteritems():
372 localmarks
[k
] = hgbin(v
)
374 if hasattr(localmarks
, 'write'):
377 bookmarks
.write(repo
)
379 def get_repo(url
, alias
):
383 myui
.setconfig('ui', 'interactive', 'off')
384 myui
.fout
= sys
.stderr
386 if get_config_bool('remote-hg.insecure'):
387 myui
.setconfig('web', 'cacerts', '')
389 extensions
.loadall(myui
)
391 if hg
.islocal(url
) and not os
.environ
.get('GIT_REMOTE_HG_TEST_REMOTE'):
392 repo
= hg
.repository(myui
, url
)
393 if not os
.path
.exists(dirname
):
396 shared_path
= os
.path
.join(gitdir
, 'hg')
397 if not os
.path
.exists(shared_path
):
399 hg
.clone(myui
, {}, url
, shared_path
, update
=False, pull
=True)
401 die('Repository error')
403 if not os
.path
.exists(dirname
):
406 local_path
= os
.path
.join(dirname
, 'clone')
407 if not os
.path
.exists(local_path
):
408 hg
.share(myui
, shared_path
, local_path
, update
=False)
410 repo
= hg
.repository(myui
, local_path
)
412 peer
= hg
.peer(myui
, {}, url
)
414 die('Repository error')
415 repo
.pull(peer
, heads
=None, force
=True)
417 updatebookmarks(repo
, peer
)
421 def rev_to_mark(rev
):
423 return marks
.from_rev(rev
.hex())
425 def mark_to_rev(mark
):
427 return marks
.to_rev(mark
)
429 def export_ref(repo
, name
, kind
, head
):
430 global prefix
, marks
, mode
432 ename
= '%s/%s' % (kind
, name
)
434 tip
= marks
.get_tip(ename
)
435 tip
= repo
[tip
].rev()
439 revs
= xrange(tip
, head
.rev() + 1)
447 if marks
.is_marked(c
.hex()):
450 (manifest
, user
, (time
, tz
), files
, desc
, extra
) = repo
.changelog
.read(node
)
451 rev_branch
= extra
['branch']
453 author
= "%s %d %s" % (fixup_user(user
), time
, gittz(tz
))
454 if 'committer' in extra
:
455 user
, time
, tz
= extra
['committer'].rsplit(' ', 2)
456 committer
= "%s %s %s" % (user
, time
, gittz(int(tz
)))
460 parents
= [repo
[p
] for p
in repo
.changelog
.parentrevs(rev
) if p
>= 0]
462 if len(parents
) == 0:
463 modified
= c
.manifest().keys()
466 modified
, removed
= get_filechanges(repo
, c
, parents
[0])
473 if rev_branch
!= 'default':
474 extra_msg
+= 'branch : %s\n' % rev_branch
478 if f
not in c
.manifest():
480 rename
= c
.filectx(f
).renamed()
482 renames
.append((rename
[0], f
))
485 extra_msg
+= "rename : %s => %s\n" % e
487 for key
, value
in extra
.iteritems():
488 if key
in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
491 extra_msg
+= "extra : %s : %s\n" % (key
, urllib
.quote(value
))
494 desc
+= '\n--HG--\n' + extra_msg
496 if len(parents
) == 0 and rev
:
497 print 'reset %s/%s' % (prefix
, ename
)
499 modified_final
= export_files(c
.filectx(f
) for f
in modified
)
501 print "commit %s/%s" % (prefix
, ename
)
502 print "mark :%d" % (marks
.get_mark(c
.hex()))
503 print "author %s" % (author
)
504 print "committer %s" % (committer
)
505 print "data %d" % (len(desc
))
509 print "from :%s" % (rev_to_mark(parents
[0]))
511 print "merge :%s" % (rev_to_mark(parents
[1]))
514 print "D %s" % (fix_file_path(f
))
515 for f
in modified_final
:
516 print "M %s :%u %s" % f
519 progress
= (rev
- tip
)
520 if (progress
% 100 == 0):
521 print "progress revision %d '%s' (%d/%d)" % (rev
, name
, progress
, total
)
523 # make sure the ref is updated
524 print "reset %s/%s" % (prefix
, ename
)
525 print "from :%u" % rev_to_mark(head
)
528 marks
.set_tip(ename
, head
.hex())
530 def export_tag(repo
, tag
):
531 export_ref(repo
, tag
, 'tags', repo
[hgref(tag
)])
533 def export_bookmark(repo
, bmark
):
534 head
= bmarks
[hgref(bmark
)]
535 export_ref(repo
, bmark
, 'bookmarks', head
)
537 def export_branch(repo
, branch
):
538 tip
= get_branch_tip(repo
, branch
)
540 export_ref(repo
, branch
, 'branches', head
)
542 def export_head(repo
):
544 export_ref(repo
, g_head
[0], 'bookmarks', g_head
[1])
546 def do_capabilities(parser
):
547 global prefix
, dirname
551 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
552 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
553 print "refspec refs/tags/*:%s/tags/*" % prefix
555 path
= os
.path
.join(dirname
, 'marks-git')
557 if os
.path
.exists(path
):
558 print "*import-marks %s" % path
559 print "*export-marks %s" % path
564 def branch_tip(branch
):
565 return branches
[branch
][-1]
567 def get_branch_tip(repo
, branch
):
570 heads
= branches
.get(hgref(branch
), None)
574 # verify there's only one head
576 warn("Branch '%s' has more than one head, consider merging" % branch
)
577 return branch_tip(hgref(branch
))
581 def list_head(repo
, cur
):
582 global g_head
, bmarks
, fake_bmark
584 if 'default' not in branches
:
588 node
= repo
[branch_tip('default')]
589 head
= 'master' if not 'master' in bmarks
else 'default'
594 print "@refs/heads/%s HEAD" % head
595 g_head
= (head
, node
)
598 global branches
, bmarks
, track_branches
601 for bmark
, node
in bookmarks
.listbookmarks(repo
).iteritems():
602 bmarks
[bmark
] = repo
[node
]
604 cur
= repo
.dirstate
.branch()
605 orig
= peer
if peer
else repo
607 for branch
, heads
in orig
.branchmap().iteritems():
609 heads
= [h
for h
in heads
if 'close' not in repo
.changelog
.read(h
)[5]]
611 branches
[branch
] = heads
616 for branch
in branches
:
617 print "? refs/heads/branches/%s" % gitref(branch
)
620 print "? refs/heads/%s" % gitref(bmark
)
622 for tag
, node
in repo
.tagslist():
625 print "? refs/tags/%s" % gitref(tag
)
629 def do_import(parser
):
632 path
= os
.path
.join(dirname
, 'marks-git')
635 if os
.path
.exists(path
):
636 print "feature import-marks=%s" % path
637 print "feature export-marks=%s" % path
638 print "feature force"
641 tmp
= encoding
.encoding
642 encoding
.encoding
= 'utf-8'
644 # lets get all the import lines
645 while parser
.check('import'):
650 elif ref
.startswith('refs/heads/branches/'):
651 branch
= ref
[len('refs/heads/branches/'):]
652 export_branch(repo
, branch
)
653 elif ref
.startswith('refs/heads/'):
654 bmark
= ref
[len('refs/heads/'):]
655 export_bookmark(repo
, bmark
)
656 elif ref
.startswith('refs/tags/'):
657 tag
= ref
[len('refs/tags/'):]
658 export_tag(repo
, tag
)
662 encoding
.encoding
= tmp
666 def parse_blob(parser
):
670 mark
= parser
.get_mark()
672 data
= parser
.get_data()
673 blob_marks
[mark
] = data
676 def get_merge_files(repo
, p1
, p2
, files
):
677 for e
in repo
[p1
].files():
679 if e
not in repo
[p1
].manifest():
681 f
= { 'ctx' : repo
[p1
][e
] }
684 def parse_commit(parser
):
685 global marks
, blob_marks
, parsed_refs
688 from_mark
= merge_mark
= None
693 commit_mark
= parser
.get_mark()
695 author
= parser
.get_author()
697 committer
= parser
.get_author()
699 data
= parser
.get_data()
701 if parser
.check('from'):
702 from_mark
= parser
.get_mark()
704 if parser
.check('merge'):
705 merge_mark
= parser
.get_mark()
707 if parser
.check('merge'):
708 die('octopus merges are not supported yet')
710 # fast-export adds an extra newline
717 if parser
.check('M'):
718 t
, m
, mark_ref
, path
= line
.split(' ', 3)
719 mark
= int(mark_ref
[1:])
720 f
= { 'mode' : hgmode(m
), 'data' : blob_marks
[mark
] }
721 elif parser
.check('D'):
722 t
, path
= line
.split(' ', 1)
723 f
= { 'deleted' : True }
725 die('Unknown file command: %s' % line
)
728 # only export the commits if we are on an internal proxy repo
729 if dry_run
and not peer
:
730 parsed_refs
[ref
] = None
733 def getfilectx(repo
, memctx
, f
):
739 is_exec
= of
['mode'] == 'x'
740 is_link
= of
['mode'] == 'l'
741 rename
= of
.get('rename', None)
742 return context
.memfilectx(f
, of
['data'],
743 is_link
, is_exec
, rename
)
747 user
, date
, tz
= author
750 if committer
!= author
:
751 extra
['committer'] = "%s %u %u" % committer
754 p1
= mark_to_rev(from_mark
)
759 p2
= mark_to_rev(merge_mark
)
764 # If files changed from any of the parents, hg wants to know, but in git if
765 # nothing changed from the first parent, nothing changed.
768 get_merge_files(repo
, p1
, p2
, files
)
770 # Check if the ref is supposed to be a named branch
771 if ref
.startswith('refs/heads/branches/'):
772 branch
= ref
[len('refs/heads/branches/'):]
773 extra
['branch'] = hgref(branch
)
776 i
= data
.find('\n--HG--\n')
778 tmp
= data
[i
+ len('\n--HG--\n'):].strip()
779 for k
, v
in [e
.split(' : ', 1) for e
in tmp
.split('\n')]:
781 old
, new
= v
.split(' => ', 1)
782 files
[new
]['rename'] = old
786 ek
, ev
= v
.split(' : ', 1)
787 extra
[ek
] = urllib
.unquote(ev
)
790 ctx
= context
.memctx(repo
, (p1
, p2
), data
,
791 files
.keys(), getfilectx
,
792 user
, (date
, tz
), extra
)
794 tmp
= encoding
.encoding
795 encoding
.encoding
= 'utf-8'
797 node
= hghex(repo
.commitctx(ctx
))
799 encoding
.encoding
= tmp
801 parsed_refs
[ref
] = node
802 marks
.new_mark(node
, commit_mark
)
804 def parse_reset(parser
):
810 if parser
.check('commit'):
813 if not parser
.check('from'):
815 from_mark
= parser
.get_mark()
819 rev
= mark_to_rev(from_mark
)
822 parsed_refs
[ref
] = rev
824 def parse_tag(parser
):
827 from_mark
= parser
.get_mark()
829 tagger
= parser
.get_author()
831 data
= parser
.get_data()
834 parsed_tags
[name
] = (tagger
, data
)
836 def write_tag(repo
, tag
, node
, msg
, author
):
837 branch
= repo
[node
].branch()
838 tip
= branch_tip(branch
)
841 def getfilectx(repo
, memctx
, f
):
843 fctx
= tip
.filectx(f
)
845 except error
.ManifestLookupError
:
847 content
= data
+ "%s %s\n" % (node
, tag
)
848 return context
.memfilectx(f
, content
, False, False, None)
853 user
, date
, tz
= author
856 cmd
= ['git', 'var', 'GIT_COMMITTER_IDENT']
857 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
858 output
, _
= process
.communicate()
859 m
= re
.match('^.* <.*>', output
)
863 user
= repo
.ui
.username()
866 ctx
= context
.memctx(repo
, (p1
, p2
), msg
,
867 ['.hgtags'], getfilectx
,
868 user
, date_tz
, {'branch' : branch
})
870 tmp
= encoding
.encoding
871 encoding
.encoding
= 'utf-8'
873 tagnode
= repo
.commitctx(ctx
)
875 encoding
.encoding
= tmp
877 return (tagnode
, branch
)
879 def checkheads_bmark(repo
, ref
, ctx
):
883 bmark
= ref
[len('refs/heads/'):]
884 if not bmark
in bmarks
:
888 ctx_old
= bmarks
[bmark
]
890 if not repo
.changelog
.descendant(ctx_old
.rev(), ctx_new
.rev()):
891 print "error %s non-fast forward" % ref
896 def checkheads(repo
, remote
, p_revs
):
898 remotemap
= remote
.branchmap()
906 for node
, ref
in p_revs
.iteritems():
908 branch
= ctx
.branch()
909 if not branch
in remotemap
:
912 if not ref
.startswith('refs/heads/branches'):
913 if ref
.startswith('refs/heads/'):
914 if not checkheads_bmark(repo
, ref
, ctx
):
917 # only check branches
919 new
.setdefault(branch
, []).append(ctx
.rev())
921 for branch
, heads
in new
.iteritems():
922 old
= [repo
.changelog
.rev(x
) for x
in remotemap
[branch
]]
924 if check_version(2, 3):
925 ancestors
= repo
.changelog
.ancestors([rev
], stoprev
=min(old
))
927 ancestors
= repo
.changelog
.ancestors(rev
)
938 node
= repo
.changelog
.node(rev
)
939 print "error %s non-fast forward" % p_revs
[node
]
944 def push_unsafe(repo
, remote
, parsed_refs
, p_revs
):
948 fci
= discovery
.findcommonincoming
949 commoninc
= fci(repo
, remote
, force
=force
)
950 common
, _
, remoteheads
= commoninc
952 if not force
and not checkheads(repo
, remote
, p_revs
):
955 cg
= repo
.getbundle('push', heads
=list(p_revs
), common
=common
)
957 unbundle
= remote
.capable('unbundle')
960 remoteheads
= ['force']
961 return remote
.unbundle(cg
, remoteheads
, 'push')
963 return remote
.addchangegroup(cg
, 'push', repo
.url())
965 def push(repo
, remote
, parsed_refs
, p_revs
):
966 if hasattr(remote
, 'canpush') and not remote
.canpush():
967 print "error cannot push"
974 unbundle
= remote
.capable('unbundle')
978 ret
= push_unsafe(repo
, remote
, parsed_refs
, p_revs
)
985 def check_tip(ref
, kind
, name
, heads
):
987 ename
= '%s/%s' % (kind
, name
)
988 tip
= marks
.get_tip(ename
)
994 def do_export(parser
):
995 global parsed_refs
, bmarks
, peer
1002 for line
in parser
.each_block('done'):
1003 if parser
.check('blob'):
1005 elif parser
.check('commit'):
1006 parse_commit(parser
)
1007 elif parser
.check('reset'):
1009 elif parser
.check('tag'):
1011 elif parser
.check('feature'):
1014 die('unhandled export command: %s' % line
)
1018 for ref
, node
in parsed_refs
.iteritems():
1019 bnode
= hgbin(node
) if node
else None
1020 if ref
.startswith('refs/heads/branches'):
1021 branch
= ref
[len('refs/heads/branches/'):]
1022 if branch
in branches
and bnode
in branches
[branch
]:
1027 remotemap
= peer
.branchmap()
1028 if remotemap
and branch
in remotemap
:
1029 heads
= [hghex(e
) for e
in remotemap
[branch
]]
1030 if not check_tip(ref
, 'branches', branch
, heads
):
1031 print "error %s fetch first" % ref
1037 elif ref
.startswith('refs/heads/'):
1038 bmark
= ref
[len('refs/heads/'):]
1040 old
= bmarks
[bmark
].hex() if bmark
in bmarks
else ''
1046 if bmark
!= fake_bmark
and \
1047 not (bmark
== 'master' and bmark
not in parser
.repo
._bookmarks
):
1048 p_bmarks
.append((ref
, bmark
, old
, new
))
1051 remote_old
= peer
.listkeys('bookmarks').get(bmark
)
1053 if not check_tip(ref
, 'bookmarks', bmark
, remote_old
):
1054 print "error %s fetch first" % ref
1059 elif ref
.startswith('refs/tags/'):
1063 tag
= ref
[len('refs/tags/'):]
1065 author
, msg
= parsed_tags
.get(tag
, (None, None))
1068 msg
= 'Added tag %s for changeset %s' % (tag
, node
[:12]);
1069 tagnode
, branch
= write_tag(parser
.repo
, tag
, node
, msg
, author
)
1070 p_revs
[tagnode
] = 'refs/heads/branches/' + gitref(branch
)
1072 fp
= parser
.repo
.opener('localtags', 'a')
1073 fp
.write('%s %s\n' % (node
, tag
))
1078 # transport-helper/fast-export bugs
1086 if peer
and not force_push
:
1087 checkheads(parser
.repo
, peer
, p_revs
)
1092 if not push(parser
.repo
, peer
, parsed_refs
, p_revs
):
1093 # do not update bookmarks
1097 # update remote bookmarks
1098 remote_bmarks
= peer
.listkeys('bookmarks')
1099 for ref
, bmark
, old
, new
in p_bmarks
:
1101 old
= remote_bmarks
.get(bmark
, '')
1102 if not peer
.pushkey('bookmarks', bmark
, old
, new
):
1103 print "error %s" % ref
1105 # update local bookmarks
1106 for ref
, bmark
, old
, new
in p_bmarks
:
1107 if not bookmarks
.pushbookmark(parser
.repo
, bmark
, old
, new
):
1108 print "error %s" % ref
1112 def do_option(parser
):
1114 _
, key
, value
= parser
.line
.split(' ')
1115 if key
== 'dry-run':
1116 dry_run
= (value
== 'true')
1121 def fix_path(alias
, repo
, orig_url
):
1122 url
= urlparse
.urlparse(orig_url
, 'file')
1123 if url
.scheme
!= 'file' or os
.path
.isabs(url
.path
):
1125 abs_url
= urlparse
.urljoin("%s/" % os
.getcwd(), orig_url
)
1126 cmd
= ['git', 'config', 'remote.%s.url' % alias
, "hg::%s" % abs_url
]
1127 subprocess
.call(cmd
)
1130 global prefix
, gitdir
, dirname
, branches
, bmarks
1131 global marks
, blob_marks
, parsed_refs
1132 global peer
, mode
, bad_mail
, bad_name
1133 global track_branches
, force_push
, is_tmp
1136 global fake_bmark
, hg_version
1143 hg_git_compat
= get_config_bool('remote-hg.hg-git-compat')
1144 track_branches
= get_config_bool('remote-hg.track-branches', True)
1145 force_push
= get_config_bool('remote-hg.force-push')
1149 bad_mail
= 'none@none'
1153 bad_mail
= 'unknown'
1154 bad_name
= 'Unknown'
1156 if alias
[4:] == url
:
1158 alias
= hashlib
.sha1(alias
).hexdigest()
1162 gitdir
= os
.environ
['GIT_DIR']
1163 dirname
= os
.path
.join(gitdir
, 'hg', alias
)
1173 hg_version
= tuple(int(e
) for e
in util
.version().split('.'))
1178 repo
= get_repo(url
, alias
)
1179 prefix
= 'refs/hg/%s' % alias
1182 fix_path(alias
, peer
or repo
, url
)
1184 marks_path
= os
.path
.join(dirname
, 'marks-hg')
1185 marks
= Marks(marks_path
, repo
)
1187 if sys
.platform
== 'win32':
1189 msvcrt
.setmode(sys
.stdout
.fileno(), os
.O_BINARY
)
1191 parser
= Parser(repo
)
1193 if parser
.check('capabilities'):
1194 do_capabilities(parser
)
1195 elif parser
.check('list'):
1197 elif parser
.check('import'):
1199 elif parser
.check('export'):
1201 elif parser
.check('option'):
1204 die('unhandled command: %s' % line
)
1213 shutil
.rmtree(dirname
)
1215 atexit
.register(bye
)
1216 sys
.exit(main(sys
.argv
))