Re: [PATCH] make hg-to-git compatible with python2.x and 3.x

2018-02-20 Thread Junio C Hamano
Junio C Hamano  writes:

> - map(lambda ..., collection) is not liked; use list comprehension.
> ...
> I am not sure about the change from map(lambda ...) to list
> comprehension, though.  Not that I have a preference for or against
> (I am not a Python person), but I do not know if this is a change
> necessary to run with Python 3 or if it is merely more preferred to
> use list comprehension.
> ...
>> -prnts = map(lambda x: x[:x.find(':')], prnts)
>> +prnts = [x[:x.find(':')] for x in prnts]

Well, I should have dug this myself a bit more [*1*] before hitting
[SEND].  This change is required because map() no longer returns a
list.  So the bullet item in the proposed commit log message in my
earlier message should be updated to say that instead--it was unclear
to me so I phrased as if it was a mere preference, but it actually
is a required change.


[Footnote]

*1* Well, it would have been ideal if the original patch had enough
information from the beginning to make this kind of "digging"
unnecessary ;-)






Re: [PATCH] make hg-to-git compatible with python2.x and 3.x

2018-02-20 Thread Junio C Hamano
Hervé Beraud  writes:

> Signed-off-by: Hervé Beraud 
> ---
>  contrib/hg-to-git/hg-to-git.py | 52 
> +-
>  1 file changed, 26 insertions(+), 26 deletions(-)

I think you shrunk the scope of the change, but I feel that it is
still a bit under-explained.  Let me try to write a proposed commit
log message and ask you to see if I understood the idea behind the
changes correctly:

Rewrite features that are no longer supported (or recommended)
in Python 3 in the script so that it can be used with both
Python 2 and 3, namely:

- print is not a statement; use print() function instead.
- dict.has_key(key) is no more; use "key in dict" instead.
- map(lambda ..., collection) is not liked; use list comprehension.

Hopefully this would also serve as an illustration of the kind of
things we want in our log message.

I am not sure about the change from map(lambda ...) to list
comprehension, though.  Not that I have a preference for or against
(I am not a Python person), but I do not know if this is a change
necessary to run with Python 3 or if it is merely more preferred to
use list comprehension.

Thanks.

> diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
> index de3f81667ed97..8fa7698df5c20 100755
> --- a/contrib/hg-to-git/hg-to-git.py
> +++ b/contrib/hg-to-git/hg-to-git.py
> @@ -42,7 +42,7 @@
>  
>  def usage():
>  
> -print """\
> +print("""\
>  %s: [OPTIONS] 
>  
>  options:
> @@ -54,7 +54,7 @@ def usage():
>  
>  required:
>  hgprj:  name of the HG project to import (directory)
> -""" % sys.argv[0]
> +""" % sys.argv[0])
>  
>  
> #--
>  
> @@ -104,29 +104,29 @@ def getgitenv(user, date):
>  if state:
>  if os.path.exists(state):
>  if verbose:
> -print 'State does exist, reading'
> +print('State does exist, reading')
>  f = open(state, 'r')
>  hgvers = pickle.load(f)
>  else:
> -print 'State does not exist, first run'
> +print('State does not exist, first run')
>  
>  sock = os.popen('hg tip --template "{rev}"')
>  tip = sock.read()
>  if sock.close():
>  sys.exit(1)
>  if verbose:
> -print 'tip is', tip
> +print('tip is', tip)
>  
>  # Calculate the branches
>  if verbose:
> -print 'analysing the branches...'
> +print('analysing the branches...')
>  hgchildren["0"] = ()
>  hgparents["0"] = (None, None)
>  hgbranch["0"] = "master"
>  for cset in range(1, int(tip) + 1):
>  hgchildren[str(cset)] = ()
>  prnts = os.popen('hg log -r %d --template "{parents}"' % 
> cset).read().strip().split(' ')
> -prnts = map(lambda x: x[:x.find(':')], prnts)
> +prnts = [x[:x.find(':')] for x in prnts]
>  if prnts[0] != '':
>  parent = prnts[0].strip()
>  else:
> @@ -154,15 +154,15 @@ def getgitenv(user, date):
>  else:
>  hgbranch[str(cset)] = "branch-" + str(cset)
>  
> -if not hgvers.has_key("0"):
> -print 'creating repository'
> +if "0" not in hgvers:
> +print('creating repository')
>  os.system('git init')
>  
>  # loop through every hg changeset
>  for cset in range(int(tip) + 1):
>  
>  # incremental, already seen
> -if hgvers.has_key(str(cset)):
> +if str(cset) in hgvers:
>  continue
>  hgnewcsets += 1
>  
> @@ -180,27 +180,27 @@ def getgitenv(user, date):
>  os.write(fdcomment, csetcomment)
>  os.close(fdcomment)
>  
> -print '-'
> -print 'cset:', cset
> -print 'branch:', hgbranch[str(cset)]
> -print 'user:', user
> -print 'date:', date
> -print 'comment:', csetcomment
> +print('-')
> +print('cset:', cset)
> +print('branch:', hgbranch[str(cset)])
> +print('user:', user)
> +print('date:', date)
> +print('comment:', csetcomment)
>  if parent:
> - print 'parent:', parent
> + print('parent:', parent)
>  if mparent:
> -print 'mparent:', mparent
> +print('mparent:', mparent)
>  if tag:
> -print 'tag:', tag
> -print '-'
> +print('tag:', tag)
> +print('-')
>  
>  # checkout the parent if necessary
>  if cset != 0:
>  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]))
>  else:
> -print 'checking out branch', hgbranch[str(cset)]
> +print('checking out branch', hgbranch[str(cset)])
>  os.system('git checkout %s' % hgbranch[str(cset)])
>  
>  # merge
> @@ -209,7 +209,7 @@ 

Re: [PATCH] make hg-to-git compatible with python2.x and 3.x

2018-02-15 Thread Junio C Hamano
Hervé Beraud  writes:

> ---

Thanks for posting, but this is way under-justified, even for
something in contrib/ area.  

Are all changes in this patch necessary to "make it compatible with
both Python 2 and 3", or are some parts do not contribute directly
to the objective but are good changes to suit your personal taste,
match BCP styles, etc.?  I am guessing it is the latter (e.g. with
Python 3, you cannot use print without invoking it as a function,
but os.system() can still accept a literal command line string, so
introducing variable "rm_f" that gets assigned a literal string only
once to pass it to os.system() does not have anything to do with the
Python2to3 transition), and if that is the case, it makes sense to
split this into at least 2 patches, i.e. prelimiary clean-up,
followed by changes required to run it with Python3.  It also is OK
to make it a 3-patch series, in which case you would limit the
preliminary clean-up to minimum uncontroversial changes and follow
up with an optional update to suit personal taste at the very end.

Also, the submission needs to be signed-off for us to be able to
use.  Please see Documentation/SubmittingPatches for details.

Thanks.

>  contrib/hg-to-git/hg-to-git.py | 140 
> -
>  1 file changed, 83 insertions(+), 57 deletions(-)
>
> diff --git a/contrib/hg-to-git/hg-to-git.py b/contrib/hg-to-git/hg-to-git.py
> ...
>  os.system('git ls-files -x .hg --others | git update-index --add 
> --stdin')
>  # delete removed files
> -os.system('git ls-files -x .hg --deleted | git update-index --remove 
> --stdin')
> +rm_f = 'git ls-files -x .hg --deleted | git update-index --remove 
> --stdin'
> +os.system(rm_f)
>  ... 
> @@ -247,7 +273,7 @@ def getgitenv(user, date):
>  # write the state for incrementals
>  if state:
>  if verbose:
> -print 'Writing state'
> +print('Writing state')
>  f = open(state, 'w')
>  pickle.dump(hgvers, f)
>  
>
> --
> https://github.com/git/git/pull/458