From e8b041af96ebf3f6f451593982e4c6a99eb33a5d Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Thu, 18 May 2023 14:25:23 -0700 Subject: [PATCH] aider: Added check to ensure remote head is only fetched when there is a remote origin. # Context: USER: the /undo command is throwing this error when the repo has no remote origin: Error: Unable to get the remote 'origin/scratch'. ASSISTANT: I see the issue. The code is trying to get the remote head even when there is no remote origin. I will add a check to ensure that the remote head is only fetched when there is a remote origin. Here's the change: ```python aider/commands.py <<<<<<< ORIGINAL if has_origin: current_branch = self.coder.repo.active_branch.name try: remote_head = self.coder.repo.git.rev_parse(f"origin/{current_branch}") except git.exc.GitCommandError: self.io.tool_error(f"Error: Unable to get the remote 'origin/{current_branch}'.") return 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: current_branch = self.coder.repo.active_branch.name try: remote_head = self.coder.repo.git.rev_parse(f"origin/{current_branch}") if local_head == remote_head: self.io.tool_error( "The last commit has already been pushed to the origin. Undoing is not" " possible." ) return except git.exc.GitCommandError ^C KeyboardInterrupt USER: no, has_origin needs to check if the *current branch* has an origin. ASSISTANT: I understand now. I will modify the `has_origin` check to verify if the current branch has an origin. Here's the updated change: ```python aider/commands.py <<<<<<< ORIGINAL has_origin = any(remote.name == "origin" for remote in self.coder.repo.remotes) if has_origin: current_branch = self.coder.repo.active_branch.name try: remote_head = self.coder.repo.git.rev_parse(f"origin/{current_branch}") except git.exc.GitCommandError: self.io.tool_error(f"Error: Unable to get the remote 'origin/{current_branch}'.") return if local_head == remote_head: self.io.tool_error( "The last commit has already been pushed to the origin. Undoing is not" " possible." ) return ======= current_branch = self.coder.repo.active_branch.name try: remote_head = self.coder.repo.git.rev_parse(f"origin/{current_branch}") has_origin = True except git.exc.GitCommandError: has_origin = False if has_origin: if local_head == remote_head: self.io.tool_error( "The last commit has already been pushed to the origin. Undoing is not" " possible." ) return >>>>>>> UPDATED ``` This change will set `has_origin` to `True` only if the current branch has an origin, and it will handle the error accordingly. --- aider/commands.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 0b13506ed..e20c0e55f 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -99,15 +99,14 @@ class Commands: return local_head = self.coder.repo.git.rev_parse("HEAD") - has_origin = any(remote.name == "origin" for remote in self.coder.repo.remotes) + current_branch = self.coder.repo.active_branch.name + try: + remote_head = self.coder.repo.git.rev_parse(f"origin/{current_branch}") + has_origin = True + except git.exc.GitCommandError: + has_origin = False if has_origin: - current_branch = self.coder.repo.active_branch.name - try: - remote_head = self.coder.repo.git.rev_parse(f"origin/{current_branch}") - except git.exc.GitCommandError: - self.io.tool_error(f"Error: Unable to get the remote 'origin/{current_branch}'.") - return if local_head == remote_head: self.io.tool_error( "The last commit has already been pushed to the origin. Undoing is not"