feat: fs_grep now works with both files and directories
This commit is contained in:
@@ -3,10 +3,11 @@ set -e
|
|||||||
|
|
||||||
# @describe Search file contents using regular expressions. Returns matching file paths and lines.
|
# @describe Search file contents using regular expressions. Returns matching file paths and lines.
|
||||||
# Use this to find relevant code before reading files. Much faster than reading files to search.
|
# Use this to find relevant code before reading files. Much faster than reading files to search.
|
||||||
|
# --path accepts either a directory (recursive search with exclude rules applied) or a single file.
|
||||||
|
|
||||||
# @option --pattern! The regex pattern to search for in file contents
|
# @option --pattern! The regex pattern to search for in file contents
|
||||||
# @option --path The directory to search in (defaults to current working directory)
|
# @option --path The directory OR file to search in (defaults to current working directory)
|
||||||
# @option --include File pattern to filter by (e.g. "*.rs", "*.{ts,tsx}", "*.py")
|
# @option --include File pattern to filter by (e.g. "*.rs", "*.{ts,tsx}", "*.py"). Ignored when --path is a single file.
|
||||||
|
|
||||||
# @env LLM_OUTPUT=/dev/stdout The output path
|
# @env LLM_OUTPUT=/dev/stdout The output path
|
||||||
|
|
||||||
@@ -19,33 +20,37 @@ main() {
|
|||||||
local search_path="${argc_path:-.}"
|
local search_path="${argc_path:-.}"
|
||||||
local include_filter="${argc_include:-}"
|
local include_filter="${argc_include:-}"
|
||||||
|
|
||||||
if [[ ! -d "$search_path" ]]; then
|
if [[ ! -e "$search_path" ]]; then
|
||||||
echo "Error: directory not found: $search_path" >> "$LLM_OUTPUT"
|
echo "Error: path not found: $search_path" >> "$LLM_OUTPUT"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local grep_args=(-rn --color=never)
|
local grep_args=(-nH --color=never)
|
||||||
|
|
||||||
grep_args+=(
|
if [[ -d "$search_path" ]]; then
|
||||||
--exclude-dir='.git'
|
grep_args+=(-r)
|
||||||
--exclude-dir='node_modules'
|
grep_args+=(
|
||||||
--exclude-dir='target'
|
--exclude-dir='.git'
|
||||||
--exclude-dir='dist'
|
--exclude-dir='node_modules'
|
||||||
--exclude-dir='build'
|
--exclude-dir='target'
|
||||||
--exclude-dir='__pycache__'
|
--exclude-dir='dist'
|
||||||
--exclude-dir='vendor'
|
--exclude-dir='build'
|
||||||
--exclude-dir='.build'
|
--exclude-dir='__pycache__'
|
||||||
--exclude-dir='.next'
|
--exclude-dir='vendor'
|
||||||
--exclude='*.min.js'
|
--exclude-dir='.build'
|
||||||
--exclude='*.min.css'
|
--exclude-dir='.next'
|
||||||
--exclude='*.map'
|
--exclude='*.min.js'
|
||||||
--exclude='*.lock'
|
--exclude='*.min.css'
|
||||||
--exclude='package-lock.json'
|
--exclude='*.map'
|
||||||
)
|
--exclude='*.lock'
|
||||||
|
--exclude='package-lock.json'
|
||||||
if [[ -n "$include_filter" ]]; then
|
)
|
||||||
grep_args+=("--include=$include_filter")
|
if [[ -n "$include_filter" ]]; then
|
||||||
|
grep_args+=("--include=$include_filter")
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
# If --path is a single file, --include and the exclude rules are ignored
|
||||||
|
# (they only matter when recursing into a directory tree).
|
||||||
|
|
||||||
local results
|
local results
|
||||||
results=$(grep "${grep_args[@]}" -E "$search_pattern" "$search_path" 2>/dev/null | head -n "$MAX_RESULTS") || true
|
results=$(grep "${grep_args[@]}" -E "$search_pattern" "$search_path" 2>/dev/null | head -n "$MAX_RESULTS") || true
|
||||||
|
|||||||
Reference in New Issue
Block a user