patch: don't separate \r and \n when colorizing diff output
authorSune Foldager <cryo@cyanite.org>
Tue, 10 Jul 2018 13:18:34 +0200
changeset 38630 e1987261dd05
parent 38629 38dfd308fe9d
child 38631 9ef10437bb88
patch: don't separate \r and \n when colorizing diff output When displaying diffs, \r at the end of a line is treated as trailing whitespace. This causes an ANSI escape code to be inserted between \r and \n. Some programs, such as less since version 530 (maybe earlier, but at least not version 487) displays ^M when it encounters a lone \r. This causes a lot of noise in diff output on Windows, where \r\n is used to terminate lines. We avoid that by treating both \n and \r\n as end of line when considering trailing whitespace.
mercurial/patch.py
--- a/mercurial/patch.py	Sat Jul 07 23:38:06 2018 -0400
+++ b/mercurial/patch.py	Tue Jul 10 13:18:34 2018 +0200
@@ -2405,7 +2405,7 @@
     """yield tokens for a list of lines in a single hunk"""
     for line in hunklines:
         # chomp
-        chompline = line.rstrip('\n')
+        chompline = line.rstrip('\r\n')
         # highlight tabs and trailing whitespace
         stripline = chompline.rstrip()
         if line.startswith('-'):
@@ -2473,6 +2473,9 @@
             isendofline = token.endswith('\n')
             if isendofline:
                 chomp = token[:-1] # chomp
+                if chomp.endswith('\r'):
+                    chomp = chomp[:-1]
+                endofline = token[len(chomp):]
                 token = chomp.rstrip() # detect spaces at the end
                 endspaces = chomp[len(token):]
             # scan tabs
@@ -2488,7 +2491,7 @@
             if isendofline:
                 if endspaces:
                     yield (endspaces, 'diff.trailingwhitespace')
-                yield ('\n', '')
+                yield (endofline, '')
                 nextisnewline = True
 
 def difflabel(func, *args, **kw):