MCPcopy Create free account
hub / github.com/go-git/go-git / findMatchInFile

Function findMatchInFile

worktree.go:1008–1049  ·  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

1006// and returns a slice of GrepResult containing the result of regex pattern
1007// matching in the given file.
1008func findMatchInFile(file *object.File, treeName string, opts *GrepOptions) ([]GrepResult, error) {
1009 var grepResults []GrepResult
1010
1011 content, err := file.Contents()
1012 if err != nil {
1013 return grepResults, err
1014 }
1015
1016 // Split the file content and parse line-by-line.
1017 contentByLine := strings.Split(content, "\n")
1018 for lineNum, cnt := range contentByLine {
1019 addToResult := false
1020
1021 // Match the patterns and content. Break out of the loop once a
1022 // match is found.
1023 for _, pattern := range opts.Patterns {
1024 if pattern != nil && pattern.MatchString(cnt) {
1025 // Add to result only if invert match is not enabled.
1026 if !opts.InvertMatch {
1027 addToResult = true
1028 break
1029 }
1030 } else if opts.InvertMatch {
1031 // If matching fails, and invert match is enabled, add to
1032 // results.
1033 addToResult = true
1034 break
1035 }
1036 }
1037
1038 if addToResult {
1039 grepResults = append(grepResults, GrepResult{
1040 FileName: file.Name,
1041 LineNumber: lineNum + 1,
1042 Content: cnt,
1043 TreeName: treeName,
1044 })
1045 }
1046 }
1047
1048 return grepResults, nil
1049}
1050
1051// will walk up the directory tree removing all encountered empty
1052// directories, not just the one containing this file

Callers 1

findMatchInFilesFunction · 0.85

Calls 1

ContentsMethod · 0.80

Tested by

no test coverage detected