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`.
This commit is contained in:
Paul Gauthier 2023-05-10 12:35:00 -07:00
parent 4fc7b99de4
commit 59ab516cbb

View file

@ -488,7 +488,17 @@ class Coder:
def cmd_undo(self, args): def cmd_undo(self, args):
"Undo the last git commit if it was done by aider" "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): def cmd_add(self, args):
"Add matching files to the chat" "Add matching files to the chat"