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')
395 # check and upgrade old organization
396 hg_path
= os
.path
.join(shared_path
, '.hg')
397 if os
.path
.exists(shared_path
) and not os
.path
.exists(hg_path
):
398 repos
= os
.listdir(shared_path
)
400 local_hg
= os
.path
.join(shared_path
, x
, 'clone', '.hg')
401 if not os
.path
.exists(local_hg
):
403 if not os
.path
.exists(hg_path
):
404 shutil
.move(local_hg
, hg_path
)
405 shutil
.rmtree(os
.path
.join(shared_path
, x
, 'clone'))
407 # setup shared repo (if not there)
409 hg
.peer(myui
, {}, shared_path
, create
=True)
410 except error
.RepoError
:
413 if not os
.path
.exists(dirname
):
416 local_path
= os
.path
.join(dirname
, 'clone')
417 if not os
.path
.exists(local_path
):
418 hg
.share(myui
, shared_path
, local_path
, update
=False)
420 repo
= hg
.repository(myui
, local_path
)
422 peer
= hg
.peer(myui
, {}, url
)
424 die('Repository error')
425 repo
.pull(peer
, heads
=None, force
=True)
427 updatebookmarks(repo
, peer
)
431 def rev_to_mark(rev
):
433 return marks
.from_rev(rev
.hex())
435 def mark_to_rev(mark
):
437 return marks
.to_rev(mark
)
439 def export_ref(repo
, name
, kind
, head
):
440 global prefix
, marks
, mode
442 ename
= '%s/%s' % (kind
, name
)
444 tip
= marks
.get_tip(ename
)
445 tip
= repo
[tip
].rev()
449 revs
= xrange(tip
, head
.rev() + 1)
457 if marks
.is_marked(c
.hex()):
460 (manifest
, user
, (time
, tz
), files
, desc
, extra
) = repo
.changelog
.read(node
)
461 rev_branch
= extra
['branch']
463 author
= "%s %d %s" % (fixup_user(user
), time
, gittz(tz
))
464 if 'committer' in extra
:
465 user
, time
, tz
= extra
['committer'].rsplit(' ', 2)
466 committer
= "%s %s %s" % (user
, time
, gittz(int(tz
)))
470 parents
= [repo
[p
] for p
in repo
.changelog
.parentrevs(rev
) if p
>= 0]
472 if len(parents
) == 0:
473 modified
= c
.manifest().keys()
476 modified
, removed
= get_filechanges(repo
, c
, parents
[0])
483 if rev_branch
!= 'default':
484 extra_msg
+= 'branch : %s\n' % rev_branch
488 if f
not in c
.manifest():
490 rename
= c
.filectx(f
).renamed()
492 renames
.append((rename
[0], f
))
495 extra_msg
+= "rename : %s => %s\n" % e
497 for key
, value
in extra
.iteritems():
498 if key
in ('author', 'committer', 'encoding', 'message', 'branch', 'hg-git'):
501 extra_msg
+= "extra : %s : %s\n" % (key
, urllib
.quote(value
))
504 desc
+= '\n--HG--\n' + extra_msg
506 if len(parents
) == 0 and rev
:
507 print 'reset %s/%s' % (prefix
, ename
)
509 modified_final
= export_files(c
.filectx(f
) for f
in modified
)
511 print "commit %s/%s" % (prefix
, ename
)
512 print "mark :%d" % (marks
.get_mark(c
.hex()))
513 print "author %s" % (author
)
514 print "committer %s" % (committer
)
515 print "data %d" % (len(desc
))
519 print "from :%s" % (rev_to_mark(parents
[0]))
521 print "merge :%s" % (rev_to_mark(parents
[1]))
524 print "D %s" % (fix_file_path(f
))
525 for f
in modified_final
:
526 print "M %s :%u %s" % f
529 progress
= (rev
- tip
)
530 if (progress
% 100 == 0):
531 print "progress revision %d '%s' (%d/%d)" % (rev
, name
, progress
, total
)
533 # make sure the ref is updated
534 print "reset %s/%s" % (prefix
, ename
)
535 print "from :%u" % rev_to_mark(head
)
538 marks
.set_tip(ename
, head
.hex())
540 def export_tag(repo
, tag
):
541 export_ref(repo
, tag
, 'tags', repo
[hgref(tag
)])
543 def export_bookmark(repo
, bmark
):
544 head
= bmarks
[hgref(bmark
)]
545 export_ref(repo
, bmark
, 'bookmarks', head
)
547 def export_branch(repo
, branch
):
548 tip
= get_branch_tip(repo
, branch
)
550 export_ref(repo
, branch
, 'branches', head
)
552 def export_head(repo
):
554 export_ref(repo
, g_head
[0], 'bookmarks', g_head
[1])
556 def do_capabilities(parser
):
557 global prefix
, dirname
561 print "refspec refs/heads/branches/*:%s/branches/*" % prefix
562 print "refspec refs/heads/*:%s/bookmarks/*" % prefix
563 print "refspec refs/tags/*:%s/tags/*" % prefix
565 path
= os
.path
.join(dirname
, 'marks-git')
567 if os
.path
.exists(path
):
568 print "*import-marks %s" % path
569 print "*export-marks %s" % path
574 def branch_tip(branch
):
575 return branches
[branch
][-1]
577 def get_branch_tip(repo
, branch
):
580 heads
= branches
.get(hgref(branch
), None)
584 # verify there's only one head
586 warn("Branch '%s' has more than one head, consider merging" % branch
)
587 return branch_tip(hgref(branch
))
591 def list_head(repo
, cur
):
592 global g_head
, bmarks
, fake_bmark
594 if 'default' not in branches
:
598 node
= repo
[branch_tip('default')]
599 head
= 'master' if not 'master' in bmarks
else 'default'
604 print "@refs/heads/%s HEAD" % head
605 g_head
= (head
, node
)
608 global branches
, bmarks
, track_branches
611 for bmark
, node
in bookmarks
.listbookmarks(repo
).iteritems():
612 bmarks
[bmark
] = repo
[node
]
614 cur
= repo
.dirstate
.branch()
615 orig
= peer
if peer
else repo
617 for branch
, heads
in orig
.branchmap().iteritems():
619 heads
= [h
for h
in heads
if 'close' not in repo
.changelog
.read(h
)[5]]
621 branches
[branch
] = heads
626 for branch
in branches
:
627 print "? refs/heads/branches/%s" % gitref(branch
)
630 print "? refs/heads/%s" % gitref(bmark
)
632 for tag
, node
in repo
.tagslist():
635 print "? refs/tags/%s" % gitref(tag
)
639 def do_import(parser
):
642 path
= os
.path
.join(dirname
, 'marks-git')
645 if os
.path
.exists(path
):
646 print "feature import-marks=%s" % path
647 print "feature export-marks=%s" % path
648 print "feature force"
651 tmp
= encoding
.encoding
652 encoding
.encoding
= 'utf-8'
654 # lets get all the import lines
655 while parser
.check('import'):
660 elif ref
.startswith('refs/heads/branches/'):
661 branch
= ref
[len('refs/heads/branches/'):]
662 export_branch(repo
, branch
)
663 elif ref
.startswith('refs/heads/'):
664 bmark
= ref
[len('refs/heads/'):]
665 export_bookmark(repo
, bmark
)
666 elif ref
.startswith('refs/tags/'):
667 tag
= ref
[len('refs/tags/'):]
668 export_tag(repo
, tag
)
672 encoding
.encoding
= tmp
676 def parse_blob(parser
):
680 mark
= parser
.get_mark()
682 data
= parser
.get_data()
683 blob_marks
[mark
] = data
686 def get_merge_files(repo
, p1
, p2
, files
):
687 for e
in repo
[p1
].files():
689 if e
not in repo
[p1
].manifest():
691 f
= { 'ctx' : repo
[p1
][e
] }
694 def parse_commit(parser
):
695 global marks
, blob_marks
, parsed_refs
698 from_mark
= merge_mark
= None
703 commit_mark
= parser
.get_mark()
705 author
= parser
.get_author()
707 committer
= parser
.get_author()
709 data
= parser
.get_data()
711 if parser
.check('from'):
712 from_mark
= parser
.get_mark()
714 if parser
.check('merge'):
715 merge_mark
= parser
.get_mark()
717 if parser
.check('merge'):
718 die('octopus merges are not supported yet')
720 # fast-export adds an extra newline
727 if parser
.check('M'):
728 t
, m
, mark_ref
, path
= line
.split(' ', 3)
729 mark
= int(mark_ref
[1:])
730 f
= { 'mode' : hgmode(m
), 'data' : blob_marks
[mark
] }
731 elif parser
.check('D'):
732 t
, path
= line
.split(' ', 1)
733 f
= { 'deleted' : True }
735 die('Unknown file command: %s' % line
)
738 # only export the commits if we are on an internal proxy repo
739 if dry_run
and not peer
:
740 parsed_refs
[ref
] = None
743 def getfilectx(repo
, memctx
, f
):
749 is_exec
= of
['mode'] == 'x'
750 is_link
= of
['mode'] == 'l'
751 rename
= of
.get('rename', None)
752 return context
.memfilectx(f
, of
['data'],
753 is_link
, is_exec
, rename
)
757 user
, date
, tz
= author
760 if committer
!= author
:
761 extra
['committer'] = "%s %u %u" % committer
764 p1
= mark_to_rev(from_mark
)
769 p2
= mark_to_rev(merge_mark
)
774 # If files changed from any of the parents, hg wants to know, but in git if
775 # nothing changed from the first parent, nothing changed.
778 get_merge_files(repo
, p1
, p2
, files
)
780 # Check if the ref is supposed to be a named branch
781 if ref
.startswith('refs/heads/branches/'):
782 branch
= ref
[len('refs/heads/branches/'):]
783 extra
['branch'] = hgref(branch
)
786 i
= data
.find('\n--HG--\n')
788 tmp
= data
[i
+ len('\n--HG--\n'):].strip()
789 for k
, v
in [e
.split(' : ', 1) for e
in tmp
.split('\n')]:
791 old
, new
= v
.split(' => ', 1)
792 files
[new
]['rename'] = old
796 ek
, ev
= v
.split(' : ', 1)
797 extra
[ek
] = urllib
.unquote(ev
)
800 ctx
= context
.memctx(repo
, (p1
, p2
), data
,
801 files
.keys(), getfilectx
,
802 user
, (date
, tz
), extra
)
804 tmp
= encoding
.encoding
805 encoding
.encoding
= 'utf-8'
807 node
= hghex(repo
.commitctx(ctx
))
809 encoding
.encoding
= tmp
811 parsed_refs
[ref
] = node
812 marks
.new_mark(node
, commit_mark
)
814 def parse_reset(parser
):
820 if parser
.check('commit'):
823 if not parser
.check('from'):
825 from_mark
= parser
.get_mark()
829 rev
= mark_to_rev(from_mark
)
832 parsed_refs
[ref
] = rev
834 def parse_tag(parser
):
837 from_mark
= parser
.get_mark()
839 tagger
= parser
.get_author()
841 data
= parser
.get_data()
844 parsed_tags
[name
] = (tagger
, data
)
846 def write_tag(repo
, tag
, node
, msg
, author
):
847 branch
= repo
[node
].branch()
848 tip
= branch_tip(branch
)
851 def getfilectx(repo
, memctx
, f
):
853 fctx
= tip
.filectx(f
)
855 except error
.ManifestLookupError
:
857 content
= data
+ "%s %s\n" % (node
, tag
)
858 return context
.memfilectx(f
, content
, False, False, None)
863 user
, date
, tz
= author
866 cmd
= ['git', 'var', 'GIT_COMMITTER_IDENT']
867 process
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
868 output
, _
= process
.communicate()
869 m
= re
.match('^.* <.*>', output
)
873 user
= repo
.ui
.username()
876 ctx
= context
.memctx(repo
, (p1
, p2
), msg
,
877 ['.hgtags'], getfilectx
,
878 user
, date_tz
, {'branch' : branch
})
880 tmp
= encoding
.encoding
881 encoding
.encoding
= 'utf-8'
883 tagnode
= repo
.commitctx(ctx
)
885 encoding
.encoding
= tmp
887 return (tagnode
, branch
)
889 def checkheads_bmark(repo
, ref
, ctx
):
890 bmark
= ref
[len('refs/heads/'):]
891 if not bmark
in bmarks
:
895 ctx_old
= bmarks
[bmark
]
897 if not repo
.changelog
.descendant(ctx_old
.rev(), ctx_new
.rev()):
899 print "ok %s forced update" % ref
901 print "error %s non-fast forward" % ref
906 def checkheads(repo
, remote
, p_revs
):
908 remotemap
= remote
.branchmap()
916 for node
, ref
in p_revs
.iteritems():
918 branch
= ctx
.branch()
919 if not branch
in remotemap
:
922 if not ref
.startswith('refs/heads/branches'):
923 if ref
.startswith('refs/heads/'):
924 if not checkheads_bmark(repo
, ref
, ctx
):
927 # only check branches
929 new
.setdefault(branch
, []).append(ctx
.rev())
931 for branch
, heads
in new
.iteritems():
932 old
= [repo
.changelog
.rev(x
) for x
in remotemap
[branch
]]
934 if check_version(2, 3):
935 ancestors
= repo
.changelog
.ancestors([rev
], stoprev
=min(old
))
937 ancestors
= repo
.changelog
.ancestors(rev
)
948 node
= repo
.changelog
.node(rev
)
951 print "ok %s forced update" % ref
953 print "error %s non-fast forward" % ref
958 def push_unsafe(repo
, remote
, parsed_refs
, p_revs
):
962 fci
= discovery
.findcommonincoming
963 commoninc
= fci(repo
, remote
, force
=force
)
964 common
, _
, remoteheads
= commoninc
966 if not checkheads(repo
, remote
, p_revs
):
969 cg
= repo
.getbundle('push', heads
=list(p_revs
), common
=common
)
971 unbundle
= remote
.capable('unbundle')
974 remoteheads
= ['force']
975 return remote
.unbundle(cg
, remoteheads
, 'push')
977 return remote
.addchangegroup(cg
, 'push', repo
.url())
979 def push(repo
, remote
, parsed_refs
, p_revs
):
980 if hasattr(remote
, 'canpush') and not remote
.canpush():
981 print "error cannot push"
988 unbundle
= remote
.capable('unbundle')
992 ret
= push_unsafe(repo
, remote
, parsed_refs
, p_revs
)
999 def check_tip(ref
, kind
, name
, heads
):
1001 ename
= '%s/%s' % (kind
, name
)
1002 tip
= marks
.get_tip(ename
)
1008 def do_export(parser
):
1009 global parsed_refs
, bmarks
, peer
1016 for line
in parser
.each_block('done'):
1017 if parser
.check('blob'):
1019 elif parser
.check('commit'):
1020 parse_commit(parser
)
1021 elif parser
.check('reset'):
1023 elif parser
.check('tag'):
1025 elif parser
.check('feature'):
1028 die('unhandled export command: %s' % line
)
1032 for ref
, node
in parsed_refs
.iteritems():
1033 bnode
= hgbin(node
) if node
else None
1034 if ref
.startswith('refs/heads/branches'):
1035 branch
= ref
[len('refs/heads/branches/'):]
1036 if branch
in branches
and bnode
in branches
[branch
]:
1041 remotemap
= peer
.branchmap()
1042 if remotemap
and branch
in remotemap
:
1043 heads
= [hghex(e
) for e
in remotemap
[branch
]]
1044 if not check_tip(ref
, 'branches', branch
, heads
):
1045 print "error %s fetch first" % ref
1051 elif ref
.startswith('refs/heads/'):
1052 bmark
= ref
[len('refs/heads/'):]
1054 old
= bmarks
[bmark
].hex() if bmark
in bmarks
else ''
1060 if bmark
!= fake_bmark
and \
1061 not (bmark
== 'master' and bmark
not in parser
.repo
._bookmarks
):
1062 p_bmarks
.append((ref
, bmark
, old
, new
))
1065 remote_old
= peer
.listkeys('bookmarks').get(bmark
)
1067 if not check_tip(ref
, 'bookmarks', bmark
, remote_old
):
1068 print "error %s fetch first" % ref
1073 elif ref
.startswith('refs/tags/'):
1077 tag
= ref
[len('refs/tags/'):]
1079 author
, msg
= parsed_tags
.get(tag
, (None, None))
1082 msg
= 'Added tag %s for changeset %s' % (tag
, node
[:12]);
1083 tagnode
, branch
= write_tag(parser
.repo
, tag
, node
, msg
, author
)
1084 p_revs
[tagnode
] = 'refs/heads/branches/' + gitref(branch
)
1086 fp
= parser
.repo
.opener('localtags', 'a')
1087 fp
.write('%s %s\n' % (node
, tag
))
1092 # transport-helper/fast-export bugs
1100 if peer
and not force_push
:
1101 checkheads(parser
.repo
, peer
, p_revs
)
1106 if not push(parser
.repo
, peer
, parsed_refs
, p_revs
):
1107 # do not update bookmarks
1111 # update remote bookmarks
1112 remote_bmarks
= peer
.listkeys('bookmarks')
1113 for ref
, bmark
, old
, new
in p_bmarks
:
1115 old
= remote_bmarks
.get(bmark
, '')
1116 if not peer
.pushkey('bookmarks', bmark
, old
, new
):
1117 print "error %s" % ref
1119 # update local bookmarks
1120 for ref
, bmark
, old
, new
in p_bmarks
:
1121 if not bookmarks
.pushbookmark(parser
.repo
, bmark
, old
, new
):
1122 print "error %s" % ref
1126 def do_option(parser
):
1127 global dry_run
, force_push
1128 _
, key
, value
= parser
.line
.split(' ')
1129 if key
== 'dry-run':
1130 dry_run
= (value
== 'true')
1132 elif key
== 'force':
1133 force_push
= (value
== 'true')
1138 def fix_path(alias
, repo
, orig_url
):
1139 url
= urlparse
.urlparse(orig_url
, 'file')
1140 if url
.scheme
!= 'file' or os
.path
.isabs(os
.path
.expanduser(url
.path
)):
1142 abs_url
= urlparse
.urljoin("%s/" % os
.getcwd(), orig_url
)
1143 cmd
= ['git', 'config', 'remote.%s.url' % alias
, "hg::%s" % abs_url
]
1144 subprocess
.call(cmd
)
1147 global prefix
, gitdir
, dirname
, branches
, bmarks
1148 global marks
, blob_marks
, parsed_refs
1149 global peer
, mode
, bad_mail
, bad_name
1150 global track_branches
, force_push
, is_tmp
1153 global fake_bmark
, hg_version
1160 hg_git_compat
= get_config_bool('remote-hg.hg-git-compat')
1161 track_branches
= get_config_bool('remote-hg.track-branches', True)
1166 bad_mail
= 'none@none'
1170 bad_mail
= 'unknown'
1171 bad_name
= 'Unknown'
1173 if alias
[4:] == url
:
1175 alias
= hashlib
.sha1(alias
).hexdigest()
1179 gitdir
= os
.environ
['GIT_DIR']
1180 dirname
= os
.path
.join(gitdir
, 'hg', alias
)
1190 hg_version
= tuple(int(e
) for e
in util
.version().split('.'))
1195 repo
= get_repo(url
, alias
)
1196 prefix
= 'refs/hg/%s' % alias
1199 fix_path(alias
, peer
or repo
, url
)
1201 marks_path
= os
.path
.join(dirname
, 'marks-hg')
1202 marks
= Marks(marks_path
, repo
)
1204 if sys
.platform
== 'win32':
1206 msvcrt
.setmode(sys
.stdout
.fileno(), os
.O_BINARY
)
1208 parser
= Parser(repo
)
1210 if parser
.check('capabilities'):
1211 do_capabilities(parser
)
1212 elif parser
.check('list'):
1214 elif parser
.check('import'):
1216 elif parser
.check('export'):
1218 elif parser
.check('option'):
1221 die('unhandled command: %s' % line
)
1230 shutil
.rmtree(dirname
)
1232 atexit
.register(bye
)
1233 sys
.exit(main(sys
.argv
))