MCPcopy Index your code
hub / github.com/go-git/go-git / findMatchInFile

Function findMatchInFile

worktree.go:981–1022  ·  view source on GitHub ↗

findMatchInFile takes a single File, worktree name and GrepOptions, and returns a slice of GrepResult containing the result of regex pattern matching in the given file.

(file *object.File, treeName string, opts *GrepOptions)

Source from the content-addressed store, hash-verified

979// and returns a slice of GrepResult containing the result of regex pattern
980// matching in the given file.
981func findMatchInFile(file *object.File, treeName string, opts *GrepOptions) ([]GrepResult, error) {
982 var grepResults []GrepResult
983
984 content, err := file.Contents()
985 if err != nil {
986 return grepResults, err
987 }
988
989 // Split the file content and parse line-by-line.
990 contentByLine := strings.Split(content, "\n")
991 for lineNum, cnt := range contentByLine {
992 addToResult := false
993
994 // Match the patterns and content. Break out of the loop once a
995 // match is found.
996 for _, pattern := range opts.Patterns {
997 if pattern != nil && pattern.MatchString(cnt) {
998 // Add to result only if invert match is not enabled.
999 if !opts.InvertMatch {
1000 addToResult = true
1001 break
1002 }
1003 } else if opts.InvertMatch {
1004 // If matching fails, and invert match is enabled, add to
1005 // results.
1006 addToResult = true
1007 break
1008 }
1009 }
1010
1011 if addToResult {
1012 grepResults = append(grepResults, GrepResult{
1013 FileName: file.Name,
1014 LineNumber: lineNum + 1,
1015 Content: cnt,
1016 TreeName: treeName,
1017 })
1018 }
1019 }
1020
1021 return grepResults, nil
1022}
1023
1024// will walk up the directory tree removing all encountered empty
1025// directories, not just the one containing this file

Callers 1

findMatchInFilesFunction · 0.85

Calls 1

ContentsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…