MCPcopy Create free account
hub / github.com/go-git/go-git / Grep

Method Grep

worktree.go:927–958  ·  view source on GitHub ↗

Grep performs grep on a repository.

(opts *GrepOptions)

Source from the content-addressed store, hash-verified

925
926// Grep performs grep on a repository.
927func (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.
961func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {

Callers

nothing calls this directly

Calls 8

ReferenceMethod · 0.95
getTreeFromCommitHashMethod · 0.95
findMatchInFilesFunction · 0.85
HashMethod · 0.65
StringMethod · 0.65
IsZeroMethod · 0.65
FilesMethod · 0.65
validateMethod · 0.45

Tested by

no test coverage detected