From d0267f0ecea3c7692b781b30dddbc05d46f53f5e Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 5 Jul 2024 13:06:47 -0300 Subject: [PATCH 1/4] Improved the handling of last commit detection and diff generation in the aider commands. --- aider/commands.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index fd129dc3c..0a8a7c2b7 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -330,8 +330,8 @@ class Commands: ) return - last_commit = self.coder.repo.repo.head.commit - if last_commit.hexsha[:7] != self.coder.last_aider_commit_hash: + last_commit_hash = self.coder.repo.repo.head.commit.hexsha[:7] + if last_commit_hash not in self.coder.aider_commit_hashes: self.io.tool_error("The last commit was not made by aider in this chat session.") self.io.tool_error( "You could try `/git reset --hard HEAD^` but be aware that this is a destructive" @@ -346,7 +346,7 @@ class Commands: self.coder.repo.repo.git.reset("--soft", "HEAD~1") self.io.tool_output( - f"Commit `{self.coder.last_aider_commit_hash}` was reset and removed from git.\n" + f"Commit `{last_commit_hash}` was reset and removed from git.\n" ) if self.coder.main_model.send_undo_reply: @@ -358,16 +358,17 @@ class Commands: self.io.tool_error("No git repository found.") return - if not self.coder.last_aider_commit_hash: - self.io.tool_error("No previous aider commit found.") + last_commit_hash = self.coder.repo.repo.head.commit.hexsha[:7] + + if last_commit_hash not in self.coder.aider_commit_hashes: + self.io.tool_error(f"Last commit {last_commit_hash} was not an aider commit.") self.io.tool_error("You could try `/git diff` or `/git diff HEAD^`.") return - commits = f"{self.coder.last_aider_commit_hash}~1" diff = self.coder.repo.diff_commits( self.coder.pretty, - commits, - self.coder.last_aider_commit_hash, + 'HEAD^', + 'HEAD', ) # don't use io.tool_output() because we don't want to log or further colorize From 9e90dbc20d718dcc1a6ccadad3ec878b16362bcd Mon Sep 17 00:00:00 2001 From: "Paul Gauthier (aider)" Date: Fri, 5 Jul 2024 13:06:49 -0300 Subject: [PATCH 2/4] Printed the commit message of the undone commit and the hash and commit message of the current HEAD after the undo operation. --- aider/commands.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/aider/commands.py b/aider/commands.py index 0a8a7c2b7..2cc18ab54 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -331,6 +331,7 @@ class Commands: return last_commit_hash = self.coder.repo.repo.head.commit.hexsha[:7] + last_commit_message = self.coder.repo.repo.head.commit.message.strip() if last_commit_hash not in self.coder.aider_commit_hashes: self.io.tool_error("The last commit was not made by aider in this chat session.") self.io.tool_error( @@ -346,7 +347,14 @@ class Commands: self.coder.repo.repo.git.reset("--soft", "HEAD~1") self.io.tool_output( - f"Commit `{last_commit_hash}` was reset and removed from git.\n" + f"Commit `{last_commit_hash}` with message '{last_commit_message}' was reset and removed from git." + ) + + # Get the current HEAD after undo + current_head_hash = self.coder.repo.repo.head.commit.hexsha[:7] + current_head_message = self.coder.repo.repo.head.commit.message.strip() + self.io.tool_output( + f"Current HEAD is now `{current_head_hash}` with message '{current_head_message}'." ) if self.coder.main_model.send_undo_reply: From 5122121265256b72dbd8aa82e31d91dfe8f53f5d Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 5 Jul 2024 13:09:05 -0300 Subject: [PATCH 3/4] allow multiple undo --- aider/coders/base_coder.py | 9 +++++++++ aider/commands.py | 12 ++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 662bd7912..f6c8f7174 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -48,6 +48,7 @@ class Coder: abs_fnames = None repo = None last_aider_commit_hash = None + aider_commit_hashes = set() aider_edited_files = None last_asked_for_commit_time = 0 repo_map = None @@ -115,6 +116,7 @@ class Coder: fnames=from_coder.get_inchat_relative_files(), done_messages=done_messages, cur_messages=from_coder.cur_messages, + aider_commit_hashes=from_coder.aider_commit_hashes, ) use_kwargs.update(update) # override to complete the switch @@ -222,6 +224,7 @@ class Coder: attribute_author=True, attribute_committer=True, attribute_commit_message=False, + aider_commit_hashes=None, ): if not fnames: fnames = [] @@ -229,6 +232,11 @@ class Coder: if io is None: io = InputOutput() + if aider_commit_hashes: + self.aider_commit_hashes = aider_commit_hashes + else: + self.aider_commit_hashes = set() + self.chat_completion_call_hashes = [] self.chat_completion_response_hashes = [] self.need_commit_before_edits = set() @@ -1451,6 +1459,7 @@ class Coder: if res: commit_hash, commit_message = res self.last_aider_commit_hash = commit_hash + self.aider_commit_hashes.add(commit_hash) self.last_aider_commit_message = commit_message if self.show_diffs: self.commands.cmd_diff() diff --git a/aider/commands.py b/aider/commands.py index 2cc18ab54..b50138c2b 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -346,16 +346,12 @@ class Commands: # Move the HEAD back before the latest commit self.coder.repo.repo.git.reset("--soft", "HEAD~1") - self.io.tool_output( - f"Commit `{last_commit_hash}` with message '{last_commit_message}' was reset and removed from git." - ) + self.io.tool_output(f"Removed: {last_commit_hash} {last_commit_message}") # Get the current HEAD after undo current_head_hash = self.coder.repo.repo.head.commit.hexsha[:7] current_head_message = self.coder.repo.repo.head.commit.message.strip() - self.io.tool_output( - f"Current HEAD is now `{current_head_hash}` with message '{current_head_message}'." - ) + self.io.tool_output(f"HEAD is: {current_head_hash} {current_head_message}") if self.coder.main_model.send_undo_reply: return prompts.undo_command_reply @@ -375,8 +371,8 @@ class Commands: diff = self.coder.repo.diff_commits( self.coder.pretty, - 'HEAD^', - 'HEAD', + "HEAD^", + "HEAD", ) # don't use io.tool_output() because we don't want to log or further colorize From 783cdb772846acd96cd1222b80cd8bd261a47494 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 5 Jul 2024 13:28:25 -0300 Subject: [PATCH 4/4] Allowed multiple use of /undo command. --- HISTORY.md | 4 ++++ aider/tests/test_commands.py | 2 +- website/HISTORY.md | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/HISTORY.md b/HISTORY.md index 3f5fffda1..8f994ff77 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,10 @@ # Release history +### main branch + +- Allow multiple use of `/undo`. + ### Aider v0.42.0 - Performance release: diff --git a/aider/tests/test_commands.py b/aider/tests/test_commands.py index 262262f39..740cb147d 100644 --- a/aider/tests/test_commands.py +++ b/aider/tests/test_commands.py @@ -536,7 +536,7 @@ class TestCommands(TestCase): # Store the commit hash last_commit_hash = repo.head.commit.hexsha[:7] - coder.last_aider_commit_hash = last_commit_hash + coder.aider_commit_hashes.add(last_commit_hash) file_path.write_text("dirty content") diff --git a/website/HISTORY.md b/website/HISTORY.md index 03ad6a5bb..356176a09 100644 --- a/website/HISTORY.md +++ b/website/HISTORY.md @@ -12,6 +12,10 @@ cog.out(text) # Release history +### main branch + +- Allow multiple use of `/undo`. + ### Aider v0.42.0 - Performance release: