| 34 | |
| 35 | |
| 36 | def apply_diff(original: Path, diff_file: Path): |
| 37 | result = subprocess.run( |
| 38 | ["patch", "-N", str(original), str(diff_file)], |
| 39 | stdout=subprocess.PIPE, |
| 40 | stderr=subprocess.PIPE, |
| 41 | text=True |
| 42 | ) |
| 43 | |
| 44 | if result.returncode == 0: |
| 45 | return |
| 46 | elif result.returncode == 1: |
| 47 | combined_output = (result.stdout + result.stderr).lower() |
| 48 | if ("reversed (or previously applied) patch detected" in combined_output or |
| 49 | "skip" in combined_output or |
| 50 | "ignoring" in combined_output): |
| 51 | # its fine |
| 52 | return |
| 53 | else: |
| 54 | raise RuntimeError(f"Patch failed with output:\n{result.stdout}\n{result.stderr}") |
| 55 | else: |
| 56 | raise RuntimeError(f"Patch command failed with code {result.returncode}:\n{result.stdout}\n{result.stderr}") |
| 57 | |
| 58 | |
| 59 | |