feat: Implement setup_git_home function

This commit is contained in:
Paul Gauthier 2024-08-28 15:14:13 -07:00 committed by Paul Gauthier (aider)
parent 5b0a660f27
commit d05cd4b459
2 changed files with 53 additions and 41 deletions

View file

@ -39,43 +39,47 @@ def setup_git_home(io):
"Enter the number or name of the repository you want to work on,\n" "Enter the number or name of the repository you want to work on,\n"
"or enter the name of a new project to create:" "or enter the name of a new project to create:"
) )
choice = choice.strip()
try: try:
choice_num = int(choice) choice_num = int(choice)
if 1 <= choice_num <= len(git_repos): if 1 <= choice_num <= len(git_repos):
chosen_repo = git_repos[choice_num - 1] chosen_repo = git_repos[choice_num - 1]
os.chdir(chosen_repo.parent) git_root = chosen_repo.parent
return str(chosen_repo.parent) break
else: else:
io.tool_error(f"Please enter a number between 1 and {len(git_repos)}") io.tool_error(f"Please enter a number between 1 and {len(git_repos)}")
except ValueError: except ValueError:
choice_lower = choice.lower() choice_lower = choice.lower()
if choice_lower in repo_dict: if choice_lower in repo_dict:
chosen_repo = repo_dict[choice_lower] chosen_repo = repo_dict[choice_lower]
os.chdir(chosen_repo.parent) git_root = chosen_repo.parent
return str(chosen_repo.parent) break
else: elif choice:
# Assume it's a new project name # Assume it's a new project name
project_name = choice git_root = home / choice
new_dir = home / project_name break
try: else:
new_dir.mkdir(parents=True, exist_ok=True) return # no response
os.chdir(new_dir)
return str(new_dir) if git_root.exists():
except OSError as e: if git_root.is_dir():
io.tool_error(f"Error creating directory: {e}") os.chdir(git_root)
return None 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: try:
new_dir.mkdir(parents=True, exist_ok=True) io.tool_output(f"Making directory {git_root}")
os.chdir(new_dir) git_root.mkdir()
return str(new_dir)
except OSError as e: except OSError as e:
io.tool_error(f"Error creating directory: {e}") io.tool_error(f"Error creating directory {git_root}: {e}")
return None return None
make_new_repo(git_root, io)
os.chdir(git_root)
return git_root
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 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) 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): def setup_git(git_root, io):
repo = None 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) 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_name = None
user_email = None user_email = None

View file

@ -127,12 +127,12 @@ class TestMain(TestCase):
): ):
mock_home.return_value = Path("/home/user") mock_home.return_value = Path("/home/user")
mock_glob.return_value = [] 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) result = setup_git_home(mock_io.return_value)
mock_io.return_value.tool_output.assert_not_called() 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:" "Enter a name for your new project directory:"
) )
mock_mkdir.assert_called_once_with(parents=True, exist_ok=True) 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_home.return_value = Path("/home/user")
mock_glob.return_value = [] 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") mock_mkdir.side_effect = OSError("Permission denied")
result = setup_git_home(mock_io.return_value) result = setup_git_home(mock_io.return_value)
mock_io.return_value.tool_output.assert_not_called() 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:" "Enter a name for your new project directory:"
) )
mock_mkdir.assert_called_once_with(parents=True, exist_ok=True) mock_mkdir.assert_called_once_with(parents=True, exist_ok=True)