# Copyright (c) 2007, 2008 Rocco Rutte <pdmef@gmx.net> and others.
# License: MIT <http://www.opensource.org/licenses/mit-license.php>
-from mercurial import repo,hg,cmdutil,util,ui,revlog,node
+from mercurial import node
from hg2git import setup_repo,fixup_user,get_branch,get_changeset
from hg2git import load_cache,save_cache,get_git_sha1,set_default_branch,set_origin_name
-from tempfile import mkstemp
from optparse import OptionParser
import re
import sys
import os
+if sys.platform == "win32":
+ # On Windows, sys.stdout is initially opened in text mode, which means that
+ # when a LF (\n) character is written to sys.stdout, it will be converted
+ # into CRLF (\r\n). That makes git blow up, so use this platform-specific
+ # code to change the mode of sys.stdout to binary.
+ import msvcrt
+ msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
+
# silly regex to catch Signed-off-by lines in log message
sob_re=re.compile('^Signed-[Oo]ff-[Bb]y: (.+)$')
# insert 'checkpoint' command after this many commits or none at all if 0
return 'l' in flags and '120000' or 'x' in flags and '100755' or '100644'
def wr(msg=''):
- if msg == None:
- msg = ''
- print msg
+ if msg:
+ sys.stdout.write(msg)
+ sys.stdout.write('\n')
#map(lambda x: sys.stderr.write('\t[%s]\n' % x),msg.split('\n'))
def checkpoint(count):
if right==None:
# we have the file but our parent hasn't: add to left set
l.append(left)
- elif match(dleft[left],right):
+ elif match(dleft[left],right) or gitmode(dleft.flags(left))!=gitmode(dright.flags(left)):
# we have it but checksums mismatch: add to center set
c.append(left)
for right in dright.keys():
return r
return committer
-def export_file_contents(ctx,manifest,files):
+def export_file_contents(ctx,manifest,files,hgtags):
count=0
max=len(files)
for file in files:
# Skip .hgtags files. They only get us in trouble.
- if file == ".hgtags":
+ if not hgtags and file == ".hgtags":
sys.stderr.write('Skip %s\n' % (file))
continue
d=ctx.filectx(file).data()
sys.stderr.write('Warning: sanitized %s [%s] to [%s]\n' % (what,name,n))
return n
-def export_commit(ui,repo,revision,old_marks,max,count,authors,sob,brmap):
+def export_commit(ui,repo,revision,old_marks,max,count,authors,sob,brmap,hgtags):
def get_branchname(name):
if brmap.has_key(name):
return brmap[name]
(branch,type,revision+1,max,len(added),len(changed),len(removed)))
map(lambda r: wr('D %s' % r),removed)
- export_file_contents(ctx,man,added)
- export_file_contents(ctx,man,changed)
+ export_file_contents(ctx,man,added,hgtags)
+ export_file_contents(ctx,man,changed,hgtags)
wr()
return checkpoint(count)
sys.stderr.write('Loaded %d authors\n' % l)
return cache
+def branchtip(repo, heads):
+ '''return the tipmost branch head in heads'''
+ tip = heads[-1]
+ for h in reversed(heads):
+ if 'close' not in repo.changelog.read(h)[5]:
+ tip = h
+ break
+ return tip
+
def verify_heads(ui,repo,cache,force):
- branches=repo.branchtags()
+ branches={}
+ for bn, heads in repo.branchmap().iteritems():
+ branches[bn] = branchtip(repo, heads)
l=[(-repo.changelog.rev(n), n, t) for t, n in branches.items()]
l.sort()
return True
-def hg2git(repourl,m,marksfile,mappingfile,headsfile,tipfile,authors={},sob=False,force=False):
+def hg2git(repourl,m,marksfile,mappingfile,headsfile,tipfile,authors={},sob=False,force=False,hgtags=False):
_max=int(m)
old_marks=load_cache(marksfile,lambda s: int(s)-1)
c=0
brmap={}
for rev in range(min,max):
- c=export_commit(ui,repo,rev,old_marks,max,c,authors,sob,brmap)
+ c=export_commit(ui,repo,rev,old_marks,max,c,authors,sob,brmap,hgtags)
state_cache['tip']=max
state_cache['repo']=repourl
help="URL of repo to import")
parser.add_option("-s",action="store_true",dest="sob",
default=False,help="Enable parsing Signed-off-by lines")
+ parser.add_option("--hgtags",action="store_true",dest="hgtags",
+ default=False,help="Enable exporting .hgtags files")
parser.add_option("-A","--authors",dest="authorfile",
help="Read authormap from AUTHORFILE")
parser.add_option("-f","--force",action="store_true",dest="force",
set_origin_name(options.origin_name)
sys.exit(hg2git(options.repourl,m,options.marksfile,options.mappingfile,options.headsfile,
- options.statusfile,authors=a,sob=options.sob,force=options.force))
+ options.statusfile,authors=a,sob=options.sob,force=options.force,hgtags=options.hgtags))