findMatchInFiles takes a FileIter, worktree name and GrepOptions, and returns a slice of GrepResult containing the result of regex pattern matching in content of all the files.
(fileiter *object.FileIter, treeName string, opts *GrepOptions)
| 966 | // returns a slice of GrepResult containing the result of regex pattern matching |
| 967 | // in content of all the files. |
| 968 | func findMatchInFiles(fileiter *object.FileIter, treeName string, opts *GrepOptions) ([]GrepResult, error) { |
| 969 | var results []GrepResult |
| 970 | |
| 971 | err := fileiter.ForEach(func(file *object.File) error { |
| 972 | var fileInPathSpec bool |
| 973 | |
| 974 | // When no pathspecs are provided, search all the files. |
| 975 | if len(opts.PathSpecs) == 0 { |
| 976 | fileInPathSpec = true |
| 977 | } |
| 978 | |
| 979 | // Check if the file name matches with the pathspec. Break out of the |
| 980 | // loop once a match is found. |
| 981 | for _, pathSpec := range opts.PathSpecs { |
| 982 | if pathSpec != nil && pathSpec.MatchString(file.Name) { |
| 983 | fileInPathSpec = true |
| 984 | break |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | // If the file does not match with any of the pathspec, skip it. |
| 989 | if !fileInPathSpec { |
| 990 | return nil |
| 991 | } |
| 992 | |
| 993 | grepResults, err := findMatchInFile(file, treeName, opts) |
| 994 | if err != nil { |
| 995 | return err |
| 996 | } |
| 997 | results = append(results, grepResults...) |
| 998 | |
| 999 | return nil |
| 1000 | }) |
| 1001 | |
| 1002 | return results, err |
| 1003 | } |
| 1004 | |
| 1005 | // findMatchInFile takes a single File, worktree name and GrepOptions, |
| 1006 | // and returns a slice of GrepResult containing the result of regex pattern |
no test coverage detected