hg-to-git: make it compatible with both python3 and python2

Python 2 is EOL at the end of 2019, many distros and systems now
come with python 3 as their default version.

Rewrite features used in hg-to-git that are no longer supported in
Python 3, in such a way that an updated code can still be usable
with Python 2:

 - print is not a statement; use print() function instead.
 - dict.has_key(key) is no more; use "key in dict" instead.

Signed-off-by: Hervé Beraud <herveberaud.pro@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Hervé Beraud 2019-09-18 08:33:22 +00:00 committed by Junio C Hamano
parent 5fa0f5238b
commit d17ae00c97

View File

@ -42,7 +42,7 @@ hgnewcsets = 0
def usage(): def usage():
print """\ print("""\
%s: [OPTIONS] <hgprj> %s: [OPTIONS] <hgprj>
options: options:
@ -54,7 +54,7 @@ options:
required: required:
hgprj: name of the HG project to import (directory) hgprj: name of the HG project to import (directory)
""" % sys.argv[0] """ % sys.argv[0])
#------------------------------------------------------------------------------ #------------------------------------------------------------------------------
@ -104,22 +104,22 @@ os.chdir(hgprj)
if state: if state:
if os.path.exists(state): if os.path.exists(state):
if verbose: if verbose:
print 'State does exist, reading' print('State does exist, reading')
f = open(state, 'r') f = open(state, 'r')
hgvers = pickle.load(f) hgvers = pickle.load(f)
else: else:
print 'State does not exist, first run' print('State does not exist, first run')
sock = os.popen('hg tip --template "{rev}"') sock = os.popen('hg tip --template "{rev}"')
tip = sock.read() tip = sock.read()
if sock.close(): if sock.close():
sys.exit(1) sys.exit(1)
if verbose: if verbose:
print 'tip is', tip print('tip is', tip)
# Calculate the branches # Calculate the branches
if verbose: if verbose:
print 'analysing the branches...' print('analysing the branches...')
hgchildren["0"] = () hgchildren["0"] = ()
hgparents["0"] = (None, None) hgparents["0"] = (None, None)
hgbranch["0"] = "master" hgbranch["0"] = "master"
@ -154,15 +154,15 @@ for cset in range(1, int(tip) + 1):
else: else:
hgbranch[str(cset)] = "branch-" + str(cset) hgbranch[str(cset)] = "branch-" + str(cset)
if not hgvers.has_key("0"): if "0" not in hgvers:
print 'creating repository' print('creating repository')
os.system('git init') os.system('git init')
# loop through every hg changeset # loop through every hg changeset
for cset in range(int(tip) + 1): for cset in range(int(tip) + 1):
# incremental, already seen # incremental, already seen
if hgvers.has_key(str(cset)): if str(cset) in hgvers:
continue continue
hgnewcsets += 1 hgnewcsets += 1
@ -180,27 +180,27 @@ for cset in range(int(tip) + 1):
os.write(fdcomment, csetcomment) os.write(fdcomment, csetcomment)
os.close(fdcomment) os.close(fdcomment)
print '-----------------------------------------' print('-----------------------------------------')
print 'cset:', cset print('cset:', cset)
print 'branch:', hgbranch[str(cset)] print('branch:', hgbranch[str(cset)])
print 'user:', user print('user:', user)
print 'date:', date print('date:', date)
print 'comment:', csetcomment print('comment:', csetcomment)
if parent: if parent:
print 'parent:', parent print('parent:', parent)
if mparent: if mparent:
print 'mparent:', mparent print('mparent:', mparent)
if tag: if tag:
print 'tag:', tag print('tag:', tag)
print '-----------------------------------------' print('-----------------------------------------')
# checkout the parent if necessary # checkout the parent if necessary
if cset != 0: if cset != 0:
if hgbranch[str(cset)] == "branch-" + str(cset): if hgbranch[str(cset)] == "branch-" + str(cset):
print 'creating new branch', hgbranch[str(cset)] print('creating new branch', hgbranch[str(cset)])
os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent])) os.system('git checkout -b %s %s' % (hgbranch[str(cset)], hgvers[parent]))
else: else:
print 'checking out branch', hgbranch[str(cset)] print('checking out branch', hgbranch[str(cset)])
os.system('git checkout %s' % hgbranch[str(cset)]) os.system('git checkout %s' % hgbranch[str(cset)])
# merge # merge
@ -209,7 +209,7 @@ for cset in range(int(tip) + 1):
otherbranch = hgbranch[mparent] otherbranch = hgbranch[mparent]
else: else:
otherbranch = hgbranch[parent] otherbranch = hgbranch[parent]
print 'merging', otherbranch, 'into', hgbranch[str(cset)] print('merging', otherbranch, 'into', hgbranch[str(cset)])
os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch)) os.system(getgitenv(user, date) + 'git merge --no-commit -s ours "" %s %s' % (hgbranch[str(cset)], otherbranch))
# remove everything except .git and .hg directories # remove everything except .git and .hg directories
@ -233,12 +233,12 @@ for cset in range(int(tip) + 1):
# delete branch if not used anymore... # delete branch if not used anymore...
if mparent and len(hgchildren[str(cset)]): if mparent and len(hgchildren[str(cset)]):
print "Deleting unused branch:", otherbranch print("Deleting unused branch:", otherbranch)
os.system('git branch -d %s' % otherbranch) os.system('git branch -d %s' % otherbranch)
# retrieve and record the version # retrieve and record the version
vvv = os.popen('git show --quiet --pretty=format:%H').read() vvv = os.popen('git show --quiet --pretty=format:%H').read()
print 'record', cset, '->', vvv print('record', cset, '->', vvv)
hgvers[str(cset)] = vvv hgvers[str(cset)] = vvv
if hgnewcsets >= opt_nrepack and opt_nrepack != -1: if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
@ -247,7 +247,7 @@ if hgnewcsets >= opt_nrepack and opt_nrepack != -1:
# write the state for incrementals # write the state for incrementals
if state: if state:
if verbose: if verbose:
print 'Writing state' print('Writing state')
f = open(state, 'w') f = open(state, 'w')
pickle.dump(hgvers, f) pickle.dump(hgvers, f)