Grep performs grep on a repository.
(opts *GrepOptions)
| 898 | |
| 899 | // Grep performs grep on a repository. |
| 900 | func (r *Repository) Grep(opts *GrepOptions) ([]GrepResult, error) { |
| 901 | if err := opts.validate(r); err != nil { |
| 902 | return nil, err |
| 903 | } |
| 904 | |
| 905 | // Obtain commit hash from options (CommitHash or ReferenceName). |
| 906 | var commitHash plumbing.Hash |
| 907 | // treeName contains the value of TreeName in GrepResult. |
| 908 | var treeName string |
| 909 | |
| 910 | if opts.ReferenceName != "" { |
| 911 | ref, err := r.Reference(opts.ReferenceName, true) |
| 912 | if err != nil { |
| 913 | return nil, err |
| 914 | } |
| 915 | commitHash = ref.Hash() |
| 916 | treeName = opts.ReferenceName.String() |
| 917 | } else if !opts.CommitHash.IsZero() { |
| 918 | commitHash = opts.CommitHash |
| 919 | treeName = opts.CommitHash.String() |
| 920 | } |
| 921 | |
| 922 | // Obtain a tree from the commit hash and get a tracked files iterator from |
| 923 | // the tree. |
| 924 | tree, err := r.getTreeFromCommitHash(commitHash) |
| 925 | if err != nil { |
| 926 | return nil, err |
| 927 | } |
| 928 | fileiter := tree.Files() |
| 929 | |
| 930 | return findMatchInFiles(fileiter, treeName, opts) |
| 931 | } |
| 932 | |
| 933 | // Grep performs grep on a worktree. |
| 934 | func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) { |
nothing calls this directly
no test coverage detected