From 83c47eef621a8262229798c14ce09234b0417d6e Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 13 May 2023 09:51:57 -0700 Subject: [PATCH] aider: Added conditional check to see if the repo has an origin before comparing local and remote commit hashes in cmd_undo. # Context: USER: not all repos have an origin. only do that check if the repo does ASSISTANT: To check if the repo has an origin, we can use `self.coder.repo.remotes` to see if there is a remote named "origin". We will add a conditional check before comparing the local and remote commit hashes. ```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 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 ======= 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") has_origin = any(remote.name == "origin" for remote in self.coder.repo.remotes) if has_origin: 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 | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 2a693148a..93e1740e4 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -90,11 +90,13 @@ class Commands: return local_head = self.coder.repo.git.rev_parse("HEAD") - remote_head = self.coder.repo.git.rev_parse("origin/HEAD") + has_origin = any(remote.name == "origin" for remote in self.coder.repo.remotes) - if local_head == remote_head: - self.io.tool_error("The last commit has already been pushed to the origin. Undoing is not possible.") - return + if has_origin: + 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 (