|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Rewrite git patches to strip python/ta/ prefix from all paths. |
| 3 | +
|
| 4 | +This tool is used to migrate commits from the TypeAgent monorepo to the |
| 5 | +standalone python/ta repository. It takes git format-patch output files |
| 6 | +and rewrites all path references to remove the python/ta/ prefix. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + # Generate patches in the TypeAgent repo |
| 10 | + git format-patch <since>..HEAD --output-directory=/tmp/patches -- python/ta/ |
| 11 | + |
| 12 | + # Rewrite the patches |
| 13 | + python3 tools/rewrite-patches.py /tmp/patches/*.patch |
| 14 | + |
| 15 | + # Apply in the new repo |
| 16 | + cd /path/to/new-repo |
| 17 | + git am /tmp/patches/*.patch |
| 18 | +""" |
| 19 | + |
| 20 | +import re |
| 21 | +import sys |
| 22 | +from pathlib import Path |
| 23 | + |
| 24 | + |
| 25 | +def rewrite_patch(patch_content: str) -> str: |
| 26 | + """Rewrite paths in a git patch to strip python/ta/ prefix. |
| 27 | + |
| 28 | + Args: |
| 29 | + patch_content: The content of a git format-patch output file |
| 30 | + |
| 31 | + Returns: |
| 32 | + The rewritten patch content with python/ta/ stripped from all paths |
| 33 | + """ |
| 34 | + lines = patch_content.split('\n') |
| 35 | + output = [] |
| 36 | + |
| 37 | + for line in lines: |
| 38 | + # Handle diff headers (e.g., "diff --git a/python/ta/foo.py b/python/ta/foo.py") |
| 39 | + if line.startswith('diff --git '): |
| 40 | + line = line.replace(' a/python/ta/', ' a/') |
| 41 | + line = line.replace(' b/python/ta/', ' b/') |
| 42 | + |
| 43 | + # Handle file path headers in unified diff format |
| 44 | + elif line.startswith('--- '): |
| 45 | + if line.startswith('--- a/python/ta/'): |
| 46 | + line = line.replace('--- a/python/ta/', '--- a/') |
| 47 | + elif line == '--- /dev/null': |
| 48 | + pass # Leave /dev/null unchanged |
| 49 | + |
| 50 | + elif line.startswith('+++ '): |
| 51 | + if line.startswith('+++ b/python/ta/'): |
| 52 | + line = line.replace('+++ b/python/ta/', '+++ b/') |
| 53 | + elif line == '+++ /dev/null': |
| 54 | + pass # Leave /dev/null unchanged |
| 55 | + |
| 56 | + # Handle rename/copy operations |
| 57 | + elif line.startswith('rename from '): |
| 58 | + line = line.replace('rename from python/ta/', 'rename from ') |
| 59 | + elif line.startswith('rename to '): |
| 60 | + line = line.replace('rename to python/ta/', 'rename to ') |
| 61 | + elif line.startswith('copy from '): |
| 62 | + line = line.replace('copy from python/ta/', 'copy from ') |
| 63 | + elif line.startswith('copy to '): |
| 64 | + line = line.replace('copy to python/ta/', 'copy to ') |
| 65 | + |
| 66 | + # Handle similarity index for renames (no path changes needed) |
| 67 | + # Handle index lines (no path changes needed) |
| 68 | + # Handle new/deleted file mode (no path changes needed) |
| 69 | + |
| 70 | + output.append(line) |
| 71 | + |
| 72 | + return '\n'.join(output) |
| 73 | + |
| 74 | + |
| 75 | +def main(): |
| 76 | + """Main entry point for the patch rewriter.""" |
| 77 | + if len(sys.argv) < 2: |
| 78 | + print("Usage: rewrite-patches.py <patch-file> [patch-file ...]", file=sys.stderr) |
| 79 | + print("\nRewrite git format-patch files to strip python/ta/ prefix from paths.", file=sys.stderr) |
| 80 | + print("\nExample:", file=sys.stderr) |
| 81 | + print(" git format-patch abc123..HEAD --output-directory=/tmp/patches -- python/ta/", file=sys.stderr) |
| 82 | + print(" python3 tools/rewrite-patches.py /tmp/patches/*.patch", file=sys.stderr) |
| 83 | + sys.exit(1) |
| 84 | + |
| 85 | + patch_files = sys.argv[1:] |
| 86 | + success_count = 0 |
| 87 | + error_count = 0 |
| 88 | + |
| 89 | + for patch_file in patch_files: |
| 90 | + try: |
| 91 | + path = Path(patch_file) |
| 92 | + if not path.exists(): |
| 93 | + print(f"Error: File not found: {patch_file}", file=sys.stderr) |
| 94 | + error_count += 1 |
| 95 | + continue |
| 96 | + |
| 97 | + if not path.is_file(): |
| 98 | + print(f"Error: Not a file: {patch_file}", file=sys.stderr) |
| 99 | + error_count += 1 |
| 100 | + continue |
| 101 | + |
| 102 | + # Read the original patch |
| 103 | + content = path.read_text(encoding='utf-8') |
| 104 | + |
| 105 | + # Rewrite paths |
| 106 | + rewritten = rewrite_patch(content) |
| 107 | + |
| 108 | + # Write back to the same file |
| 109 | + path.write_text(rewritten, encoding='utf-8') |
| 110 | + |
| 111 | + print(f"✓ Rewrote {patch_file}") |
| 112 | + success_count += 1 |
| 113 | + |
| 114 | + except Exception as e: |
| 115 | + print(f"Error processing {patch_file}: {e}", file=sys.stderr) |
| 116 | + error_count += 1 |
| 117 | + |
| 118 | + # Print summary |
| 119 | + print(f"\nProcessed {success_count + error_count} files: {success_count} successful, {error_count} errors") |
| 120 | + |
| 121 | + if error_count > 0: |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == '__main__': |
| 126 | + main() |
0 commit comments