aider: Fixed cmd_undo function to catch GitCommandError exception when trying to get the remote head of the current branch using origin/<branch_name> instead of origin/HEAD.

# Context:
USER: README.md> /undo

Traceback (most recent call last):
  File "/Users/gauthier/Projects/aider/.venv/bin/aider", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/gauthier/Projects/aider/aider/main.py", line 113, in main
    coder.run()
  File "/Users/gauthier/Projects/aider/aider/coder.py", line 178, in run
    new_user_message = self.run_loop()
                       ^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/aider/coder.py", line 223, in run_loop
    return self.commands.run(inp)
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/aider/commands.py", line 62, in run
    return self.do_run(matching_commands[0][1:], rest_inp)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/aider/commands.py", line 44, in do_run
    return cmd_method(args)
           ^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/aider/commands.py", line 96, in cmd_undo
    remote_head = self.coder.repo.git.rev_parse("origin/HEAD")
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/.venv/lib/python3.11/site-packages/git/cmd.py", line 741, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/.venv/lib/python3.11/site-packages/git/cmd.py", line 1315, in _call_process
    return self.execute(call, **exec_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/Projects/aider/.venv/lib/python3.11/site-packages/git/cmd.py", line 1109, in execute
    raise GitCommandError(redacted_command, status, stderr_value, stdout_value)
git.exc.GitCommandError: Cmd('git') failed due to: exit code(128)
  cmdline: git rev-parse origin/HEAD
  stdout: 'origin/HEAD'
  stderr: 'fatal: ambiguous argument 'origin/HEAD': unknown revision or path not in the working tree.

ASSISTANT: It seems like there is an issue with the `cmd_undo` function in the `aider/commands.py` file. The error is caused by the `self.coder.repo.git.rev_parse("origin/HEAD")` line. To fix this issue, we can check if the remote "origin" exists before trying to get the remote head.

Here's the change I suggest:

```python
aider/commands.py
<<<<<<< ORIGINAL
        if has_origin:
            remote_head = self.coder.repo.git.rev_parse("origin/HEAD")
=======
        if has_origin:
            try:
                remote_head = self.coder.repo.git.rev_parse("origin/HEAD")
            except git.exc.GitCommandError:
                self.io.tool_error("Error: Unable to get the remote 'origin/HEAD'.")
                return
>>>>>>> UPDATED
```

This change will catch the `GitCommandError` exception and display an error message instead of raising
^C KeyboardInterrupt
USER: the error occurred on a repo with an origin. Is `origin/HEAD` a valid way to check the head of the current branch's remote?
ASSISTANT: I apologize for the confusion. You are correct that `origin/HEAD` is not the proper way to check the head of the current branch's remote. Instead, we should use `origin/<branch_name>` to get the remote head of the current branch. Here's the updated change:

```python
aider/commands.py
<<<<<<< ORIGINAL
        if has_origin:
            remote_head = self.coder.repo.git.rev_parse("origin/HEAD")
=======
        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
>>>>>>> UPDATED
```

This change will get the name of the current active branch and use it to check the remote head of the current branch.
This commit is contained in:
Paul Gauthier 2023-05-13 09:55:00 -07:00
parent 51abe3d24f
commit e3f523d427

View file

@ -93,7 +93,12 @@ class Commands:
has_origin = any(remote.name == "origin" for remote in self.coder.repo.remotes) has_origin = any(remote.name == "origin" for remote in self.coder.repo.remotes)
if has_origin: if has_origin:
remote_head = self.coder.repo.git.rev_parse("origin/HEAD") 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: if local_head == remote_head:
self.io.tool_error("The last commit has already been pushed to the origin. Undoing is not possible.") self.io.tool_error("The last commit has already been pushed to the origin. Undoing is not possible.")
return return