techtonik / python-patch

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

Support patching strings #1

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
At the moment, only file patching is implemented.  Refactor to allow string
patching.

Original issue reported on code.google.com by sjvdw...@gmail.com on 25 Jul 2008 at 1:36

GoogleCodeExporter commented 9 years ago
python-patch is a great idea. After struggling with the resolve function in the
difflib module i found this - thankfully.

Ideally a function 'patch' should be added to difflib and a new script 
'patch.py'
created which calls difflib.patch() and should accompany the 
Tools/scripts/diff.py
script.

Original comment by kevinc1...@gmail.com on 24 Mar 2009 at 4:49

GoogleCodeExporter commented 9 years ago
I also think it is great idea. =)
http://bugs.python.org/issue2057

Original comment by techtonik@gmail.com on 25 Mar 2009 at 7:36

GoogleCodeExporter commented 9 years ago
lol - you've nailed it!

Original comment by kevinc1...@gmail.com on 25 Mar 2009 at 4:59

GoogleCodeExporter commented 9 years ago
About patching strings. There is a low-level internal API that applies hunks to 
stream, but it is not perfect, because you need to get hunks and apply them 
explicitly. Patch file usually contain modifications for several files, each is 
patched with a set of hunks and if you do not want to think about hunks - you 
still 
need to select which source file your string represents to select appropriate 
hunk 
set.

So, the API coud be like:
{{{
import patch
p = patch.fromfile("my.patch")

h = p.gethunks(filename="filename")
# or p.gethunks(index=0)
outstr = p.patchstream("my.file", h)
}}}

or it is also possible to add more high-level FileInfo() class.

{{{
import patch
p = patch.fromfile("my.patch")

files = p.getfiles()
output = files[0].patchstring(input)
}}}

Original comment by techtonik@gmail.com on 25 Aug 2009 at 2:36

graingert commented 7 years ago

@adamchainz ^