feat: use glob for flexible file matching in cmd_read_only

This commit is contained in:
Paul Gauthier (aider) 2024-10-02 09:16:08 -07:00
parent 510fa24ade
commit e5b27355b7

View file

@ -3,6 +3,7 @@ import re
import subprocess import subprocess
import sys import sys
import tempfile import tempfile
import glob
from collections import OrderedDict from collections import OrderedDict
from pathlib import Path from pathlib import Path
@ -1137,21 +1138,20 @@ class Commands:
return return
filenames = parse_quoted_filenames(args) filenames = parse_quoted_filenames(args)
for word in filenames: for pattern in filenames:
# Expand the home directory if the path starts with "~" expanded_paths = glob.glob(pattern, recursive=True)
expanded_path = os.path.expanduser(word) if not expanded_paths:
abs_path = self.coder.abs_root_path(expanded_path) self.io.tool_error(f"No matches found for: {pattern}")
if not os.path.exists(abs_path):
self.io.tool_error(f"Path not found: {abs_path}")
continue continue
if os.path.isfile(abs_path): for path in expanded_paths:
self._add_read_only_file(abs_path, word) abs_path = self.coder.abs_root_path(path)
elif os.path.isdir(abs_path): if os.path.isfile(abs_path):
self._add_read_only_directory(abs_path, word) self._add_read_only_file(abs_path, path)
else: elif os.path.isdir(abs_path):
self.io.tool_error(f"Not a file or directory: {abs_path}") self._add_read_only_directory(abs_path, path)
else:
self.io.tool_error(f"Not a file or directory: {abs_path}")
def _add_read_only_file(self, abs_path, original_name): def _add_read_only_file(self, abs_path, original_name):
if abs_path in self.coder.abs_read_only_fnames: if abs_path in self.coder.abs_read_only_fnames: