Skip to content

Commit f0ec280

Browse files
feat: use git diff when in git repo, empty string otherwise
Co-Authored-By: Patched <[email protected]>
1 parent 278fde2 commit f0ec280

File tree

1 file changed

+33
-41
lines changed

1 file changed

+33
-41
lines changed

patchwork/steps/FixIssue/FixIssue.py

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from pathlib import Path
44
from typing import Any, Optional
55

6+
from git import Repo, InvalidGitRepositoryError
67
from openai.types.chat import ChatCompletionMessageParam
78

89
from patchwork.common.client.llm.aio import AioLlmClient
@@ -111,6 +112,14 @@ def __init__(self, inputs):
111112
self.base_path = str(Path(str(base_path)).resolve())
112113
else:
113114
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
114123

115124
llm_client = AioLlmClient.create_aio_client(inputs)
116125
if llm_client is None:
@@ -146,51 +155,34 @@ def run(self):
146155
if isinstance(tool, CodeEditTool):
147156
cwd = Path.cwd()
148157
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
150159
modified_files_with_diffs = []
151-
file_contents = {} # Store original contents before modifications
152160

153-
# First pass: store original contents
154161
for file in modified_files:
155162
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)
194186

195187
return dict(modified_files=modified_files_with_diffs)
196188
return dict()

0 commit comments

Comments
 (0)