|
1 | 1 | #! /usr/bin/env python3 |
2 | 2 | import sys |
3 | 3 | import argparse |
4 | | -import re |
5 | 4 | from pathlib import Path |
6 | 5 |
|
7 | 6 | from fastmcp import FastMCP |
@@ -72,24 +71,27 @@ def patch_file( |
72 | 71 | if not any(str(pp).startswith(base) for base in allowed_directories): |
73 | 72 | raise PermissionError(f"File {file_path} is not in allowed directories") |
74 | 73 |
|
75 | | - # Extract all hunks (sections starting with @@) |
76 | | - # First try to find all hunks in the patch content |
77 | | - hunks = re.findall(r'@@[^@]*(?:\n(?!@@)[^\n]*)*', patch_content) |
| 74 | + # Extract just the hunk part (starting with @@) |
| 75 | + lines = patch_content.splitlines() |
| 76 | + hunk_start = -1 |
78 | 77 |
|
79 | | - if not hunks: |
80 | | - # If no complete hunks found, check if the patch itself is a single hunk |
81 | | - if patch_content.strip().startswith("@@"): |
82 | | - hunks = [patch_content.strip()] |
83 | | - else: |
84 | | - raise RuntimeError( |
85 | | - "No valid patch hunks found. Make sure the patch contains @@ line markers.\n" |
86 | | - "You can use `write_file` tool to write the whole file content instead." |
87 | | - ) |
| 78 | + for i, line in enumerate(lines): |
| 79 | + if line.startswith("@@"): |
| 80 | + hunk_start = i |
| 81 | + break |
88 | 82 |
|
89 | | - # Join all hunks and create a standardized patch with proper headers |
90 | | - hunks_content = '\n'.join(hunks) |
| 83 | + if hunk_start == -1: |
| 84 | + raise RuntimeError( |
| 85 | + "No @@ line markers found in the patch content.\n" |
| 86 | + "Make sure the patch follows the unified diff format with @@ line markers." |
| 87 | + ) |
| 88 | + |
| 89 | + # Use only the hunk part (remove any headers) |
| 90 | + hunk_content = "\n".join(lines[hunk_start:]) |
| 91 | + |
| 92 | + # Create a standardized patch with the correct filename |
91 | 93 | filename = pp.name |
92 | | - standardized_patch = f"--- {filename}\n+++ {filename}\n{hunks_content}" |
| 94 | + standardized_patch = f"--- {filename}\n+++ {filename}\n{hunk_content}" |
93 | 95 | eprint(f"Created standardized patch for {filename}") |
94 | 96 |
|
95 | 97 | # Ensure patch_content is properly encoded |
|
0 commit comments