|  | 
| 3 | 3 | import re | 
| 4 | 4 | import tempfile | 
| 5 | 5 | from openai import OpenAI | 
| 6 |  | -import patch | 
|  | 6 | +import subprocess | 
| 7 | 7 | 
 | 
| 8 | 8 | INJECTION_PATTERNS = [ | 
| 9 | 9 |     r"ignore.*instructions", r"disregard.*above", r"assistant.*role", r"user.*role", | 
| @@ -122,21 +122,39 @@ def main(): | 
| 122 | 122 | 
 | 
| 123 | 123 | 
 | 
| 124 | 124 |     # Write the filtered diff to a temporary file | 
| 125 |  | -    with tempfile.NamedTemporaryFile(mode='w+', delete=False) as tmp_patch: | 
|  | 125 | +    with tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix=".patch") as tmp_patch: | 
| 126 | 126 |         tmp_patch.write(filtered_diff) | 
| 127 | 127 |         tmp_patch_path = tmp_patch.name | 
| 128 | 128 | 
 | 
| 129 |  | -    # Apply the patch | 
| 130 |  | -    pset = patch.fromfile(tmp_patch_path) | 
| 131 |  | -    if not pset: | 
| 132 |  | -        print("::error::Failed to parse generated patch.", file=sys.stderr) | 
|  | 129 | +    # Apply the patch using the patch command-line utility | 
|  | 130 | +    swift_file_abspath = os.path.abspath(swift_file) | 
|  | 131 | +    root_dir = os.path.dirname(swift_file_abspath) | 
|  | 132 | + | 
|  | 133 | +    patch_command = ['patch', '-p0', '--input', tmp_patch_path] | 
|  | 134 | +    print(f"Running patch command: {' '.join(patch_command)} in directory {root_dir}") | 
|  | 135 | + | 
|  | 136 | +    try: | 
|  | 137 | +        result = subprocess.run( | 
|  | 138 | +            patch_command, | 
|  | 139 | +            cwd=root_dir, | 
|  | 140 | +            check=True, | 
|  | 141 | +            capture_output=True, | 
|  | 142 | +            text=True | 
|  | 143 | +        ) | 
|  | 144 | +        print("Patch applied successfully using command!") | 
|  | 145 | +    except subprocess.CalledProcessError as e: | 
|  | 146 | +        print(f"::error::Failed to apply patch using command: {' '.join(patch_command)}", file=sys.stderr) | 
|  | 147 | +        print(f"::error::Return code: {e.returncode}", file=sys.stderr) | 
|  | 148 | +        print(f"::error::stdout: {e.stdout}", file=sys.stderr) | 
|  | 149 | +        print(f"::error::stderr: {e.stderr}", file=sys.stderr) | 
|  | 150 | +        print("::error::Patch content was:", file=sys.stderr) | 
|  | 151 | +        print(filtered_diff, file=sys.stderr) | 
| 133 | 152 |         sys.exit(1) | 
| 134 |  | -    root_dir = os.path.dirname(os.path.abspath(swift_file)) | 
| 135 |  | -    if pset.apply(root=root_dir): | 
| 136 |  | -        print("Patch applied successfully!") | 
| 137 |  | -    else: | 
| 138 |  | -        print("::error::Failed to apply patch.", file=sys.stderr) | 
|  | 153 | +    except FileNotFoundError: | 
|  | 154 | +        print("::error::'patch' command not found. Is it installed in the container?", file=sys.stderr) | 
| 139 | 155 |         sys.exit(1) | 
|  | 156 | +    finally: | 
|  | 157 | +        os.remove(tmp_patch_path) | 
| 140 | 158 | 
 | 
| 141 | 159 | if __name__ == "__main__": | 
| 142 | 160 |     main() | 
0 commit comments