From 0956a8ec7b300b69aba904c702f22887d4c71cd8 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 2 Jan 2024 08:45:28 -0800 Subject: [PATCH] aider: Ensure /undo command only aborts if files modified in last commit are dirty. # Aider chat conversation: USER: the /undo command aborts with an error if there are any dirty files in the repo. it should only abort if the files that are modified in the latest commit are dirty. ASSISTANT: To address the issue with the `/undo` command, I will modify the check to ensure that it only aborts if the files modified in the latest commit are dirty. This will involve checking the list of files changed in the last commit and comparing it with the list of currently dirty files. Here is the diff for the necessary changes: ```diff --- aider/commands.py +++ aider/commands.py @@ ... @@ def cmd_undo(self, args): "Undo the last git commit if it was done by aider" if not self.coder.repo: self.io.tool_error("No git repository found.") return - if self.coder.repo.is_dirty(): - self.io.tool_error( - "The repository has uncommitted changes. Please commit or stash them before" - " undoing." - ) - return + last_commit = self.coder.repo.repo.head.commit + changed_files_last_commit = {item.a_path for item in last_commit.diff(None)} + dirty_files = self.coder.repo.repo.untracked_files + [item.a_path for item in self.coder.repo.repo.index.diff(None)] + dirty_files_in_last_commit = changed_files_last_commit.intersection(dirty_files) + + if dirty_files_in_last_commit: + self.io.tool_error( + "The repository has uncommitted changes in files that were modified in the last commit. " + "Please commit or stash them before undoing." + ) + return local_head = self.coder.repo.repo.git.rev_parse("HEAD") current_branch = self.coder.repo.repo.active_branch.name try: remote_head = self.coder.repo.repo.git.rev_parse(f"origin/{current_branch}") has_origin = True except git.exc.GitCommandError: has_origin = False ``` This change will ensure that the `/undo` command only aborts if there are uncommitted changes in the files that were modified in the last commit. If there are dirty files that were not part of the last commit, the command will proceed. --- aider/commands.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 1d805feef..391dc2a16 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -186,10 +186,15 @@ class Commands: self.io.tool_error("No git repository found.") return - if self.coder.repo.is_dirty(): + last_commit = self.coder.repo.repo.head.commit + changed_files_last_commit = {item.a_path for item in last_commit.diff(None)} + dirty_files = self.coder.repo.repo.untracked_files + [item.a_path for item in self.coder.repo.repo.index.diff(None)] + dirty_files_in_last_commit = changed_files_last_commit.intersection(dirty_files) + + if dirty_files_in_last_commit: self.io.tool_error( - "The repository has uncommitted changes. Please commit or stash them before" - " undoing." + "The repository has uncommitted changes in files that were modified in the last commit. " + "Please commit or stash them before undoing." ) return