From df096272bc791bf4dc060d33a47287e7caede8c4 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 24 Jul 2023 13:45:10 -0300 Subject: [PATCH] aider: Refactored `get_tracked_files` method to use `index.entries` instead of `index.diff` to enumerate staged files. # Aider chat conversation: USER: is there any way to enumerate the staged files using the index besides index.diff ASSISTANT: Yes, you can enumerate the staged files using the `index.entries` property of the `IndexFile` object. This property returns a dictionary-like object where the keys are the file paths and the values are `IndexEntry` objects. Here's how you can modify the `get_tracked_files` method in the `GitRepo` class to use `index.entries` instead of `index.diff`: ```python aider/repo.py <<<<<<< ORIGINAL def get_tracked_files(self): if not self.repo: return [] try: commit = self.repo.head.commit except ValueError: commit = None files = [] if commit: 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(None)] dump(staged_files) 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 ======= def get_tracked_files(self): if not self.repo: return [] try: commit = self.repo.head.commit except ValueError: commit = None files = [] if commit: 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 = [str(Path(PurePosixPath(path))) for path in index.entries.keys()] dump(staged_files) 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 make the `get_tracked_files` method return all files in the index, not just the ones that have been modified. If you only want to get the files that have been modified, you should stick with using `index.diff`. --- aider/repo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aider/repo.py b/aider/repo.py index dff2a8543..c8601a97c 100644 --- a/aider/repo.py +++ b/aider/repo.py @@ -157,7 +157,7 @@ class GitRepo: # Add staged files index = self.repo.index - staged_files = [item.a_path for item in index.diff(None)] + staged_files = [str(Path(PurePosixPath(path))) for path in index.entries.keys()] dump(staged_files) files.extend(staged_files)