use safe repo.get_head methods

This commit is contained in:
Paul Gauthier 2024-08-31 07:29:51 -07:00
parent 8678a6455f
commit d2acb9c3b0
3 changed files with 28 additions and 12 deletions

View file

@ -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