用途
本技能介绍如何配置 GrepAI 的忽略模式,把不需要的文件和目录排除在索引之外。适用于从搜索结果中排除测试文件、忽略生成代码或第三方(vendored)代码、通过排除无用文件缩减索引体积,以及自定义 GrepAI 实际索引的文件范围。
忽略模式的来源
GrepAI 使用两个忽略来源:
.grepai/config.yaml:用户自定义的模式.gitignore:自动被遵循
配置位置示例:
# .grepai/config.yaml
ignore:
- pattern1
- pattern2
模式语法
目录模式:
ignore:
# 精确目录名(匹配任意位置)
- node_modules
- vendor
- __pycache__
# 带末尾斜杠(明确表示目录)
- dist/
- build/
- coverage/
文件模式:
ignore:
# 精确文件名
- package-lock.json
- yarn.lock
# 通配符模式
- "*.min.js"
- "*.min.css"
- "*.map"
- "*.lock"
路径模式:
ignore:
# 包含子串的路径
- /tests/
- /spec/
- /__tests__/
# 具体路径
- src/generated/
- api/swagger/
Glob 模式:
ignore:
# 双星号(递归)
- "**/test/**"
- "**/mock/**"
# 单星号(单层)
- "*.test.js"
- "*.spec.ts"
- "*_test.go"
默认忽略模式
GrepAI 默认配置包含:
ignore:
# 版本控制
- .git
- .svn
- .hg
# GrepAI 自身
- .grepai
# 包管理器
- node_modules
- vendor
- .npm
- .yarn
# 构建产物
- target
- dist
- build
- out
# 缓存目录
- __pycache__
- .pytest_cache
- .mypy_cache
- .cache
# 框架产物
- .next
- .nuxt
- .output
常用忽略配置
JavaScript/TypeScript 项目:
ignore:
- node_modules
- dist
- build
- coverage
- .nyc_output
- "*.min.js"
- "*.bundle.js"
- "*.map"
- package-lock.json
- yarn.lock
- pnpm-lock.yaml
Go 项目:
ignore:
- vendor
- bin
- "*.pb.go" # Protobuf 生成
- "*_mock.go" # Mock
- "mocks/"
- go.sum
Python 项目:
ignore:
- __pycache__
- .pytest_cache
- .mypy_cache
- .venv
- venv
- env
- "*.pyc"
- "*.pyo"
- .eggs
- "*.egg-info"
- dist
- build
Rust 项目:
ignore:
- target
- Cargo.lock
- "*.rlib"
Java/Kotlin 项目:
ignore:
- target
- build
- .gradle
- "*.class"
- "*.jar"
- "*.war"
Monorepo:
ignore:
# 通用
- node_modules
- dist
- build
- coverage
# 需要排除的特定包
- packages/legacy/
- packages/deprecated/
# 生成内容
- "**/generated/**"
- "**/__generated__/**"
排除测试文件
若要把搜索聚焦在生产代码上:
ignore:
# 测试目录
- tests/
- test/
- __tests__/
- spec/
# 按模式匹配测试文件
- "*.test.js"
- "*.test.ts"
- "*.spec.js"
- "*.spec.ts"
- "*_test.go"
- "test_*.py"
- "*_test.py"
替代做法:使用搜索加权来对测试降权(而非排除):
search:
boost:
penalties:
- pattern: /tests/
factor: 0.5
- pattern: _test.
factor: 0.5
排除生成代码
ignore:
# 生成标记
- "**/generated/**"
- "*.generated.*"
- "*.gen.*"
# 具体生成器
- "*.pb.go" # Protobuf
- "*.graphql.ts" # GraphQL codegen
- "*.d.ts" # TypeScript 声明
- "swagger_*.go" # Swagger
- "openapi_*.ts" # OpenAPI
排除文档
ignore:
- docs/
- documentation/
- "*.md"
- "*.mdx"
- "*.rst"
验证忽略模式
检查当前索引情况:
# 检查索引状态
grepai status
# 输出会显示文件数量
# 若数量过高,可添加更多忽略模式
常见问题
- 问题:索引过大。解决:为依赖和生成文件添加更多忽略模式。
- 问题:搜索返回 vendor/测试代码。解决:要么忽略,要么使用加权惩罚。
- 问题:模式不生效。解决:检查语法,含特殊字符的模式要加引号:
ignore:
- "*.min.js" # 正确
- *.min.js # 可能导致 YAML 解析问题
- 问题:需要包含此前被忽略的文件。解决:从忽略列表移除,并重新运行
grepai watch。
最佳实践
- 从默认值开始:按需添加模式。
- 排除依赖:始终忽略 node_modules、vendor 等。
- 排除构建产物:dist、build、target。
- 排除锁文件:体积大,对搜索无益。
- 考虑加权而非忽略:对测试文件可降权而非排除。
- 特殊字符加引号:用
"*.min.js",不要用*.min.js。
修改后重新索引
修改忽略模式后:
# 停止现有守护进程
grepai watch --stop
# 清空索引并重启
rm .grepai/index.gob
grepai watch
输出格式
配置忽略模式后:
✅ Ignore Patterns Configured
Patterns: 15 configured
Categories:
- Directories: node_modules, vendor, dist, build
- File types: *.min.js, *.map, *.lock
- Paths: /tests/, /docs/
Also respecting: .gitignore
Run 'grepai watch' to re-index with new patterns.
限制
资料未提供 GrepAI 的安装方式、收费与平台信息,也未说明忽略模式的具体实现细节(如优先级、与 .gitignore 冲突时的处理规则),这些内容暂未提供。