From d2acb9c3b0beb521162c2cc9e1bae91789e36cd8 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 31 Aug 2024 07:29:51 -0700 Subject: [PATCH] use safe repo.get_head methods --- aider/coders/base_coder.py | 4 ++-- aider/commands.py | 15 ++++++++------- aider/repo.py | 21 ++++++++++++++++++--- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/aider/coders/base_coder.py b/aider/coders/base_coder.py index 9c9f81248..1ae95b56c 100755 --- a/aider/coders/base_coder.py +++ b/aider/coders/base_coder.py @@ -713,7 +713,7 @@ class Coder: self.shell_commands = [] if self.repo: - self.commit_before_message.append(self.repo.get_head()) + self.commit_before_message.append(self.repo.get_head_sha()) def run(self, with_message=None, preproc=True): try: @@ -1867,7 +1867,7 @@ class Coder: def show_undo_hint(self): if not self.commit_before_message: return - if self.commit_before_message[-1] != self.repo.get_head(): + if self.commit_before_message[-1] != self.repo.get_head_sha(): self.io.tool_output("You can use /undo to undo and discard each aider commit.") def dirty_commit(self): diff --git a/aider/commands.py b/aider/commands.py index f4ad56fdd..f690358f2 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -420,8 +420,8 @@ class Commands: self.io.tool_error("No git repository found.") return - last_commit = self.coder.repo.repo.head.commit - if not last_commit.parents: + last_commit = self.coder.repo.get_head() + if last_commit and not last_commit.parents: self.io.tool_error("This is the first commit in the repository. Cannot undo.") return @@ -461,8 +461,9 @@ class Commands: ) return - last_commit_hash = self.coder.repo.repo.head.commit.hexsha[:7] - last_commit_message = self.coder.repo.repo.head.commit.message.strip() + last_commit_hash = self.coder.repo.get_head_sha(short=True) + last_commit_message = self.coder.repo.get_head_message("(unknown)") + if last_commit_hash not in self.coder.aider_commit_hashes: self.io.tool_error("The last commit was not made by aider in this chat session.") self.io.tool_error( @@ -481,8 +482,8 @@ class Commands: self.io.tool_output(f"Removed: {last_commit_hash} {last_commit_message}") # Get the current HEAD after undo - current_head_hash = self.coder.repo.repo.head.commit.hexsha[:7] - current_head_message = self.coder.repo.repo.head.commit.message.strip() + current_head_hash = self.coder.repo.get_head_sha(short=True) + current_head_message = self.coder.repo.get_head_message("(unknown)") self.io.tool_output(f"Now at: {current_head_hash} {current_head_message}") if self.coder.main_model.send_undo_reply: @@ -494,7 +495,7 @@ class Commands: self.io.tool_error("No git repository found.") return - current_head = self.coder.repo.get_head() + current_head = self.coder.repo.get_head_sha() if current_head is None: self.io.tool_error("Unable to get current commit. The repository might be empty.") return diff --git a/aider/repo.py b/aider/repo.py index fbbcf3f93..6f4ffa235 100644 --- a/aider/repo.py +++ b/aider/repo.py @@ -3,6 +3,7 @@ import time from pathlib import Path, PurePosixPath import git +import gitdb import pathspec from aider import prompts, utils @@ -137,7 +138,7 @@ class GitRepo: os.environ["GIT_AUTHOR_NAME"] = committer_name self.repo.git.commit(cmd) - commit_hash = self.repo.head.commit.hexsha[:7] + commit_hash = self.get_head_sha(short=True) self.io.tool_output(f"Commit {commit_hash} {commit_message}", bold=True) # Restore the env @@ -374,6 +375,20 @@ class GitRepo: def get_head(self): try: - return self.repo.head.commit.hexsha - except ValueError: + return self.repo.head.commit + except (ValueError, gitdb.exc.ODBError): return None + + def get_head_sha(self, short=False): + commit = self.get_head() + if not commit: + return + if short: + return commit.hexsha[:7] + return commit.hexsha + + def get_head_message(self, default=None): + commit = self.get_head() + if not commit: + return default + return commit.message