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`.
This commit is contained in:
Paul Gauthier 2023-07-24 13:45:10 -03:00
parent f87ec8c38b
commit df096272bc

View file

@ -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)