aider: Refactored the if pretty: block in the get_dirty_files_and_diffs method into a separate method called get_diffs that takes relative_fname as an argument and accesses self.repo and self.pretty directly.

# Context:
USER: refactor the if pretty: repo.git.diff block into a method
ASSISTANT: I will refactor the `if pretty:` block in the `get_dirty_files_and_diffs` method into a separate method. Here's the plan:

1. Create a new method called `get_diffs` that takes `repo`, `relative_fname`, and `pretty` as arguments.
2. Move the `if pretty:` block from the `get_dirty_files_and_diffs` method to the new `get_diffs` method.
3. Replace the `if pretty:` block in the `get_dirty_files_and_diffs` method with a call to the new `get_diffs` method.

Here are the changes:

```python
aider/coder.py
<<<<<<< ORIGINAL
        def get_dirty_files_and_diffs(file_list):
            diffs = ""
            relative_dirty_files = []
            for fname in file_list:
                relative_fname = self
^C KeyboardInterrupt
USER: have the new method access self.repo and self.pretty; not as args
ASSISTANT: I will refactor the `if pretty:` block in the `get_dirty_files_and_diffs` method into a separate method called `get_diffs` that takes `relative_fname` as an argument and accesses `self.repo` and `self.pretty` directly. Here's the plan:

1. Create a new method called `get_diffs` that takes `relative_fname` as an argument.
2. Move the `if pretty:` block from the `get_dirty_files_and_diffs` method to the new `get_diffs` method.
3. Replace the `if pretty:` block in the `get_dirty_files_and_diffs` method with a call to the new `get_diffs` method.

Here are the changes:

```python
aider/coder.py
<<<<<<< ORIGINAL
        def get_dirty_files_and_diffs(file_list):
            diffs = ""
            relative_dirty_files = []
            for fname in file_list:
                relative_fname = self.get_rel_fname(fname)
                relative_dirty_files.append(relative_fname)

                try:
                    current_branch_commit_count = len(list(repo.iter_commits(repo.active_branch)))
                except git.exc.GitCommandError:
                    current_branch_commit_count = None

                if not current_branch_commit_count:
                    continue

                if self.pretty:
                    these_diffs = repo.git.diff("HEAD", "--color", "--", relative_fname)
                else:
                    these_diffs = repo.git.diff("HEAD", relative_fname)

                if these_diffs:
                    diffs += these_diffs + "\n"
=======
        def get_diffs(self, relative_fname):
            if self.pretty:
                these_diffs = self.repo.git.diff("HEAD", "--color", "--", relative_fname)
            else:
                these_diffs = self.repo.git.diff("HEAD", relative_fname)
            return these_diffs

        def get_dirty_files_and_diffs(self, file_list):
            diffs = ""
            relative_dirty_files = []
            for fname in file_list:
                relative_fname = self.get_rel_fname(fname)
                relative_dirty_files.append(relative_fname)

                try:
                    current_branch_commit_count = len(list(self.repo.iter_commits(self.repo.active_branch)))
                except git.exc.GitCommandError:
                    current_branch_commit_count = None

                if not current_branch_commit_count:
                    continue

                these_diffs = self.get_diffs(relative_fname)

                if these_diffs:
                    diffs += these_diffs + "\n"
>>>>>>> UPDATED
```
This commit is contained in:
Paul Gauthier 2023-05-15 14:10:57 -07:00
parent 7fd2c2b63d
commit 076003310e

View file

@ -469,7 +469,14 @@ class Coder:
if not repo.is_dirty():
return
def get_dirty_files_and_diffs(file_list):
def get_diffs(self, relative_fname):
if self.pretty:
these_diffs = self.repo.git.diff("HEAD", "--color", "--", relative_fname)
else:
these_diffs = self.repo.git.diff("HEAD", relative_fname)
return these_diffs
def get_dirty_files_and_diffs(self, file_list):
diffs = ""
relative_dirty_files = []
for fname in file_list:
@ -477,17 +484,14 @@ class Coder:
relative_dirty_files.append(relative_fname)
try:
current_branch_commit_count = len(list(repo.iter_commits(repo.active_branch)))
current_branch_commit_count = len(list(self.repo.iter_commits(self.repo.active_branch)))
except git.exc.GitCommandError:
current_branch_commit_count = None
if not current_branch_commit_count:
continue
if self.pretty:
these_diffs = repo.git.diff("HEAD", "--color", "--", relative_fname)
else:
these_diffs = repo.git.diff("HEAD", relative_fname)
these_diffs = self.get_diffs(relative_fname)
if these_diffs:
diffs += these_diffs + "\n"