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

Method GrepRaw

pkg/backends/state.go:224–271  ·  view source on GitHub ↗

GrepRaw 实现 BackendProtocol.GrepRaw

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

Source from the content-addressed store, hash-verified

222
223// GrepRaw 实现 BackendProtocol.GrepRaw
224func (b *StateBackend) GrepRaw(ctx context.Context, pattern, path, glob string) ([]GrepMatch, error) {
225 b.mu.RLock()
226 defer b.mu.RUnlock()
227
228 // 编译正则表达式
229 re, err := regexp.Compile(pattern)
230 if err != nil {
231 return nil, fmt.Errorf("invalid regex pattern: %w", err)
232 }
233
234 // 编译 glob 模式
235 var globRe *regexp.Regexp
236 if glob != "" {
237 globPattern := globToRegex(glob)
238 globRe, err = regexp.Compile(globPattern)
239 if err != nil {
240 return nil, fmt.Errorf("invalid glob pattern: %w", err)
241 }
242 }
243
244 var matches []GrepMatch
245
246 for filePath, data := range b.files {
247 // 检查路径是否匹配
248 if path != "" && !strings.HasPrefix(filePath, path) {
249 continue
250 }
251
252 // 检查 glob 过滤
253 if globRe != nil && !globRe.MatchString(filepath.Base(filePath)) {
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
273// GlobInfo 实现 BackendProtocol.GlobInfo
274func (b *StateBackend) GlobInfo(ctx context.Context, pattern, path string) ([]FileInfo, error) {

Callers 1

TestStateBackendFunction · 0.95

Calls 1

globToRegexFunction · 0.85

Tested by 1

TestStateBackendFunction · 0.76