From 076003310e0634f011faeda278c2a78db921659f Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Mon, 15 May 2023 14:10:57 -0700 Subject: [PATCH] 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 ``` --- aider/coder.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index 3bb9911a1..344163e41 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -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"