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)
| 1006 | // and returns a slice of GrepResult containing the result of regex pattern |
| 1007 | // matching in the given file. |
| 1008 | func 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 |
no test coverage detected