fix: Handle missing git module gracefully

This commit is contained in:
Paul Gauthier 2024-12-14 09:42:54 -08:00 committed by Paul Gauthier (aider)
parent 65555b5dd0
commit f62ef34715
4 changed files with 36 additions and 11 deletions

View file

@ -3,7 +3,11 @@
import sys import sys
from pathlib import Path from pathlib import Path
try:
import git import git
except ImportError:
git = None
from diff_match_patch import diff_match_patch from diff_match_patch import diff_match_patch
from tqdm import tqdm from tqdm import tqdm

View file

@ -9,7 +9,11 @@ import webbrowser
from dataclasses import fields from dataclasses import fields
from pathlib import Path from pathlib import Path
try:
import git import git
except ImportError:
git = None
import importlib_resources import importlib_resources
from dotenv import load_dotenv from dotenv import load_dotenv
from prompt_toolkit.enums import EditingMode from prompt_toolkit.enums import EditingMode
@ -93,6 +97,9 @@ def make_new_repo(git_root, io):
def setup_git(git_root, io): def setup_git(git_root, io):
if git is None:
return
try: try:
cwd = Path.cwd() cwd = Path.cwd()
except OSError: 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: if argv is None:
argv = sys.argv[1:] argv = sys.argv[1:]
if force_git_root: if git is None:
git_root = None
elif force_git_root:
git_root = force_git_root git_root = force_git_root
else: else:
git_root = get_git_root() 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) args, unknown = parser.parse_known_args(argv)
if git is None:
args.git = False
# Load the .env file specified in the arguments # Load the .env file specified in the arguments
loaded_dotenvs = load_dotenv_files(git_root, args.env_file, args.encoding) 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. # We can't know the git repo for sure until after parsing the args.
# If we guessed wrong, reparse because that changes things like # If we guessed wrong, reparse because that changes things like
# the location of the config.yml and history files. # 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) right_repo_root = guessed_wrong_repo(io, git_root, fnames, git_dname)
if right_repo_root: if right_repo_root:
analytics.event("exit", reason="Recursing with correct repo") analytics.event("exit", reason="Recursing with correct repo")

View file

@ -2,7 +2,17 @@ import os
import time import time
from pathlib import Path, PurePosixPath from pathlib import Path, PurePosixPath
try:
import git import git
ANY_GIT_ERROR = [
git.exc.ODBError,
git.exc.GitError,
]
except ImportError:
git = None
ANY_GIT_ERROR = []
import pathspec import pathspec
from aider import prompts, utils from aider import prompts, utils
@ -10,15 +20,14 @@ from aider.sendchat import simple_send_with_retries
from .dump import dump # noqa: F401 from .dump import dump # noqa: F401
ANY_GIT_ERROR = ( ANY_GIT_ERROR += [
git.exc.ODBError,
git.exc.GitError,
OSError, OSError,
IndexError, IndexError,
BufferError, BufferError,
TypeError, TypeError,
ValueError, ValueError,
) ]
ANY_GIT_ERROR = tuple(ANY_GIT_ERROR)
class GitRepo: class GitRepo:

View file

@ -8,8 +8,6 @@ import tempfile
import time import time
from pathlib import Path from pathlib import Path
import git
from aider.dump import dump # noqa: F401 from aider.dump import dump # noqa: F401
IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".webp", ".pdf"} IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".webp", ".pdf"}
@ -73,6 +71,8 @@ class GitTemporaryDirectory(ChdirTemporaryDirectory):
def make_repo(path=None): def make_repo(path=None):
import git
if not path: if not path:
path = "." path = "."
repo = git.Repo.init(path) repo = git.Repo.init(path)