mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-20 12:24:59 +00:00
refactor: simplify git repository setup process
This commit is contained in:
parent
12d2ba6f3e
commit
0541513c7a
2 changed files with 4 additions and 152 deletions
|
@ -22,75 +22,6 @@ from aider.versioncheck import check_version, install_from_main_branch, install_
|
|||
from .dump import dump # noqa: F401
|
||||
|
||||
|
||||
def setup_git_home(io):
|
||||
home = Path.home()
|
||||
git_repos = sorted(home.glob("*/.git"))
|
||||
git_repos = []
|
||||
git_root = None
|
||||
|
||||
if git_repos:
|
||||
io.tool_output("Found git repositories in your home directory:")
|
||||
repo_dict = {}
|
||||
for i, repo in enumerate(git_repos, 1):
|
||||
repo_name = repo.parent.name
|
||||
io.tool_output(f"{i}. {repo_name}")
|
||||
repo_dict[repo_name.lower()] = repo
|
||||
|
||||
while True:
|
||||
choice = io.prompt_ask(
|
||||
"Enter the number or name of the repository you want to work on,\n"
|
||||
"or enter the name of a new directory to create:"
|
||||
)
|
||||
choice = choice.strip()
|
||||
try:
|
||||
choice_num = int(choice)
|
||||
if 1 <= choice_num <= len(git_repos):
|
||||
chosen_repo = git_repos[choice_num - 1]
|
||||
git_root = chosen_repo.parent
|
||||
break
|
||||
else:
|
||||
io.tool_error(f"Please enter a number between 1 and {len(git_repos)}")
|
||||
except ValueError:
|
||||
choice_lower = choice.lower()
|
||||
if choice_lower in repo_dict:
|
||||
chosen_repo = repo_dict[choice_lower]
|
||||
git_root = chosen_repo.parent
|
||||
break
|
||||
elif choice:
|
||||
# Assume it's a new project name
|
||||
git_root = home / choice
|
||||
break
|
||||
else:
|
||||
return # no response
|
||||
else:
|
||||
choice = io.prompt_ask("Enter a directory name to create a new project:")
|
||||
choice = choice.strip()
|
||||
if choice:
|
||||
git_root = home / choice
|
||||
|
||||
if not git_root:
|
||||
return
|
||||
|
||||
if git_root.exists():
|
||||
if git_root.is_dir():
|
||||
os.chdir(git_root)
|
||||
return git_root
|
||||
else:
|
||||
io.tool_error(f"{git_root} exists, and is not a directory.")
|
||||
return
|
||||
|
||||
try:
|
||||
io.tool_output(f"Making directory {git_root}")
|
||||
git_root.mkdir()
|
||||
except OSError as e:
|
||||
io.tool_error(f"Error creating directory {git_root}: {e}")
|
||||
return None
|
||||
|
||||
make_new_repo(git_root, io)
|
||||
os.chdir(git_root)
|
||||
return git_root
|
||||
|
||||
|
||||
def get_git_root():
|
||||
"""Try and guess the git repo, since the conf.yml can be at the repo root"""
|
||||
try:
|
||||
|
@ -129,22 +60,10 @@ def make_new_repo(git_root, io):
|
|||
def setup_git(git_root, io):
|
||||
repo = None
|
||||
|
||||
if not git_root and Path.cwd() == Path.home():
|
||||
git_root = setup_git_home(io)
|
||||
if not git_root:
|
||||
return # don't make a .git in $HOME
|
||||
|
||||
if not git_root:
|
||||
git_root = Path.cwd()
|
||||
|
||||
try:
|
||||
if git_root:
|
||||
repo = git.Repo(git_root)
|
||||
except git.exc.InvalidGitRepositoryError:
|
||||
pass
|
||||
|
||||
if not repo and io.confirm_ask(
|
||||
"No git repo found, create one to track aider's changes (recommended)?"
|
||||
):
|
||||
elif io.confirm_ask("No git repo found, create one to track aider's changes (recommended)?"):
|
||||
git_root = str(Path.cwd().resolve())
|
||||
repo = make_new_repo(git_root, io)
|
||||
|
||||
if not repo:
|
||||
|
|
|
@ -14,7 +14,7 @@ from prompt_toolkit.output import DummyOutput
|
|||
from aider.coders import Coder
|
||||
from aider.dump import dump # noqa: F401
|
||||
from aider.io import InputOutput
|
||||
from aider.main import check_gitignore, main, setup_git, setup_git_home
|
||||
from aider.main import check_gitignore, main, setup_git
|
||||
from aider.utils import GitTemporaryDirectory, IgnorantTemporaryDirectory, make_repo
|
||||
|
||||
|
||||
|
@ -619,70 +619,3 @@ class TestMain(TestCase):
|
|||
return_coder=True,
|
||||
)
|
||||
self.assertTrue(coder.suggest_shell_commands)
|
||||
|
||||
@patch("aider.main.InputOutput")
|
||||
def test_setup_git_home_existing_repo(self, mock_io):
|
||||
mock_io_instance = mock_io.return_value
|
||||
mock_io_instance.prompt_ask.return_value = "1"
|
||||
|
||||
with IgnorantTemporaryDirectory() as temp_home:
|
||||
with patch("aider.main.Path.home", return_value=Path(temp_home)):
|
||||
# Create actual repo1 subdirectory with .git folder
|
||||
Path(temp_home, "repo1", ".git").mkdir(parents=True)
|
||||
|
||||
result = setup_git_home(mock_io_instance)
|
||||
|
||||
self.assertEqual(result, Path(temp_home) / "repo1")
|
||||
mock_io_instance.tool_output.assert_any_call(
|
||||
"Found git repositories in your home directory:"
|
||||
)
|
||||
mock_io_instance.prompt_ask.assert_called()
|
||||
|
||||
@patch("aider.main.InputOutput")
|
||||
@patch("aider.main.make_new_repo")
|
||||
def test_setup_git_home_new_repo(self, mock_make_new_repo, mock_io):
|
||||
mock_io_instance = mock_io.return_value
|
||||
mock_io_instance.prompt_ask.return_value = "new_project"
|
||||
|
||||
with IgnorantTemporaryDirectory() as temp_home:
|
||||
with patch("aider.main.Path.home", return_value=Path(temp_home)):
|
||||
Path(temp_home, "repo1", ".git").mkdir(parents=True)
|
||||
result = setup_git_home(mock_io_instance)
|
||||
|
||||
self.assertEqual(result, Path(temp_home) / "new_project")
|
||||
mock_make_new_repo.assert_called_with(
|
||||
Path(temp_home) / "new_project", mock_io_instance
|
||||
)
|
||||
|
||||
@patch("aider.main.InputOutput")
|
||||
@patch("aider.main.Path")
|
||||
def test_setup_git_home_no_repos(self, mock_path, mock_io):
|
||||
mock_io_instance = mock_io.return_value
|
||||
mock_path.home.return_value.glob.return_value = []
|
||||
|
||||
result = setup_git_home(mock_io_instance)
|
||||
|
||||
self.assertIsNone(result)
|
||||
mock_io_instance.tool_output.assert_not_called()
|
||||
mock_io_instance.prompt_ask.assert_not_called()
|
||||
|
||||
@patch("aider.main.InputOutput")
|
||||
def test_setup_git_home_invalid_choice(self, mock_io):
|
||||
mock_io_instance = mock_io.return_value
|
||||
mock_io_instance.prompt_ask.side_effect = ["3", "1"]
|
||||
|
||||
with IgnorantTemporaryDirectory() as temp_home:
|
||||
with patch("aider.main.Path.home", return_value=Path(temp_home)):
|
||||
# Create actual repo1 and repo2 subdirectories with .git folders
|
||||
Path(temp_home, "repo1", ".git").mkdir(parents=True)
|
||||
Path(temp_home, "repo2", ".git").mkdir(parents=True)
|
||||
|
||||
result = setup_git_home(mock_io_instance)
|
||||
|
||||
self.assertEqual(result, Path(temp_home) / "repo1")
|
||||
mock_io_instance.tool_error.assert_called_with(
|
||||
"Please enter a number between 1 and 2"
|
||||
)
|
||||
mock_io_instance.tool_output.assert_any_call(
|
||||
"Found git repositories in your home directory:"
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue