From 5cd7d9d7431785e86d2b50844290e8cc02bd6c49 Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Wed, 5 Jul 2023 21:37:22 -0700 Subject: [PATCH] aider: aider: Modified cmd_add to skip file creation if word contains glob characters. # Context: USER: if word doesn't match anything, don't ask to add it if it contains glob characters ASSISTANT: To implement this change, we need to modify the `aider/commands.py` file. Here's the step-by-step process: 1. In the `cmd_add` method, before asking to create a file, check if the word contains any glob characters (`*`, `?`, `[`, `]`). 2. If it does, skip the creation process and print an error message. Here's the code change: ```python aider/commands.py <<<<<<< ORIGINAL if not matched_files: if self.coder.repo is not None: create_file = self.io.confirm_ask( ( f"No files matched '{word}'. Do you want to create the file and add it" " to git?" ), ) else: create_file = self.io.confirm_ask( f"No files matched '{word}'. Do you want to create the file?" ) if create_file: with open(os.path.join(self.coder.root, word), "w"): pass matched_files = [word] if self.coder.repo is not None: self.coder.repo.git.add(os.path.join(self.coder.root, word)) commit_message = f"aider: Created and added {word} to git." self.coder.repo.git.commit("-m", commit_message, "--no-verify") else: self.io.tool_error(f"No files matched '{word}'") ======= if not matched_files: if any(char in word for char in "*?[]"): self.io.tool_error(f"No files matched '{word}' and it contains glob characters") else: if self.coder.repo is not None: create_file = self.io.confirm_ask( ( f"No files matched '{word}'. Do you want to create the file and add it" " to git?" ), ) else: create_file = self.io.confirm_ask( f"No files matched '{word}'. Do you want to create the file?" ) if create_file: with open(os.path.join(self.coder.root, word), "w"): pass matched_files = [word] if self.coder.repo is not None: self.coder.repo.git.add(os.path.join(self.coder.root, word)) commit_message = f"aider: Created and added {word} to git." self.coder.repo.git.commit("-m", commit_message, "--no-verify") else: self.io.tool_error(f"No files matched '{word}'") >>>>>>> UPDATED ``` Please let me know if you need any further changes. --- aider/commands.py | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/aider/commands.py b/aider/commands.py index 970a89ab6..83715b01f 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -225,28 +225,31 @@ class Commands: matched_files = glob.glob(word, recursive=True) if not matched_files: - if self.coder.repo is not None: - create_file = self.io.confirm_ask( - ( - f"No files matched '{word}'. Do you want to create the file and add it" - " to git?" - ), - ) + if any(char in word for char in "*?[]"): + self.io.tool_error(f"No files matched '{word}' and it contains glob characters") else: - create_file = self.io.confirm_ask( - f"No files matched '{word}'. Do you want to create the file?" - ) - - if create_file: - with open(os.path.join(self.coder.root, word), "w"): - pass - matched_files = [word] if self.coder.repo is not None: - self.coder.repo.git.add(os.path.join(self.coder.root, word)) - commit_message = f"aider: Created and added {word} to git." - self.coder.repo.git.commit("-m", commit_message, "--no-verify") - else: - self.io.tool_error(f"No files matched '{word}'") + create_file = self.io.confirm_ask( + ( + f"No files matched '{word}'. Do you want to create the file and add it" + " to git?" + ), + ) + else: + create_file = self.io.confirm_ask( + f"No files matched '{word}'. Do you want to create the file?" + ) + + if create_file: + with open(os.path.join(self.coder.root, word), "w"): + pass + matched_files = [word] + if self.coder.repo is not None: + self.coder.repo.git.add(os.path.join(self.coder.root, word)) + commit_message = f"aider: Created and added {word} to git." + self.coder.repo.git.commit("-m", commit_message, "--no-verify") + else: + self.io.tool_error(f"No files matched '{word}'") for matched_file in matched_files: abs_file_path = os.path.abspath(os.path.join(self.coder.root, matched_file))