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/coders/base_coder.py b/aider/coders/base_coder.py index 7def7b52d..b6c0ead61 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 @@ -117,6 +118,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 @@ -226,6 +228,7 @@ class Coder: attribute_author=True, attribute_committer=True, attribute_commit_message=False, + aider_commit_hashes=None, ): if not fnames: fnames = [] @@ -233,6 +236,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() @@ -1454,6 +1462,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 b42894e27..4c8933f7e 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -333,8 +333,9 @@ 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] + 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( "You could try `/git reset --hard HEAD^` but be aware that this is a destructive" @@ -348,9 +349,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 `{self.coder.last_aider_commit_hash}` was reset and removed from git.\n" - ) + 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"HEAD is: {current_head_hash} {current_head_message}") if self.coder.main_model.send_undo_reply: return prompts.undo_command_reply @@ -361,16 +365,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 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/aider/website/HISTORY.md b/aider/website/HISTORY.md index 03ad6a5bb..356176a09 100644 --- a/aider/website/HISTORY.md +++ b/aider/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: