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`).
This commit is contained in:
Paul Gauthier 2023-09-27 18:30:26 -07:00
parent 14eb12364d
commit 19e4787502

View file

@ -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