Skip to content

Commit 5c27b8c

Browse files
committed
Enhance git_diff_context to handle file renaming and deletion
This commit enhances the `git_diff_context` function in `aicodebot/helpers.py` to handle file renaming and deletion. Previously, the function only handled new and modified files. Now, it can also handle renamed and deleted files. The changes include: - Splitting the status code and file name(s) into separate variables. - Adding conditions to handle different status codes: 'A' for new files, 'R' for renamed files, and 'D' for deleted files. - Updating the `tests/test_helpers.py` to include tests for the new functionality. This enhancement provides a more comprehensive overview of changes in the git repository.
1 parent 6e6c121 commit 5c27b8c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

aicodebot/helpers.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,26 @@ def git_diff_context(commit=None):
2929

3030
diffs = []
3131
for status in file_status:
32-
status_code, file_name = status.split("\t")
32+
status_parts = status.split("\t")
33+
status_code = status_parts[0][0] # Get the first character of the status code
3334
if status_code == "A":
3435
# If the file is new, include the entire file content
36+
file_name = status_parts[1]
3537
contents = Path(file_name).read_text()
3638
diffs.append(f"## New file added: {file_name}")
3739
diffs.append(contents)
40+
elif status_code == "R":
41+
# If the file is renamed, get the diff and note the old and new names
42+
old_file_name, new_file_name = status_parts[1], status_parts[2]
43+
diffs.append(f"## File renamed: {old_file_name} -> {new_file_name}")
44+
diffs.append(exec_and_get_output(base_git_diff + [diff_type, "--", new_file_name]))
45+
elif status_code == "D":
46+
# If the file is deleted, note the deletion
47+
file_name = status_parts[1]
48+
diffs.append(f"## File deleted: {file_name}")
3849
else:
39-
# If the file is not new, get the diff
50+
# If the file is not new, renamed, or deleted, get the diff
51+
file_name = status_parts[1]
4052
diffs.append(f"## File changed: {file_name}")
4153
diffs.append(exec_and_get_output(base_git_diff + [diff_type, "--", file_name]))
4254

tests/test_helpers.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,25 @@ def test_git_diff_context(temp_git_repo):
6161
diff = git_diff_context()
6262
assert diff == ""
6363

64+
# Rename the file but don't stage it
65+
temp_git_repo.git.mv("newfile.txt", "renamedfile.txt")
66+
diff = git_diff_context()
67+
assert "## File renamed: newfile.txt -> renamedfile.txt" in diff
68+
69+
# Stage the renamed file
70+
temp_git_repo.git.add("renamedfile.txt")
71+
diff = git_diff_context()
72+
assert "## File renamed: newfile.txt -> renamedfile.txt" in diff
73+
74+
# Commit the renamed file
75+
temp_git_repo.git.commit("-m", "Rename newfile.txt to renamedfile.txt")
76+
diff = git_diff_context()
77+
assert diff == ""
78+
6479
# Test diff for a specific commit
6580
commit = temp_git_repo.head.commit.hexsha
6681
diff = git_diff_context(commit)
67-
assert "This is a modified file." in diff
82+
assert "renamedfile.txt" in diff
6883

6984

7085
def test_exec_and_get_output_success():

0 commit comments

Comments
 (0)