|
3 | 3 | from pathlib import Path |
4 | 4 | from typing import Any, Optional |
5 | 5 |
|
| 6 | +from git import Repo, InvalidGitRepositoryError |
6 | 7 | from openai.types.chat import ChatCompletionMessageParam |
7 | 8 |
|
8 | 9 | from patchwork.common.client.llm.aio import AioLlmClient |
@@ -111,6 +112,14 @@ def __init__(self, inputs): |
111 | 112 | self.base_path = str(Path(str(base_path)).resolve()) |
112 | 113 | else: |
113 | 114 | self.base_path = str(Path.cwd()) |
| 115 | + |
| 116 | + # Check if we're in a git repository |
| 117 | + try: |
| 118 | + self.repo = Repo(self.base_path, search_parent_directories=True) |
| 119 | + self.is_git_repo = True |
| 120 | + except (InvalidGitRepositoryError, Exception): |
| 121 | + self.repo = None |
| 122 | + self.is_git_repo = False |
114 | 123 |
|
115 | 124 | llm_client = AioLlmClient.create_aio_client(inputs) |
116 | 125 | if llm_client is None: |
@@ -146,51 +155,34 @@ def run(self): |
146 | 155 | if isinstance(tool, CodeEditTool): |
147 | 156 | cwd = Path.cwd() |
148 | 157 | modified_files = [file_path.relative_to(cwd) for file_path in tool.tool_records["modified_files"]] |
149 | | - # Generate diffs for modified files using in-memory comparison |
| 158 | + # Generate diffs for modified files |
150 | 159 | modified_files_with_diffs = [] |
151 | | - file_contents = {} # Store original contents before modifications |
152 | 160 |
|
153 | | - # First pass: store original contents |
154 | 161 | for file in modified_files: |
155 | 162 | file_path = Path(file) |
156 | | - try: |
157 | | - if file_path.exists(): |
158 | | - file_contents[str(file)] = file_path.read_text() |
159 | | - else: |
160 | | - file_contents[str(file)] = "" |
161 | | - except (OSError, IOError) as e: |
162 | | - print(f"Warning: Failed to read original content for {file}: {str(e)}") |
163 | | - file_contents[str(file)] = "" |
164 | | - |
165 | | - # Apply modifications through CodeEditTool (happens in the background) |
166 | | - |
167 | | - # Second pass: generate diffs |
168 | | - for file in modified_files: |
169 | | - file_path = Path(file) |
170 | | - try: |
171 | | - # Get current content after modifications |
172 | | - current_content = file_path.read_text() if file_path.exists() else "" |
173 | | - original_content = file_contents.get(str(file), "") |
174 | | - |
175 | | - # Generate unified diff |
176 | | - fromfile = f"a/{file}" |
177 | | - tofile = f"b/{file}" |
178 | | - diff = "".join(difflib.unified_diff( |
179 | | - original_content.splitlines(keepends=True), |
180 | | - current_content.splitlines(keepends=True), |
181 | | - fromfile=fromfile, |
182 | | - tofile=tofile |
183 | | - )) |
184 | | - |
185 | | - if diff: # Only add if there are actual changes |
186 | | - modified_file = { |
187 | | - "path": str(file), |
188 | | - "diff": diff |
189 | | - } |
190 | | - modified_files_with_diffs.append(modified_file) |
191 | | - except (OSError, IOError) as e: |
192 | | - print(f"Warning: Failed to generate diff for {file}: {str(e)}") |
193 | | - continue |
| 163 | + modified_file = { |
| 164 | + "path": str(file), |
| 165 | + "diff": "" # Default to empty string as requested |
| 166 | + } |
| 167 | + |
| 168 | + # Only try to generate git diff if we're in a git repository |
| 169 | + if self.is_git_repo and self.repo is not None: |
| 170 | + try: |
| 171 | + # Check if file exists and is tracked by git |
| 172 | + if file_path.exists(): |
| 173 | + try: |
| 174 | + # Try to get the diff using git |
| 175 | + diff = self.repo.git.diff('HEAD', str(file)) |
| 176 | + if diff: # Only update if we got a diff |
| 177 | + modified_file["diff"] = diff |
| 178 | + except Exception as e: |
| 179 | + # Git-specific errors (untracked files, etc) - keep empty diff |
| 180 | + print(f"Note: Could not get git diff for {file}: {str(e)}") |
| 181 | + except Exception as e: |
| 182 | + # General file processing errors |
| 183 | + print(f"Warning: Failed to process file {file}: {str(e)}") |
| 184 | + |
| 185 | + modified_files_with_diffs.append(modified_file) |
194 | 186 |
|
195 | 187 | return dict(modified_files=modified_files_with_diffs) |
196 | 188 | return dict() |
0 commit comments