From 59ab516cbb4bf7b1d9fe34e2965976a8e451d7da Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 10 May 2023 12:35:00 -0700 Subject: [PATCH] aider: Added functionality to undo the last git commit if it was done by Aider. # Context: USER: finish cmd_undo ASSISTANT: To finish the `cmd_undo` method in the `Coder` class, you can add the following code to `aider/coder.py`. This method will undo the last git commit if it was done by Aider. ```python aider/coder.py <<<<<<< ORIGINAL def cmd_undo(self, args): "Undo the last git commit if it was done by aider" ======= def cmd_undo(self, args): "Undo the last git commit if it was done by aider" if not self.repo: self.console.print("[red]No git repository found.") return last_commit = self.repo.head.commit if not last_commit.message.startswith("aider:"): self.console.print("[red]The last commit was not made by Aider.") return self.repo.git.reset("--hard", "HEAD~1") self.console.print(f"[red]Undid the last commit: {last_commit.message.strip()}") >>>>>>> UPDATED ``` This code checks if there is a git repository, and if the last commit was made by Aider. If so, it undoes the last commit using `git reset --hard HEAD~1`. --- aider/coder.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/aider/coder.py b/aider/coder.py index 691b5c5b3..0bb8b4da6 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -488,7 +488,17 @@ class Coder: def cmd_undo(self, args): "Undo the last git commit if it was done by aider" + if not self.repo: + self.console.print("[red]No git repository found.") + return + last_commit = self.repo.head.commit + if not last_commit.message.startswith("aider:"): + self.console.print("[red]The last commit was not made by Aider.") + return + + self.repo.git.reset("--hard", "HEAD~1") + self.console.print(f"[red]Undid the last commit: {last_commit.message.strip()}") def cmd_add(self, args): "Add matching files to the chat"