MCPcopy Create free account
hub / github.com/astercloud/aster / GrepRaw

Method GrepRaw

pkg/backends/store_backend.go:215–271  ·  view source on GitHub ↗

GrepRaw 实现 BackendProtocol.GrepRaw

(ctx context.Context, pattern, path, glob string)

Source from the content-addressed store, hash-verified

213
214// GrepRaw 实现 BackendProtocol.GrepRaw
215func (b *StoreBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error) {
216 // 编译正则表达式
217 re, err := regexp.Compile(pattern)
218 if err != nil {
219 return nil, fmt.Errorf("invalid regex pattern: %w", err)
220 }
221
222 // 编译 glob 模式
223 var globRe *regexp.Regexp
224 if glob != "" {
225 globPattern := globToRegex(glob)
226 globRe, err = regexp.Compile(globPattern)
227 if err != nil {
228 return nil, fmt.Errorf("invalid glob pattern: %w", err)
229 }
230 }
231
232 // 加载所有文件
233 allFiles, err := b.loadAllFileMeta(ctx)
234 if err != nil {
235 return nil, err
236 }
237
238 var matches []GrepMatch
239
240 for filePath := range allFiles {
241 // 检查路径是否匹配
242 if path != "" && !strings.HasPrefix(filePath, path) {
243 continue
244 }
245
246 // 检查 glob 过滤
247 if globRe != nil && !globRe.MatchString(filepath.Base(filePath)) {
248 continue
249 }
250
251 // 加载文件内容
252 data, err := b.loadFileData(ctx, filePath)
253 if err != nil {
254 continue
255 }
256
257 // 搜索每一行
258 for lineNum, line := range data.Lines {
259 if re.MatchString(line) {
260 matches = append(matches, GrepMatch{
261 Path: filePath,
262 LineNumber: lineNum + 1, // 1-based
263 Line: line,
264 Match: re.FindString(line),
265 })
266 }
267 }
268 }
269
270 return matches, nil
271}
272

Callers

nothing calls this directly

Calls 3

loadAllFileMetaMethod · 0.95
loadFileDataMethod · 0.95
globToRegexFunction · 0.85

Tested by

no test coverage detected