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`.
This commit is contained in:
Paul Gauthier 2023-05-14 10:14:20 -07:00
parent a8acca9366
commit 0b8690654e
2 changed files with 2 additions and 2 deletions

View file

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

View file

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