[PATCH] Handle CR newline for diff

OHASHI Hideya ohachige at gmail.com
Fri Mar 28 06:16:02 CDT 2008


# HG changeset patch
# User OHASHI Hideya <ohachige at gmail.com>
# Date 1206701660 -32400
# Node ID 2f1acb5dc43d7291da4966d72b2089a75d2ed4fd
# Parent  79ea07e9b5aea2e4376be93e77bd34eddca7ac54
Handle CR newline for diff

diff -r 79ea07e9b5ae -r 2f1acb5dc43d contrib/hgdiff
--- a/contrib/hgdiff	Fri Mar 28 18:45:28 2008 +0900
+++ b/contrib/hgdiff	Fri Mar 28 19:54:20 2008 +0900
@@ -62,7 +62,9 @@
                                   showfunc=options.show_c_function,
                                   ignorews=options.ignore_all_space))
     for x in l:
-        if x[-1] != '\n':
+        if x[-1] == '\r':
+            x = x[:-1] + '\n'
+        elif x[-1] != '\n':
             x += "\n\ No newline at end of file\n"
         print x,
 
diff -r 79ea07e9b5ae -r 2f1acb5dc43d mercurial/mdiff.py
--- a/mercurial/mdiff.py	Fri Mar 28 18:45:28 2008 +0900
+++ b/mercurial/mdiff.py	Fri Mar 28 19:54:20 2008 +0900
@@ -9,7 +9,10 @@
 
 def splitnewlines(text):
     '''like str.splitlines, but only split on newlines.'''
-    lines = [l + '\n' for l in text.split('\n')]
+    delim = '\n'
+    if text.count(delim) < text.count('\r'):
+        delim = '\r'
+    lines = [l + '\n' for l in text.split(delim)]
     if lines:
         if lines[-1] == '\n':
             lines.pop()
@@ -98,6 +101,10 @@
     else:
         al = splitnewlines(a)
         bl = splitnewlines(b)
+        if a.count('\n') < a.count('\r'):
+            a = a.replace('\r', '\n')
+        if b.count('\n') < b.count('\r'):
+            b = b.replace('\r', '\n')
         l = list(bunidiff(a, b, al, bl, "a/" + fn1, "b/" + fn2, opts=opts))
         if not l: return ""
         # difflib uses a space, rather than a tab


More information about the Mercurial-devel mailing list