feat(patch): Add patch application and backup functions

- Add create_backup() to backup files before patching
- Add apply_patch_to_file() to apply unified diff
- Add restore_from_backup() for rollback
- Add cleanup_backup() to remove backup files
- Add 15 unit tests for all patch operations
This commit is contained in:
2026-03-07 00:14:23 -05:00
parent 99a5d7045f
commit 125cbc6dd0
2 changed files with 152 additions and 4 deletions

View File

@@ -1,5 +1,8 @@
from typing import List, Dict, Optional
from typing import List, Dict, Optional, Tuple
from dataclasses import dataclass
import shutil
import os
from pathlib import Path
@dataclass
class DiffHunk:
@@ -146,4 +149,72 @@ def render_diff_text_immediate(diff_files: List[DiffFile]) -> List[tuple[str, Op
for line in hunk.lines:
color = get_line_color(line)
output.append((line, color))
return output
return output
def create_backup(file_path: str) -> Optional[str]:
path = Path(file_path)
if not path.exists():
return None
backup_path = path.with_suffix(path.suffix + ".backup")
shutil.copy2(path, backup_path)
return str(backup_path)
def apply_patch_to_file(patch_text: str, base_dir: str = ".") -> Tuple[bool, str]:
import difflib
diff_files = parse_diff(patch_text)
if not diff_files:
return False, "No valid diff found"
results = []
for df in diff_files:
file_path = Path(base_dir) / df.old_path
if not file_path.exists():
results.append(f"File not found: {file_path}")
continue
try:
with open(file_path, "r", encoding="utf-8") as f:
original_lines = f.read().splitlines(keepends=True)
new_lines = original_lines.copy()
offset = 0
for hunk in df.hunks:
hunk_old_start = hunk.old_start - 1
hunk_old_count = hunk.old_count
replace_start = hunk_old_start + offset
replace_count = hunk_old_count
hunk_new_content: List[str] = []
for line in hunk.lines:
if line.startswith("+") and not line.startswith("+++"):
hunk_new_content.append(line[1:] + "\n")
elif line.startswith(" ") or (line and not line.startswith(("-", "+", "@@"))):
hunk_new_content.append(line + "\n")
new_lines = new_lines[:replace_start] + hunk_new_content + new_lines[replace_start + replace_count:]
offset += len(hunk_new_content) - replace_count
with open(file_path, "w", encoding="utf-8", newline="") as f:
f.writelines(new_lines)
results.append(f"Patched: {file_path}")
except Exception as e:
return False, f"Error patching {file_path}: {e}"
return True, "\n".join(results)
def restore_from_backup(file_path: str) -> bool:
backup_path = Path(str(file_path) + ".backup")
if not backup_path.exists():
return False
shutil.copy2(backup_path, file_path)
return True
def cleanup_backup(file_path: str) -> None:
backup_path = Path(str(file_path) + ".backup")
if backup_path.exists():
backup_path.unlink()