Skip to content

Commit bfbdbdc

Browse files
committed
#5 Add option for fuzzy patch
Signed-off-by: Uilian Ries <uilianries@gmail.com>
1 parent e97774a commit bfbdbdc

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

patch_ng.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,9 +957,12 @@ def strip_path(self, path, base_path, strip=0):
957957

958958

959959

960-
def apply(self, strip=0, root=None):
960+
def apply(self, strip=0, root=None, fuzz=False):
961961
""" Apply parsed patch, optionally stripping leading components
962962
from file paths. `root` parameter specifies working dir.
963+
:param strip: Strip patch path
964+
:param root: Folder to apply the patch
965+
:param fuzz: Accept fuzzy patches
963966
return True on success
964967
"""
965968
items = []
@@ -1044,7 +1047,7 @@ def apply(self, strip=0, root=None):
10441047
raw_line = line.rstrip(b"\r\n")
10451048
patch_line = hunkfind[hunklineno]
10461049
if (raw_line == patch_line) or \
1047-
((not raw_line.startswith(b"+") and not patch_line.startswith(b"+")) and
1050+
(fuzz and (not raw_line.startswith(b"+") and not patch_line.startswith(b"+")) and
10481051
(not raw_line.startswith(b"-") and not patch_line.startswith(b"-"))):
10491052
hunklineno+=1
10501053
else:
@@ -1090,7 +1093,11 @@ def apply(self, strip=0, root=None):
10901093
if self._match_file_hunks(filenameo, p.hunks):
10911094
warning("already patched %s" % filenameo)
10921095
else:
1093-
warning("source file is different - %s" % filenameo)
1096+
if fuzz:
1097+
warning("source file is different - %s" % filenameo)
1098+
else:
1099+
error("source file is different - %s" % filenameo)
1100+
errors += 1
10941101
if canpatch:
10951102
backupname = filenamen+b".orig"
10961103
if exists(backupname):

tests/run_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ def test_fuzzy_patch(self):
428428
treeroot = join(self.tmpdir, 'rootparent')
429429
shutil.copytree(join(TESTS, '10fuzzy'), treeroot)
430430
pto = patch_ng.fromfile(join(TESTS, '10fuzzy/10fuzzy.patch'))
431-
self.assertTrue(pto.apply(root=treeroot))
431+
self.assertTrue(pto.apply(root=treeroot, fuzz=True))
432432

433433

434434
class TestHelpers(unittest.TestCase):

0 commit comments

Comments
 (0)