Skip to content

Commit ab06595

Browse files
committed
Refactor git_diff_context function to handle staged and unstaged files separately
1 parent 3d22ea5 commit ab06595

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

aicodebot/helpers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ def git_diff_context(commit=None):
1818
else:
1919
staged_files = exec_and_get_output(["git", "diff", "--cached", "--name-only"]).splitlines()
2020
if staged_files:
21-
file_status = exec_and_get_output(["git", "diff", "--cached", "--name-status"]).splitlines()
21+
# If there are staged files, get the diff for those files
22+
diff_type = "--cached"
2223
else:
23-
file_status = exec_and_get_output(["git", "diff", "--name-status"]).splitlines()
24+
diff_type = "HEAD"
25+
26+
file_status = exec_and_get_output(["git", "diff", diff_type, "--name-status"]).splitlines()
2427

2528
diffs = []
2629
for status in file_status:
@@ -33,7 +36,7 @@ def git_diff_context(commit=None):
3336
else:
3437
# If the file is not new, get the diff
3538
diffs.append(f"## File changed: {file_name}")
36-
diffs.append(exec_and_get_output(base_git_diff + ["--cached", "--", file_name]))
39+
diffs.append(exec_and_get_output(base_git_diff + [diff_type, "--", file_name]))
3740

3841
return "\n".join(diffs)
3942

tests/test_helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@ def test_git_diff_context(cli_runner, temp_git_repo):
2424
diff = git_diff_context()
2525
assert "Adding a new line." in diff
2626

27-
# Commit the changes
2827
repo = Repo(temp_git_repo.working_dir)
28+
29+
# Stage the changes
2930
repo.git.add("test.txt")
31+
diff = git_diff_context()
32+
assert "Adding a new line." in diff
33+
34+
# Commit the changes
3035
repo.git.commit("-m", "Add test.txt")
3136

3237
# Get the diff for the commit

0 commit comments

Comments
 (0)