spatch

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

commit d07c0783c50c36751bdc7237ab495a890d04a8bd
parent b327077fe17e11f880d737b14c0c5cefd53df4c8
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sun, 23 Aug 2020 12:47:25 +0200

removed testing files and added installer

Diffstat:
D2 | 97-------------------------------------------------------------------------------
D_input | 3---
Dinput | 5-----
Ainstall | 3+++
Mpatch.py | 2++
Dtest | 13-------------
6 files changed, 5 insertions(+), 118 deletions(-)

diff --git a/2 b/2 @@ -1,97 +0,0 @@ -import sys, os, re - -file_header = """\ ---- (.*) -\+\+\+ (.*) -""" -file_header_pattern = re.compile(file_header) - -chunk_header = "@@ -([0-9]*),([0-9]*) \+([0-9]*),([0-9]*) @@(.*)\n"; -chunk_header_pattern = re.compile(chunk_header) - -def patch_file(src_filename, dst_filename, content): - prev_header_match = chunk_header_pattern.search(content) - if prev_header_match == None: - print("[CHUNK] No chunks found, skipping.") - return - - src_content = open(src_filename, "r").read() - - chunks = list() - next_header_match = True # for do_while loop - while (next_header_match): - next_header_match = chunk_header_pattern.search(content, prev_header_match.span()[1]) - chunks.append(prev_header_match) - prev_header_match = next_header_match - - for i,c in enumerate(chunks): - src_line = c.group(1) - src_count = c.group(2) - dst_line = c.group(3) - dst_count = c.group(4) - comment = c.group(5).strip() - - if comment != "": - print("[CHUNK] Applying chunk with comment: {}".format(comment)) - else: - print("[CHUNK] Applying chunk at line: {}".format(src_line)) - - start_pos = c.span()[1] - if i != len(chunks) - 1: - end_pos = chunks[i + 1].span()[0] - else: - end_pos = len(content) - - chunk_content = content[start_pos:end_pos].split("\n") - valid_lines = 0 - for l in chunk_content: - if len(l) == 0 or l[0] not in (' ', '+', '-'): - break - valid_lines += 1 - chunk_content = chunk_content[:valid_lines] - - src_lines = "\n".join([l for l in chunk_content if l[0] in (' ', '-')]) - dst_lines = "\n".join([l for l in chunk_content if l[0] in (' ', '+')]) - - src_content.replace(src_lines, dst_lines) - - open(dst_filename, "w+").write(src_content) - -def main(): - if len(sys.argv) < 2: - print("Supply the path of a unified diff file as argument") - return 1 - - diff_file = sys.argv[1] - content = open(diff_file).read() - - prev_header_match = file_header_pattern.search(content) - if prev_header_match == None: - print("Not a unified diff file!") - return 1 - header_matches = list() - next_header_match = True # for do_while loop - while (next_header_match): - next_header_match = file_header_pattern.search(content, prev_header_match.span()[1]) - header_matches.append(prev_header_match) - prev_header_match = next_header_match - - 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) - print("[PATCH] Applying patch from {} to {}".format(src_file, dst_file)) - - startpos = header_matches[i].span()[1] - if i == len(header_matches) - 1: - endpos = len(content) - else: - endpos = header_matches[i + 1].span()[0] - - patch_file(src_file, dst_file, content[startpos:endpos]) - - return 0 - -if __name__ == "__main__": - sys.exit(main()) diff --git a/_input b/_input @@ -1,3 +0,0 @@ -one -two -three diff --git a/input b/input @@ -1,5 +0,0 @@ -one -two -hahaha gotem -hahaha gotem -three diff --git a/install b/install @@ -0,0 +1,3 @@ +#!/bin/bash + +sudo ln -sf $PWD/patch.py /usr/bin/spatch diff --git a/patch.py b/patch.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python3 + import sys, os, re file_header = """\ diff --git a/test b/test @@ -1,13 +0,0 @@ ---- input -+++ input - -@@ -1,1 +1,2 @@ bla bla bla - one - two -+hahaha gotem - ---- input -+++ input - - -