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.
This commit is contained in:
Paul Gauthier 2023-05-18 14:25:23 -07:00
parent c9e35ebf83
commit e8b041af96

View file

@ -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)
if has_origin:
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:
self.io.tool_error(f"Error: Unable to get the remote 'origin/{current_branch}'.")
return
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"