spatch

Lenient universal diff patcher
git clone https://git.sinitax.com/sinitax/spatch
Log | Files | Refs | LICENSE | sfeed.txt

commit 30a754d8c211a76539f1a742d6a5b008c02e8678
parent d07c0783c50c36751bdc7237ab495a890d04a8bd
Author: Louis Burda <quent.burda@gmail.com>
Date:   Fri, 11 Sep 2020 19:36:53 +0200

improved matching of comments and single replace

Diffstat:
ATODO | 2++
Mpatch.py | 21++++++++++++++++-----
2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/TODO b/TODO @@ -0,0 +1,2 @@ +add handling of lines with: +\ No newline at end of file diff --git a/patch.py b/patch.py @@ -3,8 +3,8 @@ import sys, os, re file_header = """\ ---- (.*) -\+\+\+ (.*) +--- ([^\n ]*)( [^\n]*)? +\+\+\+ ([^\n ]*)( [^\n]*)? """ file_header_pattern = re.compile(file_header) @@ -55,7 +55,12 @@ def patch_file(src_filename, dst_filename, content): src_lines = "\n".join([l[1:] for l in chunk_content if l[0] in (' ', '-')]) dst_lines = "\n".join([l[1:] for l in chunk_content if l[0] in (' ', '+')]) - src_content = src_content.replace(src_lines, dst_lines) + try: + replace_start = src_content.index(src_lines) + src_content = src_content[:replace_start] + dst_lines + src_content[replace_start+len(src_lines):] + except Exception as e: + print("[ERROR] Failed to find corresponding lines for chunk, exiting..") + sys.exit(1) open(dst_filename, "w+").write(src_content) @@ -78,11 +83,17 @@ def main(): header_matches.append(prev_header_match) prev_header_match = next_header_match + print("[GLOBAL] Processing diff file '{}'".format(diff_file)) print("[GLOBAL] Found {} file patch headers..".format(len(header_matches))) for i in range(len(header_matches)): - src_file = header_matches[i].group(1) - dst_file = header_matches[i].group(2) + if len(header_matches[i].groups()) == 4: + src_file = header_matches[i].group(1) + dst_file = header_matches[i].group(3) + else: + src_file = header_matches[i].group(1) + dst_file = header_matches[i].group(2) + print("[PATCH] Applying patch from {} to {}".format(src_file, dst_file)) startpos = header_matches[i].span()[1]