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 want the equivalent of hg's clone/pull--insecure option:
33 # git config --global remote-hg.insecure true
35 # If you want to switch to hg-git compatibility mode:
36 # git config --global remote-hg.hg-git-compat true
39 # Sensible defaults for git.
40 # hg bookmarks are exported as git branches, hg branches are prefixed
41 # with 'branches/', HEAD is a special case.
45 # Only hg bookmarks are exported as git branches.
46 # Commits are modified to preserve hg information and allow bidirectionality.
49 NAME_RE
= re
.compile('^([^<>]+)')
50 AUTHOR_RE
= re
.compile('^([^<>]+?)? ?<([^<>]*)>$')
51 EMAIL_RE
= re
.compile('^([^<>]+[^ \\\t<>])?\\b(?:[ \\t<>]*?)\\b([^ \\t<>]+@[^ \\t<>]+)')
52 AUTHOR_HG_RE
= re
.compile('^(.*?) ?<(.*?)(?:>(.+)?)?$')
53 RAW_AUTHOR_RE
= re
.compile('^(\w+) (?:(.+)? )?<(.*)> (\d+) ([+-]\d+)')
58 sys
.stderr
.write('ERROR: %s\n' % (msg
% args
))
62 sys
.stderr
.write('WARNING: %s\n' % (msg
% args
))
65 return 'l' in flags
and '120000' or 'x' in flags
and '100755' or '100644'
68 return '%+03d%02d' % (-tz
/ 3600, -tz
% 3600 / 60)
71 m
= { '100755': 'x', '120000': 'l' }
72 return m
.get(mode
, '')
81 return ref
.replace('___', ' ')
84 return ref
.replace(' ', '___')
86 def check_version(*check
):
89 return hg_version
>= check
91 def get_config(config
):
92 cmd
= ['git', 'config', '--get', config
]
93 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
94 output
, _
= process
.communicate()
97 def get_config_bool(config
, default
=False):
98 value
= get_config(config
).rstrip('\n')
101 elif value
== "false":
108 def __init__(self
, path
, repo
):
114 if self
.version
< VERSION
:
115 if self
.version
== 1:
119 if self
.version
< VERSION
:
121 self
.version
= VERSION
131 if not os
.path
.exists(self
.path
):
134 tmp
= json
.load(open(self
.path
))
136 self
.tips
= tmp
['tips']
137 self
.marks
= tmp
['marks']
138 self
.last_mark
= tmp
['last-mark']
139 self
.version
= tmp
.get('version', 1)
141 for rev
, mark
in self
.marks
.iteritems():
142 self
.rev_marks
[mark
] = rev
144 def upgrade_one(self
):
146 return hghex(self
.repo
.changelog
.node(int(rev
)))
147 self
.tips
= dict((name
, get_id(rev
)) for name
, rev
in self
.tips
.iteritems())
148 self
.marks
= dict((get_id(rev
), mark
) for rev
, mark
in self
.marks
.iteritems())
149 self
.rev_marks
= dict((mark
, get_id(rev
)) for mark
, rev
in self
.rev_marks
.iteritems())
153 return { 'tips': self
.tips
, 'marks': self
.marks
, 'last-mark' : self
.last_mark
, 'version' : self
.version
}
156 json
.dump(self
.dict(), open(self
.path
, 'w'))
159 return str(self
.dict())
161 def from_rev(self
, rev
):
162 return self
.marks
[rev
]
164 def to_rev(self
, mark
):
165 return str(self
.rev_marks
[mark
])
169 return self
.last_mark
171 def get_mark(self
, rev
):
173 self
.marks
[rev
] = self
.last_mark
174 return self
.last_mark
176 def new_mark(self
, rev
, mark
):
177 self
.marks
[rev
] = mark
178 self
.rev_marks
[mark
] = rev
179 self
.last_mark
= mark
181 def is_marked(self
, rev
):
182 return rev
in self
.marks
184 def get_tip(self
, branch
):
185 return str(self
.tips
[branch
])
187 def set_tip(self
, branch
, tip
):
188 self
.tips
[branch
] = tip
192 def __init__(self
, repo
):
194 self
.line
= self
.get_line()
197 return sys
.stdin
.readline().strip()
199 def __getitem__(self
, i
):
200 return self
.line
.split()[i
]
202 def check(self
, word
):
203 return self
.line
.startswith(word
)
205 def each_block(self
, separator
):
206 while self
.line
!= separator
:
208 self
.line
= self
.get_line()
211 return self
.each_block('')
214 self
.line
= self
.get_line()
215 if self
.line
== 'done':
219 i
= self
.line
.index(':') + 1
220 return int(self
.line
[i
:])
223 if not self
.check('data'):
225 i
= self
.line
.index(' ') + 1
226 size
= int(self
.line
[i
:])
227 return sys
.stdin
.read(size
)
229 def get_author(self
):
233 m
= RAW_AUTHOR_RE
.match(self
.line
)
236 _
, name
, email
, date
, tz
= m
.groups()
237 if name
and 'ext:' in name
:
238 m
= re
.match('^(.+?) ext:\((.+)\)$', name
)
241 ex
= urllib
.unquote(m
.group(2))
243 if email
!= bad_mail
:
245 user
= '%s <%s>' % (name
, email
)
247 user
= '<%s>' % (email
)
255 tz
= ((tz
/ 100) * 3600) + ((tz
% 100) * 60)
256 return (user
, int(date
), -tz
)
258 def fix_file_path(path
):
259 if not os
.path
.isabs(path
):
261 return os
.path
.relpath(path
, '/')
263 def export_files(files
):
264 global marks
, filenodes
268 fid
= node
.hex(f
.filenode())
271 mark
= filenodes
[fid
]
273 mark
= marks
.next_mark()
274 filenodes
[fid
] = mark
278 print "mark :%u" % mark
279 print "data %d" % len(d
)
282 path
= fix_file_path(f
.path())
283 final
.append((gitmode(f
.flags()), mark
, path
))
287 def get_filechanges(repo
, ctx
, parent
):
292 # load earliest manifest first for caching reasons
293 prev
= parent
.manifest().copy()
298 if (cur
.flags(fn
) != prev
.flags(fn
) or cur
[fn
] != prev
[fn
]):
303 removed |
= set(prev
.keys())
305 return added | modified
, removed
307 def fixup_user_git(user
):
309 user
= user
.replace('"', '')
310 m
= AUTHOR_RE
.match(user
)
313 mail
= m
.group(2).strip()
315 m
= EMAIL_RE
.match(user
)
320 m
= NAME_RE
.match(user
)
322 name
= m
.group(1).strip()
325 def fixup_user_hg(user
):
327 # stole this from hg-git
328 return re
.sub('[<>\n]', '?', name
.lstrip('< ').rstrip('> '))
330 m
= AUTHOR_HG_RE
.match(user
)
332 name
= sanitize(m
.group(1))
333 mail
= sanitize(m
.group(2))
336 name
+= ' ext:(' + urllib
.quote(ex
) + ')'
338 name
= sanitize(user
)
346 def fixup_user(user
):
347 global mode
, bad_mail
350 name
, mail
= fixup_user_git(user
)
352 name
, mail
= fixup_user_hg(user
)
359 return '%s <%s>' % (name
, mail
)
361 def updatebookmarks(repo
, peer
):
362 remotemarks
= peer
.listkeys('bookmarks')
363 localmarks
= repo
._bookmarks
368 for k
, v
in remotemarks
.iteritems():
369 localmarks
[k
] = hgbin(v
)
371 if hasattr(localmarks
, 'write'):
374 bookmarks
.write(repo
)
376 def get_repo(url
, alias
):
380 myui
.setconfig('ui', 'interactive', 'off')
381 myui
.fout
= sys
.stderr
383 if get_config_bool('remote-hg.insecure'):
384 myui
.setconfig('web', 'cacerts', '')
386 extensions
.loadall(myui
)
388 if hg
.islocal(url
) and not os
.environ
.get('GIT_REMOTE_HG_TEST_REMOTE'):
389 repo
= hg
.repository(myui
, url
)
390 if not os
.path
.exists(dirname
):
393 shared_path
= os
.path
.join(gitdir
, 'hg')
394 if not os
.path
.exists(shared_path
):
396 hg
.clone(myui
, {}, url
, shared_path
, update
=False, pull
=True)
398 die('Repository error')
400 if not os
.path
.exists(dirname
):
403 local_path
= os
.path
.join(dirname
, 'clone')
404 if not os
.path
.exists(local_path
):
405 hg
.share(myui
, shared_path
, local_path
, update
=False)
407 repo
= hg
.repository(myui
, local_path
)
409 peer
= hg
.peer(myui
, {}, url
)
411 die('Repository error')
412 repo
.pull(peer
, heads
=None, force
=True)
414 updatebookmarks(repo
, peer
)
418 def rev_to_mark(rev
):
420 return marks
.from_rev(rev
.hex())
422 def mark_to_rev(mark
):
424 return marks
.to_rev(mark
)
426 def export_ref(repo
, name
, kind
, head
):
427 global prefix
, marks
, mode
429 ename
= '%s/%s' % (kind
, name
)
431 tip
= marks
.get_tip(ename
)
432 tip
= repo
[tip
].rev()
436 revs
= xrange(tip
, head
.rev() + 1)
444 if marks
.is_marked(c
.hex()):
447 (manifest
, user
, (time
, tz
), files
, desc
, extra
) = repo
.changelog
.read(node
)
448 rev_branch
= extra
['branch']
450 author
= "%s %d %s" % (fixup_user(user
), time
, gittz(tz
))
451 if 'committer' in extra
:
452 user
, time
, tz
= extra
['committer'].rsplit(' ', 2)
453 committer
= "%s %s %s" % (user
, time
, gittz(int(tz
)))
457 parents
= [repo
[p
] for p
in repo
.changelog
.parentrevs(rev
) if p
>= 0]
459 if len(parents
) == 0:
460 modified
= c
.manifest().keys()
463 modified
, removed
= get_filechanges(repo
, c
, parents
[0])
470 if rev_branch
!= 'default':
471 extra_msg
+= 'branch : %s\n' % rev_branch
475 if f
not in c
.manifest():
477 rename
= c
.filectx(f
).renamed()
479 renames
.append((rename
[0], f
))
482 extra_msg
+= "rename : %s => %s\n" % e
484 for key
, value
in extra
.iteritems():
485 if key
in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
488 extra_msg
+= "extra : %s : %s\n" % (key
, urllib
.quote(value
))
491 desc
+= '\n--HG--\n' + extra_msg
493 if len(parents
) == 0 and rev
:
494 print 'reset %s/%s' % (prefix
, ename
)
496 modified_final
= export_files(c
.filectx(f
) for f
in modified
)
498 print "commit %s/%s" % (prefix
, ename
)
499 print "mark :%d" % (marks
.get_mark(c
.hex()))
500 print "author %s" % (author
)
501 print "committer %s" % (committer
)
502 print "data %d" % (len(desc
))
506 print "from :%s" % (rev_to_mark(parents
[0]))
508 print "merge :%s" % (rev_to_mark(parents
[1]))
511 print "D %s" % (fix_file_path(f
))
512 for f
in modified_final
:
513 print "M %s :%u %s" % f
516 progress
= (rev
- tip
)
517 if (progress
% 100 == 0):
518 print "progress revision %d '%s' (%d/%d)" % (rev
, name
, progress
, total
)
520 # make sure the ref is updated
521 print "reset %s/%s" % (prefix
, ename
)
522 print "from :%u" % rev_to_mark(head
)
525 marks
.set_tip(ename
, head
.hex())
527 def export_tag(repo
, tag
):
528 export_ref(repo
, tag
, 'tags', repo
[hgref(tag
)])
530 def export_bookmark(repo
, bmark
):
531 head
= bmarks
[hgref(bmark
)]
532 export_ref(repo
, bmark
, 'bookmarks', head
)
534 def export_branch(repo
, branch
):
535 tip
= get_branch_tip(repo
, branch
)
537 export_ref(repo
, branch
, 'branches', head
)
539 def export_head(repo
):
541 export_ref(repo
, g_head
[0], 'bookmarks', g_head
[1])
543 def do_capabilities(parser
):
544 global prefix
, dirname
548 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
549 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
550 print "refspec refs/tags/*:%s/tags/*" % prefix
552 path
= os
.path
.join(dirname
, 'marks-git')
554 if os
.path
.exists(path
):
555 print "*import-marks %s" % path
556 print "*export-marks %s" % path
561 def branch_tip(branch
):
562 return branches
[branch
][-1]
564 def get_branch_tip(repo
, branch
):
567 heads
= branches
.get(hgref(branch
), None)
571 # verify there's only one head
573 warn("Branch '%s' has more than one head, consider merging" % branch
)
574 return branch_tip(hgref(branch
))
578 def list_head(repo
, cur
):
579 global g_head
, bmarks
, fake_bmark
581 if 'default' not in branches
:
585 node
= repo
[branch_tip('default')]
586 head
= 'master' if not 'master' in bmarks
else 'default'
591 print "@refs/heads/%s HEAD" % head
592 g_head
= (head
, node
)
595 global branches
, bmarks
, track_branches
598 for bmark
, node
in bookmarks
.listbookmarks(repo
).iteritems():
599 bmarks
[bmark
] = repo
[node
]
601 cur
= repo
.dirstate
.branch()
602 orig
= peer
if peer
else repo
604 for branch
, heads
in orig
.branchmap().iteritems():
606 heads
= [h
for h
in heads
if 'close' not in repo
.changelog
.read(h
)[5]]
608 branches
[branch
] = heads
613 for branch
in branches
:
614 print "? refs/heads/branches/%s" % gitref(branch
)
617 print "? refs/heads/%s" % gitref(bmark
)
619 for tag
, node
in repo
.tagslist():
622 print "? refs/tags/%s" % gitref(tag
)
626 def do_import(parser
):
629 path
= os
.path
.join(dirname
, 'marks-git')
632 if os
.path
.exists(path
):
633 print "feature import-marks=%s" % path
634 print "feature export-marks=%s" % path
635 print "feature force"
638 tmp
= encoding
.encoding
639 encoding
.encoding
= 'utf-8'
641 # lets get all the import lines
642 while parser
.check('import'):
647 elif ref
.startswith('refs/heads/branches/'):
648 branch
= ref
[len('refs/heads/branches/'):]
649 export_branch(repo
, branch
)
650 elif ref
.startswith('refs/heads/'):
651 bmark
= ref
[len('refs/heads/'):]
652 export_bookmark(repo
, bmark
)
653 elif ref
.startswith('refs/tags/'):
654 tag
= ref
[len('refs/tags/'):]
655 export_tag(repo
, tag
)
659 encoding
.encoding
= tmp
663 def parse_blob(parser
):
667 mark
= parser
.get_mark()
669 data
= parser
.get_data()
670 blob_marks
[mark
] = data
673 def get_merge_files(repo
, p1
, p2
, files
):
674 for e
in repo
[p1
].files():
676 if e
not in repo
[p1
].manifest():
678 f
= { 'ctx' : repo
[p1
][e
] }
681 def c_style_unescape(string
):
682 if string
[0] == string
[-1] == '"':
683 return string
.decode('string-escape')[1:-1]
686 def parse_commit(parser
):
687 global marks
, blob_marks
, parsed_refs
690 from_mark
= merge_mark
= None
695 commit_mark
= parser
.get_mark()
697 author
= parser
.get_author()
699 committer
= parser
.get_author()
701 data
= parser
.get_data()
703 if parser
.check('from'):
704 from_mark
= parser
.get_mark()
706 if parser
.check('merge'):
707 merge_mark
= parser
.get_mark()
709 if parser
.check('merge'):
710 die('octopus merges are not supported yet')
712 # fast-export adds an extra newline
719 if parser
.check('M'):
720 t
, m
, mark_ref
, path
= line
.split(' ', 3)
721 mark
= int(mark_ref
[1:])
722 f
= { 'mode' : hgmode(m
), 'data' : blob_marks
[mark
] }
723 elif parser
.check('D'):
724 t
, path
= line
.split(' ', 1)
725 f
= { 'deleted' : True }
727 die('Unknown file command: %s' % line
)
728 path
= c_style_unescape(path
)
731 # only export the commits if we are on an internal proxy repo
732 if dry_run
and not peer
:
733 parsed_refs
[ref
] = None
736 def getfilectx(repo
, memctx
, f
):
742 is_exec
= of
['mode'] == 'x'
743 is_link
= of
['mode'] == 'l'
744 rename
= of
.get('rename', None)
745 return context
.memfilectx(f
, of
['data'],
746 is_link
, is_exec
, rename
)
750 user
, date
, tz
= author
753 if committer
!= author
:
754 extra
['committer'] = "%s %u %u" % committer
757 p1
= mark_to_rev(from_mark
)
762 p2
= mark_to_rev(merge_mark
)
767 # If files changed from any of the parents, hg wants to know, but in git if
768 # nothing changed from the first parent, nothing changed.
771 get_merge_files(repo
, p1
, p2
, files
)
773 # Check if the ref is supposed to be a named branch
774 if ref
.startswith('refs/heads/branches/'):
775 branch
= ref
[len('refs/heads/branches/'):]
776 extra
['branch'] = hgref(branch
)
779 i
= data
.find('\n--HG--\n')
781 tmp
= data
[i
+ len('\n--HG--\n'):].strip()
782 for k
, v
in [e
.split(' : ', 1) for e
in tmp
.split('\n')]:
784 old
, new
= v
.split(' => ', 1)
785 files
[new
]['rename'] = old
789 ek
, ev
= v
.split(' : ', 1)
790 extra
[ek
] = urllib
.unquote(ev
)
793 ctx
= context
.memctx(repo
, (p1
, p2
), data
,
794 files
.keys(), getfilectx
,
795 user
, (date
, tz
), extra
)
797 tmp
= encoding
.encoding
798 encoding
.encoding
= 'utf-8'
800 node
= hghex(repo
.commitctx(ctx
))
802 encoding
.encoding
= tmp
804 parsed_refs
[ref
] = node
805 marks
.new_mark(node
, commit_mark
)
807 def parse_reset(parser
):
813 if parser
.check('commit'):
816 if not parser
.check('from'):
818 from_mark
= parser
.get_mark()
822 rev
= mark_to_rev(from_mark
)
825 parsed_refs
[ref
] = rev
827 def parse_tag(parser
):
830 from_mark
= parser
.get_mark()
832 tagger
= parser
.get_author()
834 data
= parser
.get_data()
837 parsed_tags
[name
] = (tagger
, data
)
839 def write_tag(repo
, tag
, node
, msg
, author
):
840 branch
= repo
[node
].branch()
841 tip
= branch_tip(branch
)
844 def getfilectx(repo
, memctx
, f
):
846 fctx
= tip
.filectx(f
)
848 except error
.ManifestLookupError
:
850 content
= data
+ "%s %s\n" % (node
, tag
)
851 return context
.memfilectx(f
, content
, False, False, None)
856 user
, date
, tz
= author
859 cmd
= ['git', 'var', 'GIT_COMMITTER_IDENT']
860 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
861 output
, _
= process
.communicate()
862 m
= re
.match('^.* <.*>', output
)
866 user
= repo
.ui
.username()
869 ctx
= context
.memctx(repo
, (p1
, p2
), msg
,
870 ['.hgtags'], getfilectx
,
871 user
, date_tz
, {'branch' : branch
})
873 tmp
= encoding
.encoding
874 encoding
.encoding
= 'utf-8'
876 tagnode
= repo
.commitctx(ctx
)
878 encoding
.encoding
= tmp
880 return (tagnode
, branch
)
882 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()):
892 print "ok %s forced update" % ref
894 print "error %s non-fast forward" % ref
899 def checkheads(repo
, remote
, p_revs
):
901 remotemap
= remote
.branchmap()
909 for node
, ref
in p_revs
.iteritems():
911 branch
= ctx
.branch()
912 if not branch
in remotemap
:
915 if not ref
.startswith('refs/heads/branches'):
916 if ref
.startswith('refs/heads/'):
917 if not checkheads_bmark(repo
, ref
, ctx
):
920 # only check branches
922 new
.setdefault(branch
, []).append(ctx
.rev())
924 for branch
, heads
in new
.iteritems():
925 old
= [repo
.changelog
.rev(x
) for x
in remotemap
[branch
]]
927 if check_version(2, 3):
928 ancestors
= repo
.changelog
.ancestors([rev
], stoprev
=min(old
))
930 ancestors
= repo
.changelog
.ancestors(rev
)
941 node
= repo
.changelog
.node(rev
)
944 print "ok %s forced update" % ref
946 print "error %s non-fast forward" % ref
951 def push_unsafe(repo
, remote
, parsed_refs
, p_revs
):
955 fci
= discovery
.findcommonincoming
956 commoninc
= fci(repo
, remote
, force
=force
)
957 common
, _
, remoteheads
= commoninc
959 if not checkheads(repo
, remote
, p_revs
):
962 cg
= repo
.getbundle('push', heads
=list(p_revs
), common
=common
)
964 unbundle
= remote
.capable('unbundle')
967 remoteheads
= ['force']
968 return remote
.unbundle(cg
, remoteheads
, 'push')
970 return remote
.addchangegroup(cg
, 'push', repo
.url())
972 def push(repo
, remote
, parsed_refs
, p_revs
):
973 if hasattr(remote
, 'canpush') and not remote
.canpush():
974 print "error cannot push"
981 unbundle
= remote
.capable('unbundle')
985 ret
= push_unsafe(repo
, remote
, parsed_refs
, p_revs
)
992 def check_tip(ref
, kind
, name
, heads
):
994 ename
= '%s/%s' % (kind
, name
)
995 tip
= marks
.get_tip(ename
)
1001 def do_export(parser
):
1002 global parsed_refs
, bmarks
, peer
1009 for line
in parser
.each_block('done'):
1010 if parser
.check('blob'):
1012 elif parser
.check('commit'):
1013 parse_commit(parser
)
1014 elif parser
.check('reset'):
1016 elif parser
.check('tag'):
1018 elif parser
.check('feature'):
1021 die('unhandled export command: %s' % line
)
1025 for ref
, node
in parsed_refs
.iteritems():
1026 bnode
= hgbin(node
) if node
else None
1027 if ref
.startswith('refs/heads/branches'):
1028 branch
= ref
[len('refs/heads/branches/'):]
1029 if branch
in branches
and bnode
in branches
[branch
]:
1034 remotemap
= peer
.branchmap()
1035 if remotemap
and branch
in remotemap
:
1036 heads
= [hghex(e
) for e
in remotemap
[branch
]]
1037 if not check_tip(ref
, 'branches', branch
, heads
):
1038 print "error %s fetch first" % ref
1044 elif ref
.startswith('refs/heads/'):
1045 bmark
= ref
[len('refs/heads/'):]
1047 old
= bmarks
[bmark
].hex() if bmark
in bmarks
else ''
1053 if bmark
!= fake_bmark
and \
1054 not (bmark
== 'master' and bmark
not in parser
.repo
._bookmarks
):
1055 p_bmarks
.append((ref
, bmark
, old
, new
))
1058 remote_old
= peer
.listkeys('bookmarks').get(bmark
)
1060 if not check_tip(ref
, 'bookmarks', bmark
, remote_old
):
1061 print "error %s fetch first" % ref
1066 elif ref
.startswith('refs/tags/'):
1070 tag
= ref
[len('refs/tags/'):]
1072 author
, msg
= parsed_tags
.get(tag
, (None, None))
1075 msg
= 'Added tag %s for changeset %s' % (tag
, node
[:12]);
1076 tagnode
, branch
= write_tag(parser
.repo
, tag
, node
, msg
, author
)
1077 p_revs
[tagnode
] = 'refs/heads/branches/' + gitref(branch
)
1079 fp
= parser
.repo
.opener('localtags', 'a')
1080 fp
.write('%s %s\n' % (node
, tag
))
1085 # transport-helper/fast-export bugs
1093 if peer
and not force_push
:
1094 checkheads(parser
.repo
, peer
, p_revs
)
1099 if not push(parser
.repo
, peer
, parsed_refs
, p_revs
):
1100 # do not update bookmarks
1104 # update remote bookmarks
1105 remote_bmarks
= peer
.listkeys('bookmarks')
1106 for ref
, bmark
, old
, new
in p_bmarks
:
1108 old
= remote_bmarks
.get(bmark
, '')
1109 if not peer
.pushkey('bookmarks', bmark
, old
, new
):
1110 print "error %s" % ref
1112 # update local bookmarks
1113 for ref
, bmark
, old
, new
in p_bmarks
:
1114 if not bookmarks
.pushbookmark(parser
.repo
, bmark
, old
, new
):
1115 print "error %s" % ref
1119 def do_option(parser
):
1120 global dry_run
, force_push
1121 _
, key
, value
= parser
.line
.split(' ')
1122 if key
== 'dry-run':
1123 dry_run
= (value
== 'true')
1125 elif key
== 'force':
1126 force_push
= (value
== 'true')
1131 def fix_path(alias
, repo
, orig_url
):
1132 url
= urlparse
.urlparse(orig_url
, 'file')
1133 if url
.scheme
!= 'file' or os
.path
.isabs(url
.path
):
1135 abs_url
= urlparse
.urljoin("%s/" % os
.getcwd(), orig_url
)
1136 cmd
= ['git', 'config', 'remote.%s.url' % alias
, "hg::%s" % abs_url
]
1137 subprocess
.call(cmd
)
1140 global prefix
, gitdir
, dirname
, branches
, bmarks
1141 global marks
, blob_marks
, parsed_refs
1142 global peer
, mode
, bad_mail
, bad_name
1143 global track_branches
, force_push
, is_tmp
1146 global fake_bmark
, hg_version
1153 hg_git_compat
= get_config_bool('remote-hg.hg-git-compat')
1154 track_branches
= get_config_bool('remote-hg.track-branches', True)
1159 bad_mail
= 'none@none'
1163 bad_mail
= 'unknown'
1164 bad_name
= 'Unknown'
1166 if alias
[4:] == url
:
1168 alias
= hashlib
.sha1(alias
).hexdigest()
1172 gitdir
= os
.environ
['GIT_DIR']
1173 dirname
= os
.path
.join(gitdir
, 'hg', alias
)
1183 hg_version
= tuple(int(e
) for e
in util
.version().split('.'))
1188 repo
= get_repo(url
, alias
)
1189 prefix
= 'refs/hg/%s' % alias
1192 fix_path(alias
, peer
or repo
, url
)
1194 marks_path
= os
.path
.join(dirname
, 'marks-hg')
1195 marks
= Marks(marks_path
, repo
)
1197 if sys
.platform
== 'win32':
1199 msvcrt
.setmode(sys
.stdout
.fileno(), os
.O_BINARY
)
1201 parser
= Parser(repo
)
1203 if parser
.check('capabilities'):
1204 do_capabilities(parser
)
1205 elif parser
.check('list'):
1207 elif parser
.check('import'):
1209 elif parser
.check('export'):
1211 elif parser
.check('option'):
1214 die('unhandled command: %s' % line
)
1223 shutil
.rmtree(dirname
)
1225 atexit
.register(bye
)
1226 sys
.exit(main(sys
.argv
))