comparison mercurial/bundlerepo.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 ff5caa8dfd99
children e0e28e910fa3
comparison
equal deleted inserted replaced
24305:867c3649be5d 24306:6ddc86eedc3b
424 # compat with older servers when pulling all remote heads 424 # compat with older servers when pulling all remote heads
425 cg = other.changegroup(incoming, "incoming") 425 cg = other.changegroup(incoming, "incoming")
426 rheads = None 426 rheads = None
427 else: 427 else:
428 cg = other.changegroupsubset(incoming, rheads, 'incoming') 428 cg = other.changegroupsubset(incoming, rheads, 'incoming')
429 bundletype = localrepo and "HG10BZ" or "HG10UN" 429 if localrepo:
430 bundletype = "HG10BZ"
431 else:
432 bundletype = "HG10UN"
430 fname = bundle = changegroup.writebundle(ui, cg, bundlename, bundletype) 433 fname = bundle = changegroup.writebundle(ui, cg, bundlename, bundletype)
431 # keep written bundle? 434 # keep written bundle?
432 if bundlename: 435 if bundlename:
433 bundle = None 436 bundle = None
434 if not localrepo: 437 if not localrepo: