[PATCH 1 of 1] patch: create .rej without eol transformation

Shun-ichi Goto shunichi.goto at gmail.com
Sun Dec 5 06:23:21 CST 2010


# HG changeset patch
# User Shun-ichi GOTO <shunichi.goto at gmail.com>
# Date 1291551559 -32400
# Node ID 9f632a224f1ad6a6cd343016e7e1bdd098d1fc57
# Parent  cec6140b0f3c8c00515dbf3589068d1e2d233fdf
patch: create .rej without eol transformation

Adds an optional boolean argument 'strict' to skip eol transformations
and use it on creating .rej file.

diff -r cec6140b0f3c -r 9f632a224f1a mercurial/patch.py
--- a/mercurial/patch.py	Thu Dec 02 18:14:04 2010 +0900
+++ b/mercurial/patch.py	Sun Dec 05 21:19:19 2010 +0900
@@ -425,7 +425,7 @@
         finally:
             fp.close()
 
-    def writelines(self, fname, lines):
+    def writelines(self, fname, lines, strict=False):
         # Ensure supplied data ends in fname, being a regular file or
         # a symlink. cmdutil.updatedir will -too magically- take care
         # of setting it to the proper type afterwards.
@@ -435,7 +435,9 @@
         else:
             fp = self.opener(fname, 'w')
         try:
-            if self.eolmode == 'auto':
+            if strict:
+                eol = None
+            elif self.eolmode == 'auto':
                 eol = self.eol
             elif self.eolmode == 'crlf':
                 eol = '\r\n'
@@ -508,7 +510,7 @@
                     if l[-1] != '\n':
                         yield "\n\ No newline at end of file\n"
 
-        self.writelines(fname, rejlines())
+        self.writelines(fname, rejlines(), strict=True)
 
     def apply(self, h):
         if not h.complete():
diff -r cec6140b0f3c -r 9f632a224f1a tests/test-mq-eol.t
--- a/tests/test-mq-eol.t	Thu Dec 02 18:14:04 2010 +0900
+++ b/tests/test-mq-eol.t	Sun Dec 05 21:19:19 2010 +0900
@@ -26,6 +26,22 @@
   > w('\ No newline at end of file\r\n')
   > EOF
 
+  $ cat > makepatch2.py <<EOF
+  > f = file('eol-auto.diff', 'wb')
+  > w = f.write
+  > w('test message\n')
+  > w('diff --git a/a b/a\n')
+  > w('--- a/a\n')
+  > w('+++ b/a\n')
+  > w('@@ -1,5 +1,5 @@\n')
+  > w(' a\r\n')
+  > w(' b\r\n')
+  > w('-c\r\n')
+  > w('+cc\r\n')
+  > w(' d\r\n')
+  > w(' e\r\n')
+  > EOF
+
   $ cat > cateol.py <<EOF
   > import sys
   > for line in file(sys.argv[1], 'rb'):
@@ -141,3 +157,71 @@
   $ hg qpop
   popping eol.diff
   patch queue now empty
+
+Another test case to check keeping original eol in .rej file in auto mode.
+If all the lines in hunk have CRLF line ending and eol mode is auto,
+patcher may detect as eol=CRLF. But it should not affects the .rej file.
+This test checks not making CRCRLF lines in such case.
+
+  $ python ../makepatch2.py
+  $ hg qimport eol-auto.diff
+  adding eol-auto.diff to series file
+  $ python -c 'file("a", "wb").write("a\r\nb\r\nC\r\nd\r\ne\r\n")'
+  $ hg ci -m tobefail a
+  $ hg --config 'patch.eol=LF' qpush
+  applying eol-auto.diff
+  patching file a
+  Hunk #1 FAILED at 0
+  1 out of 1 hunks FAILED -- saving rejects to file a.rej
+  patch failed, unable to continue (try -v)
+  patch failed, rejects left in working dir
+  errors during apply, please fix and refresh eol-auto.diff
+  [2]
+  $ hg qpop
+  popping eol-auto.diff
+  patch queue now empty
+  $ python ../cateol.py a
+  a<CR><LF>
+  b<CR><LF>
+  C<CR><LF>
+  d<CR><LF>
+  e<CR><LF>
+  $ python ../cateol.py a.rej
+  --- a<LF>
+  +++ a<LF>
+  @@ -1,5 +1,5 @@<LF>
+   a<CR><LF>
+   b<CR><LF>
+  -c<CR><LF>
+  +cc<CR><LF>
+   d<CR><LF>
+   e<CR><LF>
+
+  $ hg --config 'patch.eol=auto' qpush
+  applying eol-auto.diff
+  patching file a
+  Hunk #1 FAILED at 0
+  1 out of 1 hunks FAILED -- saving rejects to file a.rej
+  patch failed, unable to continue (try -v)
+  patch failed, rejects left in working dir
+  errors during apply, please fix and refresh eol-auto.diff
+  [2]
+  $ hg qpop
+  popping eol-auto.diff
+  patch queue now empty
+  $ python ../cateol.py a
+  a<CR><LF>
+  b<CR><LF>
+  C<CR><LF>
+  d<CR><LF>
+  e<CR><LF>
+  $ python ../cateol.py a.rej
+  --- a<LF>
+  +++ a<LF>
+  @@ -1,5 +1,5 @@<LF>
+   a<CR><LF>
+   b<CR><LF>
+  -c<CR><LF>
+  +cc<CR><LF>
+   d<CR><LF>
+   e<CR><LF>


More information about the Mercurial-devel mailing list