# HG changeset patch # User Jun Wu # Date 1521458909 25200 # Node ID 8d730f96e792f52f8dee5f02d80ff6b2e590371b # Parent 6e137da59ad9a4f9a24ac73053a9add2d15cef5e patch: move yielding "\n" to the end of loop The original logic makes it harder to reason about - it yields the "\n" character belonging to the last line in the next loop iteration. The new code is in theory a little bit slower. But is more readable. It makes the following changes easier to read. Differential Revision: https://phab.mercurial-scm.org/D3210 diff -r 6e137da59ad9 -r 8d730f96e792 mercurial/patch.py --- a/mercurial/patch.py Mon Apr 16 09:39:40 2018 -0700 +++ b/mercurial/patch.py Mon Mar 19 04:28:29 2018 -0700 @@ -2505,9 +2505,8 @@ matches = {} if inlinecolor: matches = _findmatches(lines) + linecount = len(lines) for i, line in enumerate(lines): - if i != 0: - yield ('\n', '') if head: if line.startswith('@'): head = False @@ -2546,6 +2545,8 @@ yield (line, '') if line != stripline: yield (line[len(stripline):], 'diff.trailingwhitespace') + if i + 1 < linecount: + yield ('\n', '') def _findmatches(slist): '''Look for insertion matches to deletion and returns a dict of