python-patch icon indicating copy to clipboard operation
python-patch copied to clipboard

Unable to apply patches with trailing blank line removed

Open fmarier opened this issue 6 years ago • 2 comments

Given a.txt:

1
2
3

and broken.patch (note that it's missing the third line of context):

--- a.txt	2019-03-28 17:02:04.832004033 -0700
+++ b.txt	2019-03-28 17:02:34.999576803 -0700
@@ -1,4 +1,4 @@
-1
+0
 2
 3

one can get to b.txt:

0
2
3

using patch -p0 < broken.patch but not with python-patch:

$ ./patch.py --debug broken.patch 
   DEBUG reading broken.patch
 WARNING error: patch stream is incomplete!
   DEBUG -  0 hunks for a.txt
   DEBUG total files: 1  total hunks: 0
   DEBUG normalize filenames
   DEBUG     patch type = plain
   DEBUG     source = a.txt
   DEBUG     target = b.txt
Traceback (most recent call last):
  File "./patch.py", line 1193, in <module>
    main()
  File "./patch.py", line 1186, in main
    patch.apply(options.strip, root=options.directory) or sys.exit(-1)
AttributeError: 'bool' object has no attribute 'apply'

To patch a.txt correctly using python-patch, one needs to provide the non-truncated patch file which contains the full three lines of context (with the last line consisting of a single trailing space):

--- a.txt	2019-03-28 17:02:04.832004033 -0700
+++ b.txt	2019-03-28 17:02:34.999576803 -0700
@@ -1,4 +1,4 @@
-1
+0
 2
 3
 

This can be a problem since git is commonly configured to remove "unnecessary trailing whitespace" via the following option in ~/.gitconfig:

[apply]
        whitespace = fix

fmarier avatar Mar 29 '19 00:03 fmarier

Had the same issue, very annoying as this lib is now used with conan package manager

swissembedded avatar Jun 25 '19 14:06 swissembedded

How can I reproduce it with git locally? Ideally on this repo.

techtonik avatar Jan 01 '21 15:01 techtonik