diff --git a/aider/main.py b/aider/main.py index b7508d4d8..775e49762 100644 --- a/aider/main.py +++ b/aider/main.py @@ -42,9 +42,11 @@ def setup_git(git_root, io): def check_gitignore(git_root, io): + if not git_root: + return + pat = ".aider*" - updated_ignore = None gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): content = gitignore_file.read_text(encoding="utf-8") @@ -56,7 +58,7 @@ def check_gitignore(git_root, io): if not io.confirm_ask(f"Add {pat} to .gitignore (recommended)?"): return - if not content.endswith("\n"): + if content and not content.endswith("\n"): content += "\n" content += pat + "\n" gitignore_file.write_text(content, encoding="utf-8") @@ -366,8 +368,6 @@ def main(args=None, input=None, output=None): if args.git: git_root = setup_git(git_root, io) - - if git_root: check_gitignore(git_root, io) def scrub_sensitive_info(text): diff --git a/tests/test_main.py b/tests/test_main.py index 847b47c08..ed818cc84 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -12,7 +12,7 @@ from prompt_toolkit.output import DummyOutput from aider.dump import dump # noqa: F401 from aider.io import InputOutput -from aider.main import main, setup_git +from aider.main import check_gitignore, main, setup_git from tests.utils import make_repo @@ -80,6 +80,22 @@ class TestMain(TestCase): self.assertTrue(git.Repo(self.tempdir)) + def test_check_gitignore(self): + make_repo() + io = InputOutput(pretty=False, yes=True) + cwd = Path.cwd() + gitignore = cwd / ".gitignore" + + self.assertFalse(gitignore.exists()) + check_gitignore(cwd, io) + self.assertTrue(gitignore.exists()) + + self.assertEqual(".aider*", gitignore.read_text().splitlines()[0]) + + gitignore.write_text("one\ntwo\n") + check_gitignore(cwd, io) + self.assertEqual("one\ntwo\n.aider*\n", gitignore.read_text()) + def test_main_args(self): with patch("aider.main.Coder.create") as MockCoder: # --yes will just ok the git repo without blocking on input