From 5c27b8c164f4800ac46e10339557d6550c4fa2b5 Mon Sep 17 00:00:00 2001 From: Nick Sullivan Date: Sun, 2 Jul 2023 19:20:10 -0600 Subject: [PATCH] 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. --- aicodebot/helpers.py | 16 ++++++++++++++-- tests/test_helpers.py | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/aicodebot/helpers.py b/aicodebot/helpers.py index 1a609ff..4cfd799 100644 --- a/aicodebot/helpers.py +++ b/aicodebot/helpers.py @@ -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])) diff --git a/tests/test_helpers.py b/tests/test_helpers.py index 176cf73..8905d4b 100644 --- a/tests/test_helpers.py +++ b/tests/test_helpers.py @@ -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():