Skip to content

Commit

Permalink
Enhance git_diff_context to handle file renaming and deletion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TechNickAI committed Jul 3, 2023
1 parent 6e6c121 commit 5c27b8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
16 changes: 14 additions & 2 deletions aicodebot/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,26 @@ def git_diff_context(commit=None):

diffs = []
for status in file_status:
status_code, file_name = status.split("\t")
status_parts = status.split("\t")
status_code = status_parts[0][0] # Get the first character of the status code
if status_code == "A":
# If the file is new, include the entire file content
file_name = status_parts[1]
contents = Path(file_name).read_text()
diffs.append(f"## New file added: {file_name}")
diffs.append(contents)
elif status_code == "R":
# If the file is renamed, get the diff and note the old and new names
old_file_name, new_file_name = status_parts[1], status_parts[2]
diffs.append(f"## File renamed: {old_file_name} -> {new_file_name}")
diffs.append(exec_and_get_output(base_git_diff + [diff_type, "--", new_file_name]))
elif status_code == "D":
# If the file is deleted, note the deletion
file_name = status_parts[1]
diffs.append(f"## File deleted: {file_name}")
else:
# If the file is not new, get the diff
# If the file is not new, renamed, or deleted, get the diff
file_name = status_parts[1]
diffs.append(f"## File changed: {file_name}")
diffs.append(exec_and_get_output(base_git_diff + [diff_type, "--", file_name]))

Expand Down
17 changes: 16 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,25 @@ def test_git_diff_context(temp_git_repo):
diff = git_diff_context()
assert diff == ""

# Rename the file but don't stage it
temp_git_repo.git.mv("newfile.txt", "renamedfile.txt")
diff = git_diff_context()
assert "## File renamed: newfile.txt -> renamedfile.txt" in diff

# Stage the renamed file
temp_git_repo.git.add("renamedfile.txt")
diff = git_diff_context()
assert "## File renamed: newfile.txt -> renamedfile.txt" in diff

# Commit the renamed file
temp_git_repo.git.commit("-m", "Rename newfile.txt to renamedfile.txt")
diff = git_diff_context()
assert diff == ""

# Test diff for a specific commit
commit = temp_git_repo.head.commit.hexsha
diff = git_diff_context(commit)
assert "This is a modified file." in diff
assert "renamedfile.txt" in diff


def test_exec_and_get_output_success():
Expand Down

0 comments on commit 5c27b8c

Please sign in to comment.