From 84b3a1756d5ad6a541fb0d6e126feb182fcea1d6 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 13 May 2023 09:48:30 -0700 Subject: [PATCH] aider: Added check to prevent undoing a commit that has already been pushed to origin. # Context: USER: also check to make sure the commit hasn't already been pushed to origin ASSISTANT: To check if the commit has already been pushed to the origin, we can compare the local and remote commit hashes. We will add a check using `self.coder.repo.git.rev_parse("HEAD")` and `self.coder.repo.git.rev_parse("origin/HEAD")` before performing the undo operation. ```python aider/commands.py <<<<<<< ORIGINAL 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.head.commit ======= 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 local_head = self.coder.repo.git.rev_parse("HEAD") remote_head = self.coder.repo.git.rev_parse("origin/HEAD") if local_head == remote_head: self.io.tool_error("The last commit has already been pushed to the origin. Undoing is not possible.") return last_commit = self.coder.repo.head.commit >>>>>>> UPDATED ``` --- aider/commands.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/aider/commands.py b/aider/commands.py index cde5dd93d..2a693148a 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -89,6 +89,13 @@ class Commands: self.io.tool_error("The repository has uncommitted changes. Please commit or stash them before undoing.") return + local_head = self.coder.repo.git.rev_parse("HEAD") + remote_head = self.coder.repo.git.rev_parse("origin/HEAD") + + if local_head == remote_head: + self.io.tool_error("The last commit has already been pushed to the origin. Undoing is not possible.") + return + last_commit = self.coder.repo.head.commit if ( not last_commit.message.startswith("aider:")