Accept either a git dname or a list of fnames

This commit is contained in:
Paul Gauthier 2023-07-24 17:31:00 -03:00
parent 256dd7bbd9
commit 64c50bab48
3 changed files with 109 additions and 27 deletions

View file

@ -98,6 +98,7 @@ class Coder:
main_model, main_model,
io, io,
fnames=None, fnames=None,
git_dname=None,
pretty=True, pretty=True,
show_diffs=False, show_diffs=False,
auto_commits=True, auto_commits=True,
@ -155,11 +156,14 @@ class Coder:
fname.parent.mkdir(parents=True, exist_ok=True) fname.parent.mkdir(parents=True, exist_ok=True)
fname.touch() fname.touch()
if not fname.is_file():
raise ValueError(f"{fname} is not a file")
self.abs_fnames.add(str(fname.resolve())) self.abs_fnames.add(str(fname.resolve()))
if use_git: if use_git:
try: try:
self.repo = GitRepo(self.io, fnames) self.repo = GitRepo(self.io, fnames, git_dname)
self.root = self.repo.root self.root = self.repo.root
except FileNotFoundError: except FileNotFoundError:
self.repo = None self.repo = None
@ -197,7 +201,7 @@ class Coder:
self.io.tool_output(f"Added {fname} to the chat.") self.io.tool_output(f"Added {fname} to the chat.")
if self.repo: if self.repo:
self.repo.add_new_files(fnames) self.repo.add_new_files(fname for fname in fnames if not Path(fname).is_dir())
# validate the functions jsonschema # validate the functions jsonschema
if self.functions: if self.functions:

View file

@ -9,10 +9,14 @@ import openai
from aider import __version__, models from aider import __version__, models
from aider.coders import Coder from aider.coders import Coder
from aider.io import InputOutput from aider.io import InputOutput
from aider.repo import GitRepo
from aider.versioncheck import check_version from aider.versioncheck import check_version
from .dump import dump # noqa: F402
def get_git_root(): def get_git_root():
"""Try and guess the git repo, since the conf.yml can be at the repo root"""
try: try:
repo = git.Repo(search_parent_directories=True) repo = git.Repo(search_parent_directories=True)
return repo.working_tree_dir return repo.working_tree_dir
@ -20,6 +24,33 @@ def get_git_root():
return None return None
def guessed_wrong_repo(io, git_root, fnames, git_dname):
"""After we parse the args, we can determine the real repo. Did we guess wrong?"""
dump(git_root)
try:
check_repo = Path(GitRepo(io, fnames, git_dname).root).resolve()
except FileNotFoundError:
return
dump(check_repo)
# we had no guess, rely on the "true" repo result
if not git_root:
return str(check_repo)
git_root = Path(git_root).resolve()
if check_repo == git_root:
return
print("guessed the wrong repo")
dump(check_repo)
dump(git_root)
return str(check_repo)
def setup_git(git_root, io): def setup_git(git_root, io):
if git_root: if git_root:
return git_root return git_root
@ -71,10 +102,13 @@ def check_gitignore(git_root, io, ask=True):
io.tool_output(f"Added {pat} to .gitignore") io.tool_output(f"Added {pat} to .gitignore")
def main(args=None, input=None, output=None): def main(argv=None, input=None, output=None, force_git_root=None):
if args is None: if argv is None:
args = sys.argv[1:] argv = sys.argv[1:]
if force_git_root:
git_root = force_git_root
else:
git_root = get_git_root() git_root = get_git_root()
conf_fname = Path(".aider.conf.yml") conf_fname = Path(".aider.conf.yml")
@ -344,7 +378,7 @@ def main(args=None, input=None, output=None):
), ),
) )
args = parser.parse_args(args) args = parser.parse_args(argv)
if args.dark_mode: if args.dark_mode:
args.user_input_color = "#32FF32" args.user_input_color = "#32FF32"
@ -371,6 +405,38 @@ def main(args=None, input=None, output=None):
dry_run=args.dry_run, dry_run=args.dry_run,
) )
fnames = args.files
if len(args.files) > 1:
good = True
for fname in args.files:
if Path(fname).is_dir():
io.tool_error(f"{fname} is a directory, not provided alone.")
good = False
if not good:
io.tool_error(
"Provide either a single directory of a git repo, or a list of one or more files."
)
return 1
git_dname = None
if len(args.files) == 1:
if Path(args.files[0]).is_dir():
if args.git:
git_dname = args.files[0]
fnames = []
else:
io.tool_error(f"{args.files[0]} is a directory, but --no-git selected.")
return -1
# 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:
right_repo_root = guessed_wrong_repo(io, git_root, fnames, git_dname)
if right_repo_root:
print("guessed wrong")
return main(argv, input, output, right_repo_root)
io.tool_output(f"Aider v{__version__}") io.tool_output(f"Aider v{__version__}")
check_version(io.tool_error) check_version(io.tool_error)
@ -418,12 +484,14 @@ def main(args=None, input=None, output=None):
setattr(openai, mod_key, val) setattr(openai, mod_key, val)
io.tool_output(f"Setting openai.{mod_key}={val}") io.tool_output(f"Setting openai.{mod_key}={val}")
try:
coder = Coder.create( coder = Coder.create(
main_model, main_model,
args.edit_format, args.edit_format,
io, io,
## ##
fnames=args.files, fnames=fnames,
git_dname=git_dname,
pretty=args.pretty, pretty=args.pretty,
show_diffs=args.show_diffs, show_diffs=args.show_diffs,
auto_commits=args.auto_commits, auto_commits=args.auto_commits,
@ -436,6 +504,9 @@ def main(args=None, input=None, output=None):
stream=args.stream, stream=args.stream,
use_git=args.git, use_git=args.git,
) )
except ValueError as err:
io.tool_error(str(err))
return
if args.show_repo_map: if args.show_repo_map:
repo_map = coder.get_repo_map() repo_map = coder.get_repo_map()

View file

@ -12,10 +12,12 @@ from .dump import dump # noqa: F401
class GitRepo: class GitRepo:
repo = None repo = None
def __init__(self, io, fnames): def __init__(self, io, fnames, git_dname):
self.io = io self.io = io
if fnames: if git_dname:
check_fnames = [git_dname]
elif fnames:
check_fnames = fnames check_fnames = fnames
else: else:
check_fnames = ["."] check_fnames = ["."]
@ -25,6 +27,9 @@ class GitRepo:
fname = Path(fname) fname = Path(fname)
fname = fname.resolve() fname = fname.resolve()
if not fname.exists() and fname.parent.exists():
fname = fname.parent
try: try:
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)
@ -49,6 +54,8 @@ class GitRepo:
for fname in fnames: for fname in fnames:
if Path(fname).resolve() in cur_files: if Path(fname).resolve() in cur_files:
continue continue
if not Path(fname).exists():
continue
self.io.tool_output(f"Adding {fname} to git") self.io.tool_output(f"Adding {fname} to git")
self.repo.git.add(fname) self.repo.git.add(fname)