From f62ef347157676ea6dc14ee4088f27ce33eefa05 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sat, 14 Dec 2024 09:42:54 -0800 Subject: [PATCH] fix: Handle missing git module gracefully --- aider/coders/search_replace.py | 6 +++++- aider/main.py | 18 +++++++++++++++--- aider/repo.py | 19 ++++++++++++++----- aider/utils.py | 4 ++-- 4 files changed, 36 insertions(+), 11 deletions(-) diff --git a/aider/coders/search_replace.py b/aider/coders/search_replace.py index f89f629cb..a72a7845b 100755 --- a/aider/coders/search_replace.py +++ b/aider/coders/search_replace.py @@ -3,7 +3,11 @@ import sys from pathlib import Path -import git +try: + import git +except ImportError: + git = None + from diff_match_patch import diff_match_patch from tqdm import tqdm diff --git a/aider/main.py b/aider/main.py index 51377d0fe..3fed8ca98 100644 --- a/aider/main.py +++ b/aider/main.py @@ -9,7 +9,11 @@ import webbrowser from dataclasses import fields from pathlib import Path -import git +try: + import git +except ImportError: + git = None + import importlib_resources from dotenv import load_dotenv from prompt_toolkit.enums import EditingMode @@ -93,6 +97,9 @@ def make_new_repo(git_root, io): def setup_git(git_root, io): + if git is None: + return + try: cwd = Path.cwd() except OSError: @@ -410,7 +417,9 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F if argv is None: argv = sys.argv[1:] - if force_git_root: + if git is None: + git_root = None + elif force_git_root: git_root = force_git_root else: git_root = get_git_root() @@ -451,6 +460,9 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F args, unknown = parser.parse_known_args(argv) + if git is None: + args.git = False + # Load the .env file specified in the arguments loaded_dotenvs = load_dotenv_files(git_root, args.env_file, args.encoding) @@ -646,7 +658,7 @@ def main(argv=None, input=None, output=None, force_git_root=None, return_coder=F # We can't know the git repo for sure until after parsing the args. # If we guessed wrong, reparse because that changes things like # the location of the config.yml and history files. - if args.git and not force_git_root: + if args.git and not force_git_root and git is not None: right_repo_root = guessed_wrong_repo(io, git_root, fnames, git_dname) if right_repo_root: analytics.event("exit", reason="Recursing with correct repo") diff --git a/aider/repo.py b/aider/repo.py index 21ee85a3e..35905df6e 100644 --- a/aider/repo.py +++ b/aider/repo.py @@ -2,7 +2,17 @@ import os import time from pathlib import Path, PurePosixPath -import git +try: + import git + + ANY_GIT_ERROR = [ + git.exc.ODBError, + git.exc.GitError, + ] +except ImportError: + git = None + ANY_GIT_ERROR = [] + import pathspec from aider import prompts, utils @@ -10,15 +20,14 @@ from aider.sendchat import simple_send_with_retries from .dump import dump # noqa: F401 -ANY_GIT_ERROR = ( - git.exc.ODBError, - git.exc.GitError, +ANY_GIT_ERROR += [ OSError, IndexError, BufferError, TypeError, ValueError, -) +] +ANY_GIT_ERROR = tuple(ANY_GIT_ERROR) class GitRepo: diff --git a/aider/utils.py b/aider/utils.py index cda9777db..429d9a4d3 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -8,8 +8,6 @@ import tempfile import time from pathlib import Path -import git - from aider.dump import dump # noqa: F401 IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".webp", ".pdf"} @@ -73,6 +71,8 @@ class GitTemporaryDirectory(ChdirTemporaryDirectory): def make_repo(path=None): + import git + if not path: path = "." repo = git.Repo.init(path)