aider: Set default config file path based on whether the current working directory is part of a Git repository or not.

# Context:
USER: where does configargparse look for the config file?
ASSISTANT: `configargparse` looks for the configuration file in the locations specified by the `add_config_file_help` and `default_config_files` arguments when creating an `ArgumentParser` instance. If these arguments are not provided, it does not look for a configuration file by default.

To specify the locations for the configuration file, you can do the following:

```python
parser = configargparse.ArgumentParser(
    description="aider - chat with GPT about your code",
    add_config_file_help=True,
    default_config_files=["./config.ini", "/etc/aider/config.ini"],
)
```

In this example, `configargparse` will look for the configuration file in the current directory with the name `config.ini` and in the `/etc/aider/` directory with the same name. If a configuration file is found, the values from the file will be used as default values for the arguments.
USER: can it search parent dirs for the conf file
ASSISTANT: Yes, `configargparse` can search parent directories for the configuration file. You can use a loop to generate a list of configuration file paths in parent directories and pass it to the `default_config_files` argument. Here's an example:

```python
import os

def generate_config_paths(filename):
    config_paths = []
    current_dir = os.path.abspath(os.getcwd())

    while True:
        config_path = os.path.join(current_dir, filename)
        config_paths.append(config_path)

        parent_dir = os.path.dirname(current_dir)
        if parent_dir == current_dir:
            break

        current_dir = parent_dir

    return config_paths

config_file_name = "config.ini"
config_paths = generate_config_paths(config_file_name)

parser = configargparse.ArgumentParser(
    description="aider - chat with GPT about your code",
    add_config_file_help=True,
    default_config_files=config_paths,
)
```

This code will generate a list of configuration file paths in parent directories with the given `config_file_name` and pass it to the `default_config_files` argument. `configargparse` will search for the configuration file in the specified paths.
USER: nah. instead, see if cwd is part of a git repo. if so, make the default be the working dir of the repo/.aider.conf.yml, otherwise expand ~/.aider.conf.yml
ASSISTANT: To achieve this, you can use the `git` command to check if the current working directory is part of a Git repository and then set the default configuration file path accordingly. Here's how you can do it:

```python
import os
import subprocess

def get_git_root():
    try:
        git_root = subprocess.check_output(["git", "rev-parse", "--
^C KeyboardInterrupt
USER: use import git
ASSISTANT: To use the `git` Python library, first, make sure it's installed. You can install it using pip:

```
pip install GitPython
```

Then, you can modify the code to use the `git` library to check if the current working directory is part of a Git repository and set the default configuration file path accordingly:

```python
import os
import git
import configargparse

def get_git_root():
    try:
        repo = git.Repo(search_parent_directories=True)
        return repo.working_tree_dir
    except git.InvalidGitRepositoryError:

^C KeyboardInterrupt
USER: as an edit block
ASSISTANT: First, install the `GitPython` library if you haven't already:

```
pip install GitPython
```

Then, update the `aider/main.py` file with the following changes:

```python
aider/main.py
<<<<<<< ORIGINAL
import sys
import configargparse
from dotenv import load_dotenv
from aider.coder import Coder
from aider.io import InputOutput
=======
import os
import sys
import git
import configargparse
from dotenv import load_dotenv
from aider.coder import Coder
from aider.io import InputOutput
>>>>>>> UPDATED

```

```python
aider/main.py
<<<<<<< ORIGINAL
parser = configargparse.ArgumentParser(description="aider - chat with GPT about your code")
=======
def get_git_root():
    try:
        repo = git.Repo(search_parent_directories=True)
        return repo.working_tree_dir
    except git.InvalidGitRepositoryError:
        return None

git_root = get_git_root()
if git_root:
    default_config_file = os.path.join(git_root, ".aider.conf.yml")
else:
    default_config_file = os.path.expanduser("~/.aider.conf.yml")

parser = configargparse.ArgumentParser(
    description="aider - chat with GPT about your code",
    add_config_file_help=True,
    default_config_files=[default_config_file],
)
>>>>>>> UPDATED
```

These changes will import the `git` library and define a `get_git_root()` function to find the Git repository root. The default configuration file path will be set to either the repository root with `.aider.conf.yml` or the user's home directory with `.aider.conf.yml`.
This commit is contained in:
Paul Gauthier 2023-05-26 15:42:44 -07:00
parent 80c195b323
commit e17265c998

View file

@ -1,4 +1,6 @@
import os
import sys
import git
import configargparse
from dotenv import load_dotenv
from aider.coder import Coder
@ -11,7 +13,24 @@ def main(args=None, input=None, output=None):
load_dotenv()
env_prefix = "AIDER_"
parser = configargparse.ArgumentParser(description="aider - chat with GPT about your code")
def get_git_root():
try:
repo = git.Repo(search_parent_directories=True)
return repo.working_tree_dir
except git.InvalidGitRepositoryError:
return None
git_root = get_git_root()
if git_root:
default_config_file = os.path.join(git_root, ".aider.conf.yml")
else:
default_config_file = os.path.expanduser("~/.aider.conf.yml")
parser = configargparse.ArgumentParser(
description="aider - chat with GPT about your code",
add_config_file_help=True,
default_config_files=[default_config_file],
)
parser.add_argument(
"files",
metavar="FILE",