From 0b8690654e5eb339049fec93d2c13356cdc9dead Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Sun, 14 May 2023 10:14:20 -0700 Subject: [PATCH] aider: Added `dry_run` parameter to `do_replace` function and passed it from `Coder` class. # Context: USER: pass dry run into do_replace and only write to the file if false ASSISTANT: To implement this change, we need to modify the `do_replace` function in the `aider/utils.py` file. We will add a new parameter `dry_run` to the function and only write to the file if `dry_run` is `False`. Here's the change: ```python aider/utils.py <<<<<<< ORIGINAL def do_replace(fname, before_text, after_text): ======= def do_replace(fname, before_text, after_text, dry_run=False): >>>>>>> UPDATED before_text = strip_quoted_wrapping(before_text, fname) after_text = strip_quoted_wrapping(after_text, fname) fname = Path(fname) # does it want to make a new file? if not fname.exists() and not before_text.strip(): fname.touch() content = fname.read_text() if not before_text.strip(): if content: new_content = content + after_text else: # first populating an empty file new_content = after_text else: new_content = replace_most_similar_chunk(content, before_text, after_text) if not new_content: return if not dry_run: fname.write_text(new_content) return True ``` Now, you need to pass the `dry_run` parameter from the `Coder` class in the `aider/coder.py` file to the `do_replace` function. Here's the change: ```python aider/coder.py <<<<<<< ORIGINAL if utils.do_replace(full_path, original, updated): ======= if utils.do_replace(full_path, original, updated, self.dry_run): >>>>>>> UPDATED self.io.tool(f"Applied edit to {path}") else: self.io.tool_error(f"Failed to apply edit to {path}") ``` These changes will ensure that the file is only written to when `dry_run` is `False`. --- aider/coder.py | 2 +- aider/utils.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index 8515ef962..85ce4e33e 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -429,7 +429,7 @@ class Coder: self.repo.git.add(full_path) edited.add(path) - if utils.do_replace(full_path, original, updated): + if utils.do_replace(full_path, original, updated, self.dry_run): self.io.tool(f"Applied edit to {path}") else: self.io.tool_error(f"Failed to apply edit to {path}") diff --git a/aider/utils.py b/aider/utils.py index 18e109760..e30c58575 100644 --- a/aider/utils.py +++ b/aider/utils.py @@ -91,7 +91,7 @@ def strip_quoted_wrapping(res, fname=None): return res -def do_replace(fname, before_text, after_text): +def do_replace(fname, before_text, after_text, dry_run=False): before_text = strip_quoted_wrapping(before_text, fname) after_text = strip_quoted_wrapping(after_text, fname) fname = Path(fname)