diff mercurial/patch.py @ 24306:6ddc86eedc3b

style: kill ersatz if-else ternary operators Although Python supports `X = Y if COND else Z`, this was only introduced in Python 2.5. Since we have to support Python 2.4, it was a very common thing to write instead `X = COND and Y or Z`, which is a bit obscure at a glance. It requires some intricate knowledge of Python to understand how to parse these one-liners. We change instead all of these one-liners to 4-liners. This was executed with the following perlism: find -name "*.py" -exec perl -pi -e 's,(\s*)([\.\w]+) = \(?(\S+)\s+and\s+(\S*)\)?\s+or\s+(\S*)$,$1if $3:\n$1 $2 = $4\n$1else:\n$1 $2 = $5,' {} \; I tweaked the following cases from the automatic Perl output: prev = (parents and parents[0]) or nullid port = (use_ssl and 443 or 80) cwd = (pats and repo.getcwd()) or '' rename = fctx and webutil.renamelink(fctx) or [] ctx = fctx and fctx or ctx self.base = (mapfile and os.path.dirname(mapfile)) or '' I also added some newlines wherever they seemd appropriate for readability There are probably a few ersatz ternary operators still in the code somewhere, lurking away from the power of a simple regex.
author Jordi Guti?rrez Hermoso <jordigh@octave.org>
date Fri, 13 Mar 2015 17:00:06 -0400
parents 9a745ced79a9
children 616c01b69898
line wrap: on
line diff
--- a/mercurial/patch.py	Fri Mar 13 14:20:13 2015 -0400
+++ b/mercurial/patch.py	Fri Mar 13 17:00:06 2015 -0400
@@ -259,8 +259,17 @@
     if not diffs_seen:
         os.unlink(tmpname)
         return None, message, user, date, branch, None, None, None
-    p1 = parents and parents.pop(0) or None
-    p2 = parents and parents.pop(0) or None
+
+    if parents:
+        p1 = parents.pop(0)
+    else:
+        p1 = None
+
+    if parents:
+        p2 = parents.pop(0)
+    else:
+        p2 = None
+
     return tmpname, message, user, date, branch, nodeid, p1, p2
 
 class patchmeta(object):
@@ -1489,13 +1498,19 @@
     fname = None
     if not missing:
         if gooda and goodb:
-            fname = isbackup and afile or bfile
+            if isbackup:
+                fname = afile
+            else:
+                fname = bfile
         elif gooda:
             fname = afile
 
     if not fname:
         if not nullb:
-            fname = isbackup and afile or bfile
+            if isbackup:
+                fname = afile
+            else:
+                fname = bfile
         elif not nulla:
             fname = afile
         else:
@@ -2070,7 +2085,10 @@
     if not modified and not added and not removed:
         return []
 
-    hexfunc = repo.ui.debugflag and hex or short
+    if repo.ui.debugflag:
+        hexfunc = hex
+    else:
+        hexfunc = short
     revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node]
 
     copy = {}