techtonik / python-patch

Library to parse and apply unified diffs
https://pypi.python.org/pypi/patch
106 stars 63 forks source link

First context line is discarded from hunk text if it is at the end andon the same line as the hunk "@@ <...> @@" line #22

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What version of the product are you using?  HEAD r158

What steps will reproduce the problem?

1.Use this patch (attached too):

See how the first context line is on the hunk header line
{{{
Index: linux-2.6.23/fs/Makefile
===================================================================
--- linux-2.6.23.orig/fs/Makefile
+++ linux-2.6.23/fs/Makefile
@@ -72,7 +72,6 @@ obj-$(CONFIG_JBD)     += jbd/
 obj-$(CONFIG_JBD2)     += jbd2/
 obj-$(CONFIG_EXT2_FS)      += ext2/
 obj-$(CONFIG_CRAMFS)       += cramfs/
-obj-$(CONFIG_SQUASHFS)     += squashfs/
 obj-$(CONFIG_RAMFS)        += ramfs/
 obj-$(CONFIG_HUGETLBFS)        += hugetlbfs/
 obj-$(CONFIG_CODA_FS)      += coda/

}}}

2. Then run this:
{{{
# python -c "import patch;p=patch.fromfile('sample.patch');from pprint import 
pprint as pp;pp( p.items[0].hunks[0].text)"
[' obj-$(CONFIG_JBD2)\t\t+= jbd2/\r\n',
 ' obj-$(CONFIG_EXT2_FS)\t\t+= ext2/\r\n',
 ' obj-$(CONFIG_CRAMFS)\t\t+= cramfs/\r\n',
 '-obj-$(CONFIG_SQUASHFS)\t\t+= squashfs/\r\n',
 ' obj-$(CONFIG_RAMFS)\t\t+= ramfs/\r\n',
 ' obj-$(CONFIG_HUGETLBFS)\t\t+= hugetlbfs/\r\n',
 ' obj-$(CONFIG_CODA_FS)\t\t+= coda/']
}}}

3. The expected results here should be:
{{{
[' obj-$(CONFIG_JBD)\t\t+= jbd/\r\n',
 ' obj-$(CONFIG_JBD2)\t\t+= jbd2/\r\n',
 ' obj-$(CONFIG_EXT2_FS)\t\t+= ext2/\r\n',
 ' obj-$(CONFIG_CRAMFS)\t\t+= cramfs/\r\n',
 '-obj-$(CONFIG_SQUASHFS)\t\t+= squashfs/\r\n',
 ' obj-$(CONFIG_RAMFS)\t\t+= ramfs/\r\n',
 ' obj-$(CONFIG_HUGETLBFS)\t\t+= hugetlbfs/\r\n',
 ' obj-$(CONFIG_CODA_FS)\t\t+= coda/']

}}}
Note that I have no idea how the patch was originally produced. It could have 
been manually edited and could be incorrect. I do not know either if that extra 
line would be used by GNU patch as context line or also discarded.

Original issue reported on code.google.com by pombreda...@nexb.com on 22 Aug 2012 at 1:42

Attachments:

GoogleCodeExporter commented 9 years ago
The patch is not valid with this extra line inserted. Without it it's a correct 
diff with 3 lines of context. I wonder if patch.py should complain if there is 
a garbage on the hunk line..

Original comment by techtonik@gmail.com on 22 Nov 2012 at 7:07

GoogleCodeExporter commented 9 years ago
Actually this sting is documented "optional section heading" 
http://en.wikipedia.org/wiki/Diff#Unified_format

diff command places here the name of the function where chunk is contained when 
given -p argument on the command line.

Original comment by techtonik@gmail.com on 2 Dec 2012 at 10:47

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r189.

Original comment by techtonik@gmail.com on 2 Dec 2012 at 1:14