D3210: patch: move yielding "\n" to the end of loop

quark (Jun Wu) phabricator at mercurial-scm.org
Mon Apr 9 22:59:26 UTC 2018


quark created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3210

AFFECTED FILES
  mercurial/patch.py

CHANGE DETAILS

diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -2499,9 +2499,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
@@ -2540,6 +2539,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



To: quark, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list