From 540c0b10b9961847b6ea398a9e808bfe15926763 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Tue, 9 May 2023 13:04:43 -0700 Subject: [PATCH] Add `--show-diffs` parameter and only print diffs if it is set to `True`. # Context: USER: add a --show-diffs parameter which defaults to false. allow an env var to override the default. pass this to Coder.__init__() and use it to set a self.show_diffs. only self.console.print(Text(diffs)) if it is true. ASSISTANT: To add a `--show-diffs` parameter with a default value of `False` and allow an environment variable to override the default, we will make changes to `aider/main.py`. We will also pass this value to `Coder.__init__()` and use it to set `self.show_diffs`. Finally, we will only print the diffs if `self.show_diffs` is `True`. Here are the changes: ```python aider/main.py <<<<<<< ORIGINAL default=bool(int(os.environ.get(f"{env_prefix}COMMIT_DIRTY", 0))), ) args = parser.parse_args() ======= default=bool(int(os.environ.get(f"{env_prefix}COMMIT_DIRTY", 0))), ) parser.add_argument( "--show-diffs", action="store_true", help=f"Show diffs when committing changes (default: False, ${env_prefix}SHOW_DIFFS)", default=bool(int(os.environ.get(f"{env_prefix}SHOW_DIFFS", 0))), ) args = parser.parse_args() >>>>>>> UPDATED ``` ```python aider/main.py <<<<<<< ORIGINAL coder = Coder(args.model, fnames, pretty, args.history_file) ======= coder = Coder(args.model, fnames, pretty, args.history_file, args.show_diffs) >>>>>>> UPDATED ``` ```python aider/coder.py <<<<<<< ORIGINAL def __init__(self, main_model, files, pretty, history_file): ======= def __init__(self, main_model, files, pretty, history_file, show_diffs): >>>>>>> UPDATED ``` ```python aider/coder.py <<<<<<< ORIGINAL self.pretty = pretty ======= self.pretty = pretty self.show_diffs = show_diffs >>>>>>> UPDATED ``` ```python aider/coder.py <<<<<<< ORIGINAL self.console.print(Text(diffs)) ======= if self.show_diffs: self.console.print(Text(diffs)) >>>>>>> UPDATED ``` These changes will add the `--show-diffs` parameter and only print the diffs if the parameter is set to `True`. --- aider/coder.py | 8 ++++---- aider/main.py | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/aider/coder.py b/aider/coder.py index e98ecd2b9..08385398b 100755 --- a/aider/coder.py +++ b/aider/coder.py @@ -31,7 +31,7 @@ class Coder: last_modified = 0 repo = None - def __init__(self, main_model, files, pretty, history_file): + def __init__(self, main_model, files, pretty, history_file, show_diffs): self.history_file = history_file if pretty: @@ -62,7 +62,7 @@ class Coder: ) self.pretty = pretty - + self.show_diffs = show_diffs def set_repo(self): repo_paths = [] for fname in self.fnames: @@ -372,8 +372,8 @@ class Coder: self.last_modified = self.get_last_modified() return - self.console.print(Text(diffs)) - + if self.show_diffs: + self.console.print(Text(diffs)) diffs = "# Diffs:\n" + diffs # for fname in dirty_fnames: diff --git a/aider/main.py b/aider/main.py index 5449eaf6a..0d1d3afbf 100644 --- a/aider/main.py +++ b/aider/main.py @@ -53,12 +53,17 @@ def main(): help=f"On launch, commit dirty files w/o confirmation (default: False, ${env_prefix}COMMIT_DIRTY)", # noqa: E501 default=bool(int(os.environ.get(f"{env_prefix}COMMIT_DIRTY", 0))), ) + parser.add_argument( + "--show-diffs", + action="store_true", + help=f"Show diffs when committing changes (default: False, ${env_prefix}SHOW_DIFFS)", + default=bool(int(os.environ.get(f"{env_prefix}SHOW_DIFFS", 0))), + ) args = parser.parse_args() - fnames = args.files pretty = args.pretty - coder = Coder(args.model, fnames, pretty, args.history_file) + coder = Coder(args.model, fnames, pretty, args.history_file, args.show_diffs) coder.commit(ask=not args.commit_dirty, prefix="WIP: ") if args.apply: