From e3f523d4271fdf5118a3781b95ee7046a7f2d4cf Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 13 May 2023 09:55:00 -0700 Subject: [PATCH] aider: Fixed `cmd_undo` function to catch `GitCommandError` exception when trying to get the remote head of the current branch using `origin/` 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 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 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/` 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. --- aider/commands.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/aider/commands.py b/aider/commands.py index 93e1740e4..a157117d1 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -93,7 +93,12 @@ class Commands: 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") + 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