From 89542d2139dbc822d7d4539f72191b01ad00caa6 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 24 Jul 2023 13:43:05 -0300 Subject: [PATCH] aider: Handle the case where the repository has never had any commits by using `index.diff(None)` to get the staged files. # Aider chat conversation: USER: index.diff("HEAD") fails when the repo has never had any commits ASSISTANT: To handle the case where the repository has never had any commits, we can add a try-except block around the `index.diff("HEAD")` call in the `get_tracked_files` method of the `GitRepo` class in the `aider/repo.py` file. If a `git.exc.BadName` exception is raised, we can assume that there are no commits and return an empty list for the staged files. Here is the proposed change: ```python aider/repo.py <<<<<<< ORIGINAL # Add staged files index = self.repo.index staged_files = [item.a_path for item in index.diff("HEAD")] files.extend(staged_files) ======= # Add staged files ^C KeyboardInterrupt USER: there can be staged files when there are not commits ASSISTANT: You're correct. If there are no commits, `index.diff("HEAD")` will fail, but there can still be staged files. We can use `index.diff(None)` to get the staged files in this case. Here's the proposed change: ```python aider/repo.py <<<<<<< ORIGINAL # Add staged files index = self.repo.index staged_files = [item.a_path for item in index.diff("HEAD")] files.extend(staged_files) ======= # Add staged files index = self.repo.index try: staged_files = [item.a_path for item in index.diff("HEAD")] except git.exc.BadName: staged_files = [item.a_path for item in index.diff(None)] files.extend(staged_files) >>>>>>> UPDATED ``` --- aider/repo.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aider/repo.py b/aider/repo.py index 0615e3b39..15e89a54f 100644 --- a/aider/repo.py +++ b/aider/repo.py @@ -156,7 +156,10 @@ class GitRepo: # Add staged files index = self.repo.index - staged_files = [item.a_path for item in index.diff("HEAD")] + try: + staged_files = [item.a_path for item in index.diff("HEAD")] + except git.exc.BadName: + staged_files = [item.a_path for item in index.diff(None)] files.extend(staged_files) # convert to appropriate os.sep, since git always normalizes to /