refactor: consolidate git error handling with ANY_GIT_ERROR constant

This commit is contained in:
Paul Gauthier 2024-08-31 08:41:32 -07:00 committed by Paul Gauthier (aider)
parent 5a6f7b3cd1
commit 5781f91649
4 changed files with 13 additions and 9 deletions

View file

@ -29,7 +29,7 @@ from aider.io import ConfirmGroup, InputOutput
from aider.linter import Linter from aider.linter import Linter
from aider.llm import litellm from aider.llm import litellm
from aider.mdstream import MarkdownStream from aider.mdstream import MarkdownStream
from aider.repo import GitRepo from aider.repo import GitRepo, ANY_GIT_ERROR
from aider.repomap import RepoMap from aider.repomap import RepoMap
from aider.run_cmd import run_cmd from aider.run_cmd import run_cmd
from aider.sendchat import retry_exceptions, send_completion from aider.sendchat import retry_exceptions, send_completion
@ -1784,7 +1784,7 @@ class Coder:
self.reflected_message = str(err) self.reflected_message = str(err)
return edited return edited
except (git.exc.ODBError, git.exc.GitError) as err: except ANY_GIT_ERROR as err:
self.io.tool_error(str(err)) self.io.tool_error(str(err))
return edited return edited
except Exception as err: except Exception as err:

View file

@ -14,6 +14,7 @@ from aider import models, prompts, voice
from aider.format_settings import format_settings from aider.format_settings import format_settings
from aider.help import Help, install_help_extra from aider.help import Help, install_help_extra
from aider.llm import litellm from aider.llm import litellm
from aider.repo import ANY_GIT_ERROR
from aider.run_cmd import run_cmd from aider.run_cmd import run_cmd
from aider.scrape import Scraper, install_playwright from aider.scrape import Scraper, install_playwright
from aider.utils import is_image_file from aider.utils import is_image_file
@ -450,7 +451,7 @@ class Commands:
try: try:
remote_head = self.coder.repo.repo.git.rev_parse(f"origin/{current_branch}") remote_head = self.coder.repo.repo.git.rev_parse(f"origin/{current_branch}")
has_origin = True has_origin = True
except (git.exc.ODBError, git.exc.GitError): except ANY_GIT_ERROR:
has_origin = False has_origin = False
if has_origin: if has_origin:

View file

@ -17,7 +17,7 @@ from aider.format_settings import format_settings, scrub_sensitive_info
from aider.history import ChatSummary from aider.history import ChatSummary
from aider.io import InputOutput from aider.io import InputOutput
from aider.llm import litellm # noqa: F401; properly init litellm on launch from aider.llm import litellm # noqa: F401; properly init litellm on launch
from aider.repo import GitRepo, UnableToCountRepoFiles from aider.repo import ANY_GIT_ERROR, GitRepo, UnableToCountRepoFiles
from aider.report import report_uncaught_exceptions from aider.report import report_uncaught_exceptions
from aider.versioncheck import check_version, install_from_main_branch, install_upgrade from aider.versioncheck import check_version, install_from_main_branch, install_upgrade
@ -56,7 +56,7 @@ def make_new_repo(git_root, io):
try: try:
repo = git.Repo.init(git_root) repo = git.Repo.init(git_root)
check_gitignore(git_root, io, False) check_gitignore(git_root, io, False)
except (git.exc.ODBError, git.exc.GitError) as err: # issue #1233 except ANY_GIT_ERROR as err: # issue #1233
io.tool_error(f"Unable to create git repo in {git_root}") io.tool_error(f"Unable to create git repo in {git_root}")
io.tool_error(str(err)) io.tool_error(str(err))
return return
@ -114,7 +114,7 @@ def check_gitignore(git_root, io, ask=True):
repo = git.Repo(git_root) repo = git.Repo(git_root)
if repo.ignored(".aider"): if repo.ignored(".aider"):
return return
except (git.exc.ODBError, git.exc.GitError): except ANY_GIT_ERROR:
pass pass
pat = ".aider*" pat = ".aider*"

View file

@ -15,6 +15,9 @@ class UnableToCountRepoFiles(Exception):
pass pass
ANY_GIT_ERROR = (git.exc.ODBError, git.exc.GitError)
class GitRepo: class GitRepo:
repo = None repo = None
aider_ignore_file = None aider_ignore_file = None
@ -71,7 +74,7 @@ class GitRepo:
repo_path = git.Repo(fname, search_parent_directories=True).working_dir repo_path = git.Repo(fname, search_parent_directories=True).working_dir
repo_path = utils.safe_abs_path(repo_path) repo_path = utils.safe_abs_path(repo_path)
repo_paths.append(repo_path) repo_paths.append(repo_path)
except (git.exc.ODBError, git.exc.GitError): except ANY_GIT_ERROR:
pass pass
num_repos = len(set(repo_paths)) num_repos = len(set(repo_paths))
@ -205,7 +208,7 @@ class GitRepo:
try: try:
commits = self.repo.iter_commits(active_branch) commits = self.repo.iter_commits(active_branch)
current_branch_has_commits = any(commits) current_branch_has_commits = any(commits)
except (git.exc.ODBError, git.exc.GitError): except ANY_GIT_ERROR:
pass pass
except TypeError: except TypeError:
pass pass
@ -373,7 +376,7 @@ class GitRepo:
def get_head_commit(self): def get_head_commit(self):
try: try:
return self.repo.head.commit return self.repo.head.commit
except (ValueError, git.exc.ODBError, git.exc.GitError): except (ValueError,) + ANY_GIT_ERROR:
return None return None
def get_head_commit_sha(self, short=False): def get_head_commit_sha(self, short=False):