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.
This commit is contained in:
Paul Gauthier 2023-07-05 21:37:22 -07:00
parent 2b07e2de30
commit 5cd7d9d743

View file

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