Commit graph

165 commits

Author SHA1 Message Date
Paul Gauthier
589e7eefea We always want diffs of working-dir + index versus repo #184 2023-08-08 09:00:19 -03:00
Paul Gauthier
64c50bab48 Accept either a git dname or a list of fnames 2023-07-24 17:31:00 -03:00
Paul Gauthier
66da82094d use index.entries 2023-07-24 13:47:33 -03:00
Paul Gauthier
df096272bc 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`.
2023-07-24 13:45:10 -03:00
Paul Gauthier
f87ec8c38b Refactor getting staged files to handle ValueError. 2023-07-24 13:44:22 -03:00
Paul Gauthier
89542d2139 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
```
2023-07-24 13:43:05 -03:00
Paul Gauthier
85166341d1 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.
2023-07-24 13:03:38 -03:00
Paul Gauthier
661a521693 aider.repo.simple_send_with_retries 2023-07-21 16:20:27 -03:00
Paul Gauthier
9f8ea93ce6 AiderRepo -> GitRepo 2023-07-21 16:08:27 -03:00
Paul Gauthier
6d22dd06f8 aider: Refactored test_main_with_empty_dir_new_file to use @patch decorator to mock get_commit_message method.
# Aider chat conversation:

USER: @patch("aider.repo.AiderRepo.get_commit_message") is not patching it

ASSISTANT: It seems like you're having trouble with the `@patch` decorator not correctly patching the `get_commit_message` method of the `AiderRepo` class in your tests.

To help you with this, I need to understand the context better. Could you please provide more details about the issue? Are you getting any error messages? If so, could you share them?

USER: in `test_main_with_empty_dir_new_file` i don't want `get_commit_message` to be actually called. just to return a mock value

ASSISTANT: The `@patch` decorator from the `unittest.mock` module is used to replace the actual method with a mock method during testing. If `get_commit_message` is still being called, it might be due to the incorrect import path in the `@patch` decorator.

In your case, the `@patch` decorator should be used as follows:

```python
@patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
```

This will replace the `get_commit_message` method with a mock method that always returns "commit message!".

Here is the updated `test_main_with_empty_dir_new_file` method:

```python
tests/test_main.py
<<<<<<< ORIGINAL
    @patch("aider.repo.AiderRepo.get_commit_message")
    def test_main_with_empty_dir_new_file(self, get_commit_message):
        get_commit_message.return_value = "commit message!"
        main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
=======
    @patch("aider.repo.AiderRepo.get_commit_message", return_value="commit message!")
    def test_main_with_empty_dir_new_file(self, get_commit_message):
        main(["foo.txt", "--yes","--no-git"], input=DummyInput(), output=DummyOutput())
        self.assertTrue(os.path.exists("foo.txt"))
>>>>>>> UPDATED
```

This change should ensure that the actual `get_commit_message` method is not called during the test. Instead, the mock method will be used, returning the specified mock value.
2023-07-21 15:56:27 -03:00
Paul Gauthier
74875eac6e sssh 2023-07-21 12:58:23 -03:00
Paul Gauthier
14b24dc2fd works 2023-07-21 12:57:01 -03:00
Paul Gauthier
296e7614c4 wip 2023-07-21 12:06:32 -03:00
Paul Gauthier
23beb7cb5d wip 2023-07-21 11:49:19 -03:00
Paul Gauthier
e34e6ff897 wip 2023-07-21 11:14:15 -03:00