From 85166341d18553c72a28f1db3adb25097431b018 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 24 Jul 2023 13:03:38 -0300 Subject: [PATCH] aider: Add staged files to the list of tracked files in the `get_tracked_files` method. # Aider chat conversation: USER: doing commit.tree.traverse() is not picking up files which have been git added but not yet committed ASSISTANT: To include files that have been staged but not yet committed, you need to use the `git diff --cached` command. This command shows the changes between the index and your last commit; in other words, what you would be committing if you run "git commit" without "-a" option. However, this change requires modification of the `aider/repo.py` file, which is currently read-only. Could you please make it read-write so that I can propose the necessary changes? USER: I added these *read-write* files: aider/repo.py ASSISTANT: Great, now that we have write access to `aider/repo.py`, we can modify the `get_tracked_files` method to include files that have been staged but not yet committed. Here are the steps: 1. We will use the `git diff --cached --name-only` command to get a list of staged files. This command lists the file names that have been added (staged) but not yet committed. 2. We will add these staged files to the list of tracked files. Here is the *edit block* for the changes: ```python aider/repo.py <<<<<<< ORIGINAL def get_tracked ^C KeyboardInterrupt USER: is there a way to use traverse? ASSISTANT: Yes, you can use the `git.IndexFile` object to traverse the staged files. The `IndexFile` object represents the git index, which is where files are staged before they are committed. Here are the steps: 1. Get the `IndexFile` object from the `Repo` object. 2. Traverse the `IndexFile` object to get the staged files. Here is the *edit block* for the changes: ```python aider/repo.py <<<<<<< ORIGINAL def get_tracked_files(self): if not self.repo: return [] try: commit = self.repo.head.commit except ValueError: return set() files = [] for blob in commit.tree.traverse(): if blob.type == "blob": # blob is a file files.append(blob.path) # convert to appropriate os.sep, since git always normalizes to / res = set(str(Path(PurePosixPath(path))) for path in files) return res ======= def get_tracked_files(self): if not self.repo: return [] try: commit = self.repo.head.commit except ValueError: return set() files = [] for blob in commit.tree.traverse(): if blob.type == "blob": # blob is a file files.append(blob.path) # Add staged files index = self.repo.index staged_files = [item.a_path for item in index.diff("HEAD")] files.extend(staged_files) # convert to appropriate os.sep, since git always normalizes to / res = set(str(Path(PurePosixPath(path))) for path in files) return res >>>>>>> UPDATED ``` This change will ensure that the `get_tracked_files` method returns both committed and staged files. --- aider/repo.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/aider/repo.py b/aider/repo.py index 5ae06baed..0615e3b39 100644 --- a/aider/repo.py +++ b/aider/repo.py @@ -154,6 +154,11 @@ class GitRepo: if blob.type == "blob": # blob is a file files.append(blob.path) + # Add staged files + index = self.repo.index + staged_files = [item.a_path for item in index.diff("HEAD")] + files.extend(staged_files) + # convert to appropriate os.sep, since git always normalizes to / res = set(str(Path(PurePosixPath(path))) for path in files)