From 0cfbabb03beca5ae344e691a95b07d7e4a36807c Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 9 May 2023 11:31:52 -0700 Subject: [PATCH] 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 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. --- aider/coder.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index c11b8f412..1870f6a0f 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -64,10 +64,13 @@ class Coder: self.pretty = pretty def set_repo(self): - 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 num_repos = len(set(repo_paths)) if num_repos == 0: