diff --git a/aider/commands.py b/aider/commands.py index e2070e049..b09fe1a5d 100644 --- a/aider/commands.py +++ b/aider/commands.py @@ -238,6 +238,7 @@ class Commands: git_added = [] git_files = self.coder.get_tracked_files() + expanded_files = set() for word in args.split(): matched_files = self.glob_filtered_to_repo(word) @@ -246,42 +247,36 @@ class Commands: self.io.tool_error(f"No files to add matching pattern: {word}") else: if Path(word).exists(): - if Path(word).is_file(): - matched_files = [word] - elif Path(word).is_dir(): - matched_files = expand_subdir(word) - else: - self.io.tool_error(f"Can not add unknown file type: {word}") + matched_files = [word] elif self.io.confirm_ask( f"No files matched '{word}'. Do you want to create the file?" ): (Path(self.coder.root) / word).touch() matched_files = [word] - expanded_files = [] for matched_file in matched_files: if Path(matched_file).is_dir(): - expanded_files += expand_subdir(matched_file) + expanded_files.update(expand_subdir(matched_file)) else: - expanded_files.append(matched_file) + expanded_files.add(matched_file) - for matched_file in expanded_files: - abs_file_path = self.coder.abs_root_path(matched_file) + for matched_file in expanded_files: + abs_file_path = self.coder.abs_root_path(matched_file) - if self.coder.repo and matched_file not in git_files: - self.coder.repo.git.add(abs_file_path) - git_added.append(matched_file) + if self.coder.repo and matched_file not in git_files: + self.coder.repo.git.add(abs_file_path) + git_added.append(matched_file) - if abs_file_path not in self.coder.abs_fnames: - content = self.io.read_text(abs_file_path) - if content is not None: - self.coder.abs_fnames.add(abs_file_path) - self.io.tool_output(f"Added {matched_file} to the chat") - added_fnames.append(matched_file) - else: - self.io.tool_error(f"Unable to read {matched_file}") + if abs_file_path not in self.coder.abs_fnames: + content = self.io.read_text(abs_file_path) + if content is not None: + self.coder.abs_fnames.add(abs_file_path) + self.io.tool_output(f"Added {matched_file} to the chat") + added_fnames.append(matched_file) else: - self.io.tool_error(f"{matched_file} is already in the chat") + self.io.tool_error(f"Unable to read {matched_file}") + else: + self.io.tool_error(f"{matched_file} is already in the chat") if self.coder.repo and git_added: git_added = " ".join(git_added) diff --git a/tests/test_commands.py b/tests/test_commands.py index 3a2749122..642b4e561 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -89,17 +89,21 @@ class TestCommands(TestCase): # Create a directory and add files to it os.mkdir("test_dir") + os.mkdir("test_dir/another_dir") with open("test_dir/test_file1.txt", "w") as f: f.write("Test file 1") with open("test_dir/test_file2.txt", "w") as f: f.write("Test file 2") + with open("test_dir/another_dir/test_file.txt", "w") as f: + f.write("Test file 3") # Call the cmd_add method with a directory - commands.cmd_add("test_dir") + commands.cmd_add("test_dir test_dir/test_file2.txt") # Check if the files have been added to the chat session self.assertIn(str(Path("test_dir/test_file1.txt").resolve()), coder.abs_fnames) self.assertIn(str(Path("test_dir/test_file2.txt").resolve()), coder.abs_fnames) + self.assertIn(str(Path("test_dir/another_dir/test_file.txt").resolve()), coder.abs_fnames) def test_cmd_drop_with_glob_patterns(self): # Initialize the Commands and InputOutput objects