comparison 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
comparison
equal deleted inserted replaced
24305:867c3649be5d 24306:6ddc86eedc3b
257 message = '%s\n%s' % (subject, message) 257 message = '%s\n%s' % (subject, message)
258 tmpfp.close() 258 tmpfp.close()
259 if not diffs_seen: 259 if not diffs_seen:
260 os.unlink(tmpname) 260 os.unlink(tmpname)
261 return None, message, user, date, branch, None, None, None 261 return None, message, user, date, branch, None, None, None
262 p1 = parents and parents.pop(0) or None 262
263 p2 = parents and parents.pop(0) or None 263 if parents:
264 p1 = parents.pop(0)
265 else:
266 p1 = None
267
268 if parents:
269 p2 = parents.pop(0)
270 else:
271 p2 = None
272
264 return tmpname, message, user, date, branch, nodeid, p1, p2 273 return tmpname, message, user, date, branch, nodeid, p1, p2
265 274
266 class patchmeta(object): 275 class patchmeta(object):
267 """Patched file metadata 276 """Patched file metadata
268 277
1487 # file should be patched (see original mpatch code). 1496 # file should be patched (see original mpatch code).
1488 isbackup = (abase == bbase and bfile.startswith(afile)) 1497 isbackup = (abase == bbase and bfile.startswith(afile))
1489 fname = None 1498 fname = None
1490 if not missing: 1499 if not missing:
1491 if gooda and goodb: 1500 if gooda and goodb:
1492 fname = isbackup and afile or bfile 1501 if isbackup:
1502 fname = afile
1503 else:
1504 fname = bfile
1493 elif gooda: 1505 elif gooda:
1494 fname = afile 1506 fname = afile
1495 1507
1496 if not fname: 1508 if not fname:
1497 if not nullb: 1509 if not nullb:
1498 fname = isbackup and afile or bfile 1510 if isbackup:
1511 fname = afile
1512 else:
1513 fname = bfile
1499 elif not nulla: 1514 elif not nulla:
1500 fname = afile 1515 fname = afile
1501 else: 1516 else:
1502 raise PatchError(_("undefined source and destination files")) 1517 raise PatchError(_("undefined source and destination files"))
1503 1518
2068 modified, added, removed = changes[:3] 2083 modified, added, removed = changes[:3]
2069 2084
2070 if not modified and not added and not removed: 2085 if not modified and not added and not removed:
2071 return [] 2086 return []
2072 2087
2073 hexfunc = repo.ui.debugflag and hex or short 2088 if repo.ui.debugflag:
2089 hexfunc = hex
2090 else:
2091 hexfunc = short
2074 revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node] 2092 revs = [hexfunc(node) for node in [ctx1.node(), ctx2.node()] if node]
2075 2093
2076 copy = {} 2094 copy = {}
2077 if opts.git or opts.upgrade: 2095 if opts.git or opts.upgrade:
2078 copy = copies.pathcopies(ctx1, ctx2) 2096 copy = copies.pathcopies(ctx1, ctx2)