From 19e47875022e9d7a6c22dd31d7eff2d5e2600c21 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 27 Sep 2023 18:30:26 -0700 Subject: [PATCH] aider: Refactored `check_gitignore` function to use `gitpython` library to check if the `.aider` pattern is already ignored by the repository before adding it to `.gitignore`. # Aider chat conversation: USER: in `check_gitignore` use gitpython to see if the repo is already ignoring `.aider`. if so, don't add anything to gitignore ASSISTANT: To implement this change, we need to modify the `check_gitignore` function in the `aider/main.py` file. We will use the `gitpython` library to check if the `.aider` pattern is already ignored by the repository. If it is, we won't add anything to the `.gitignore` file. Here are the steps we need to take: 1. Import the `git` module at the top of the file. 2. In the `check_gitignore` function, create a `git.Repo` object for the current repository. 3. Use the `Repo.ignored` method to check if the `.aider` pattern is already ignored. 4. Only add the `.aider` pattern to the `.gitignore` file if it's not already ignored. Here is the *edit block* for this change: ```python aider/main.py <<<<<<< HEAD def check_gitignore(git_root, io, ask=True): if not git_root: return pat = ".aider*" gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): content = io.read_text(gitignore_file) if pat in content.splitlines(): return else: content = "" if ask and not io.confirm_ask(f"Add {pat} to .gitignore (recommended)?"): return if content and not content.endswith("\n"): content += "\n" content += pat + "\n" io.write_text(gitignore_file, content) io.tool_output(f"Added {pat} to .gitignore") ======= import git def check_gitignore(git_root, io, ask=True): if not git_root: return pat = ".aider*" gitignore_file = Path(git_root) / ".gitignore" if gitignore_file.exists(): content = io.read_text(gitignore_file) if pat in content.splitlines(): return else: content = "" repo = git.Repo(git_root) if repo.is_ignored(pat): return if ask and not io.confirm_ask(f"Add {pat} to .gitignore (recommended)?"): return if content and not content.endswith("\n"): content += "\n" content += pat + "\n" io.write_text(gitignore_file, content) io.tool_output(f"Added {pat} to .gitignore") >>>>>>> updated ``` Please note that this change assumes that the `gitpython` library is already installed in your environment. If it's not, you will need to install it using pip (`pip install gitpython`). --- aider/main.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/aider/main.py b/aider/main.py index 5dee72fb0..00489504c 100644 --- a/aider/main.py +++ b/aider/main.py @@ -70,6 +70,8 @@ def setup_git(git_root, io): return repo.working_tree_dir +import git + def check_gitignore(git_root, io, ask=True): if not git_root: return @@ -84,6 +86,10 @@ def check_gitignore(git_root, io, ask=True): else: content = "" + repo = git.Repo(git_root) + if repo.is_ignored(pat): + return + if ask and not io.confirm_ask(f"Add {pat} to .gitignore (recommended)?"): return