Grep performs grep on a repository.
(opts *GrepOptions)
| 925 | |
| 926 | // Grep performs grep on a repository. |
| 927 | func (r *Repository) Grep(opts *GrepOptions) ([]GrepResult, error) { |
| 928 | if err := opts.validate(r); err != nil { |
| 929 | return nil, err |
| 930 | } |
| 931 | |
| 932 | // Obtain commit hash from options (CommitHash or ReferenceName). |
| 933 | var commitHash plumbing.Hash |
| 934 | // treeName contains the value of TreeName in GrepResult. |
| 935 | var treeName string |
| 936 | |
| 937 | if opts.ReferenceName != "" { |
| 938 | ref, err := r.Reference(opts.ReferenceName, true) |
| 939 | if err != nil { |
| 940 | return nil, err |
| 941 | } |
| 942 | commitHash = ref.Hash() |
| 943 | treeName = opts.ReferenceName.String() |
| 944 | } else if !opts.CommitHash.IsZero() { |
| 945 | commitHash = opts.CommitHash |
| 946 | treeName = opts.CommitHash.String() |
| 947 | } |
| 948 | |
| 949 | // Obtain a tree from the commit hash and get a tracked files iterator from |
| 950 | // the tree. |
| 951 | tree, err := r.getTreeFromCommitHash(commitHash) |
| 952 | if err != nil { |
| 953 | return nil, err |
| 954 | } |
| 955 | fileiter := tree.Files() |
| 956 | |
| 957 | return findMatchInFiles(fileiter, treeName, opts) |
| 958 | } |
| 959 | |
| 960 | // Grep performs grep on a worktree. |
| 961 | func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) { |
nothing calls this directly
no test coverage detected