Handle InvalidGitRepositoryError by replacing list comprehension with for loop and try-except.

# Context:
USER: unroll the list comprehension so we can try/catch the exception below which is thrown for files not in any git repo

  File "/Users/gauthier/Projects/aider/aider/coder.py", line 68, in <genexpr>
    git.Repo(fname, search_parent_directories=True).git_dir
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/gauthier/tmp/try-aider/.venv/lib/python3.11/site-packages/git/repo/base.py", line 265, in __init__
    raise InvalidGitRepositoryError(epath)
git.exc.InvalidGitRepositoryError: /Users/gauthier/tmp/try-aider/tmp.py

ASSISTANT: To unroll the list comprehension and handle the exception, we can replace it with a for loop and use try-except to catch the `InvalidGitRepositoryError`.

```python
aider/coder.py
<<<<<<< ORIGINAL
        repo_paths = list(
            git.Repo(fname, search_parent_directories=True).git_dir
            for fname in self.fnames
        )
=======
        repo_paths = []
        for fname in self.fnames:
            try:
                repo_path = git.Repo(fname, search_parent_directories=True).git_dir
                repo_paths.append(repo_path)
            except git.exc.InvalidGitRepositoryError:
                pass
>>>>>>> UPDATED
```

This change will catch the `InvalidGitRepositoryError` and continue processing the remaining files.
This commit is contained in:
Paul Gauthier 2023-05-09 11:31:52 -07:00
parent 132e0a687f
commit 0cfbabb03b

View file

@ -64,10 +64,13 @@ class Coder:
self.pretty = pretty self.pretty = pretty
def set_repo(self): def set_repo(self):
repo_paths = list( repo_paths = []
git.Repo(fname, search_parent_directories=True).git_dir for fname in self.fnames:
for fname in self.fnames try:
) repo_path = git.Repo(fname, search_parent_directories=True).git_dir
repo_paths.append(repo_path)
except git.exc.InvalidGitRepositoryError:
pass
num_repos = len(set(repo_paths)) num_repos = len(set(repo_paths))
if num_repos == 0: if num_repos == 0: