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)
| 979 | // and returns a slice of GrepResult containing the result of regex pattern |
| 980 | // matching in the given file. |
| 981 | func 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 |
no test coverage detected
searching dependent graphs…