diff options
author | Sunny <me@darkowlzz.space> | 2017-12-12 13:56:08 +0530 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-12-12 09:26:08 +0100 |
commit | 757a26038e5404f94523ba07d017d1b38bcbf6dd (patch) | |
tree | 7dae086e094b3af83e608155b9304c85941af740 /options.go | |
parent | afdd28d3cfe11c280723c4f5f33845fd415350d6 (diff) | |
download | go-git-757a26038e5404f94523ba07d017d1b38bcbf6dd.tar.gz |
git: worktree, add Grep() method for git grep (#686)
This change implemented grep on worktree with options to invert match and specify pathspec. Also, a commit hash or reference can be used to specify the worktree to search.
Diffstat (limited to 'options.go')
-rw-r--r-- | options.go | 38 |
1 files changed, 38 insertions, 0 deletions
@@ -2,6 +2,7 @@ package git import ( "errors" + "regexp" "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/plumbing" @@ -365,3 +366,40 @@ type ListOptions struct { type CleanOptions struct { Dir bool } + +// GrepOptions describes how a grep should be performed. +type GrepOptions struct { + // Pattern is a compiled Regexp object to be matched. + Pattern *regexp.Regexp + // InvertMatch selects non-matching lines. + InvertMatch bool + // CommitHash is the hash of the commit from which worktree should be derived. + CommitHash plumbing.Hash + // ReferenceName is the branch or tag name from which worktree should be derived. + ReferenceName plumbing.ReferenceName + // PathSpec is a compiled Regexp object of pathspec to use in the matching. + PathSpec *regexp.Regexp +} + +var ( + ErrHashOrReference = errors.New("ambiguous options, only one of CommitHash or ReferenceName can be passed") +) + +// Validate validates the fields and sets the default values. +func (o *GrepOptions) Validate(w *Worktree) error { + if !o.CommitHash.IsZero() && o.ReferenceName != "" { + return ErrHashOrReference + } + + // If none of CommitHash and ReferenceName are provided, set commit hash of + // the repository's head. + if o.CommitHash.IsZero() && o.ReferenceName == "" { + ref, err := w.r.Head() + if err != nil { + return err + } + o.CommitHash = ref.Hash() + } + + return nil +} |