diff --git a/aider/main.py b/aider/main.py index c55146923..87a624921 100644 --- a/aider/main.py +++ b/aider/main.py @@ -39,43 +39,47 @@ def setup_git_home(io): "Enter the number or name of the repository you want to work on,\n" "or enter the name of a new project to create:" ) - + choice = choice.strip() try: choice_num = int(choice) if 1 <= choice_num <= len(git_repos): chosen_repo = git_repos[choice_num - 1] - os.chdir(chosen_repo.parent) - return str(chosen_repo.parent) + 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] - os.chdir(chosen_repo.parent) - return str(chosen_repo.parent) - else: + git_root = chosen_repo.parent + break + elif choice: # Assume it's a new project name - project_name = choice - new_dir = home / project_name - try: - new_dir.mkdir(parents=True, exist_ok=True) - os.chdir(new_dir) - return str(new_dir) - except OSError as e: - io.tool_error(f"Error creating directory: {e}") - return None + git_root = home / choice + break + else: + return # no response + + 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 - project_name = io.user_input("Enter a name for your new project directory:") - new_dir = home / project_name try: - new_dir.mkdir(parents=True, exist_ok=True) - os.chdir(new_dir) - return str(new_dir) + io.tool_output(f"Making directory {git_root}") + git_root.mkdir() except OSError as e: - io.tool_error(f"Error creating directory: {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""" @@ -105,26 +109,34 @@ def guessed_wrong_repo(io, git_root, fnames, git_dname): return str(check_repo) +def make_new_repo(git_root, io): + repo = git.Repo.init(git_root) + io.tool_output(f"Git repository created in {git_root}") + check_gitignore(git_root, io, False) + return repo + + def setup_git(git_root, io): repo = None - if git_root: - repo = git.Repo(git_root) - else: - cwd = Path.cwd() - if cwd == Path.home(): - git_root = setup_git_home(io) - elif io.confirm_ask( - "No git repo found, create one to track aider's changes (recommended)?" - ): - git_root = str(cwd.resolve()) - repo = git.Repo.init(git_root) - io.tool_output("Git repository created in the current working directory.") - check_gitignore(git_root, io, False) - else: - return - if not repo and git_root: + if not git_root and Path.cwd() == Path.home(): + git_root = setup_git_home(io) + + if not git_root: + git_root = Path.cwd() + + try: 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)?" + ): + repo = make_new_repo(git_root, io) + + if not repo: + return user_name = None user_email = None diff --git a/tests/basic/test_main.py b/tests/basic/test_main.py index 759dfe99b..d5021fc10 100644 --- a/tests/basic/test_main.py +++ b/tests/basic/test_main.py @@ -127,12 +127,12 @@ class TestMain(TestCase): ): mock_home.return_value = Path("/home/user") mock_glob.return_value = [] - mock_io.return_value.user_input.return_value = "new_project" + mock_io.return_value.prompt_ask.return_value = "new_project" result = setup_git_home(mock_io.return_value) mock_io.return_value.tool_output.assert_not_called() - mock_io.return_value.user_input.assert_called_once_with( + mock_io.return_value.prompt_ask.assert_called_once_with( "Enter a name for your new project directory:" ) mock_mkdir.assert_called_once_with(parents=True, exist_ok=True) @@ -149,13 +149,13 @@ class TestMain(TestCase): ): mock_home.return_value = Path("/home/user") mock_glob.return_value = [] - mock_io.return_value.user_input.return_value = "new_project" + mock_io.return_value.prompt_ask.return_value = "new_project" mock_mkdir.side_effect = OSError("Permission denied") result = setup_git_home(mock_io.return_value) mock_io.return_value.tool_output.assert_not_called() - mock_io.return_value.user_input.assert_called_once_with( + mock_io.return_value.prompt_ask.assert_called_once_with( "Enter a name for your new project directory:" ) mock_mkdir.assert_called_once_with(parents=True, exist_ok=True)