This commit is contained in:
Paul Gauthier 2023-07-10 15:10:23 -07:00
parent 79030d968f
commit a88b2a70e8
2 changed files with 23 additions and 24 deletions

View file

@ -238,6 +238,7 @@ class Commands:
git_added = [] git_added = []
git_files = self.coder.get_tracked_files() git_files = self.coder.get_tracked_files()
expanded_files = set()
for word in args.split(): for word in args.split():
matched_files = self.glob_filtered_to_repo(word) 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}") self.io.tool_error(f"No files to add matching pattern: {word}")
else: else:
if Path(word).exists(): if Path(word).exists():
if Path(word).is_file(): matched_files = [word]
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}")
elif self.io.confirm_ask( elif self.io.confirm_ask(
f"No files matched '{word}'. Do you want to create the file?" f"No files matched '{word}'. Do you want to create the file?"
): ):
(Path(self.coder.root) / word).touch() (Path(self.coder.root) / word).touch()
matched_files = [word] matched_files = [word]
expanded_files = []
for matched_file in matched_files: for matched_file in matched_files:
if Path(matched_file).is_dir(): if Path(matched_file).is_dir():
expanded_files += expand_subdir(matched_file) expanded_files.update(expand_subdir(matched_file))
else: else:
expanded_files.append(matched_file) expanded_files.add(matched_file)
for matched_file in expanded_files: for matched_file in expanded_files:
abs_file_path = self.coder.abs_root_path(matched_file) abs_file_path = self.coder.abs_root_path(matched_file)
if self.coder.repo and matched_file not in git_files: if self.coder.repo and matched_file not in git_files:
self.coder.repo.git.add(abs_file_path) self.coder.repo.git.add(abs_file_path)
git_added.append(matched_file) git_added.append(matched_file)
if abs_file_path not in self.coder.abs_fnames: if abs_file_path not in self.coder.abs_fnames:
content = self.io.read_text(abs_file_path) content = self.io.read_text(abs_file_path)
if content is not None: if content is not None:
self.coder.abs_fnames.add(abs_file_path) self.coder.abs_fnames.add(abs_file_path)
self.io.tool_output(f"Added {matched_file} to the chat") self.io.tool_output(f"Added {matched_file} to the chat")
added_fnames.append(matched_file) added_fnames.append(matched_file)
else:
self.io.tool_error(f"Unable to read {matched_file}")
else: 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: if self.coder.repo and git_added:
git_added = " ".join(git_added) git_added = " ".join(git_added)

View file

@ -89,17 +89,21 @@ class TestCommands(TestCase):
# Create a directory and add files to it # Create a directory and add files to it
os.mkdir("test_dir") os.mkdir("test_dir")
os.mkdir("test_dir/another_dir")
with open("test_dir/test_file1.txt", "w") as f: with open("test_dir/test_file1.txt", "w") as f:
f.write("Test file 1") f.write("Test file 1")
with open("test_dir/test_file2.txt", "w") as f: with open("test_dir/test_file2.txt", "w") as f:
f.write("Test file 2") 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 # 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 # 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_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/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): def test_cmd_drop_with_glob_patterns(self):
# Initialize the Commands and InputOutput objects # Initialize the Commands and InputOutput objects