aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--options.go8
-rw-r--r--worktree.go21
-rw-r--r--worktree_test.go81
3 files changed, 101 insertions, 9 deletions
diff --git a/options.go b/options.go
index 6a38ad9..e79cf9d 100644
--- a/options.go
+++ b/options.go
@@ -650,7 +650,13 @@ var (
)
// Validate validates the fields and sets the default values.
+//
+// TODO: deprecate in favor of Validate(r *Repository) in v6.
func (o *GrepOptions) Validate(w *Worktree) error {
+ return o.validate(w.r)
+}
+
+func (o *GrepOptions) validate(r *Repository) error {
if !o.CommitHash.IsZero() && o.ReferenceName != "" {
return ErrHashOrReference
}
@@ -658,7 +664,7 @@ func (o *GrepOptions) Validate(w *Worktree) error {
// 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()
+ ref, err := r.Head()
if err != nil {
return err
}
diff --git a/worktree.go b/worktree.go
index d28ba32..7d2f3b4 100644
--- a/worktree.go
+++ b/worktree.go
@@ -290,7 +290,7 @@ func (w *Worktree) ResetSparsely(opts *ResetOptions, dirs []string) error {
return nil
}
- t, err := w.getTreeFromCommitHash(opts.Commit)
+ t, err := w.r.getTreeFromCommitHash(opts.Commit)
if err != nil {
return err
}
@@ -633,8 +633,8 @@ func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *indexBuil
return nil
}
-func (w *Worktree) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
- c, err := w.r.CommitObject(commit)
+func (r *Repository) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
+ c, err := r.CommitObject(commit)
if err != nil {
return nil, err
}
@@ -802,9 +802,9 @@ func (gr GrepResult) String() string {
return fmt.Sprintf("%s:%s:%d:%s", gr.TreeName, gr.FileName, gr.LineNumber, gr.Content)
}
-// Grep performs grep on a worktree.
-func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
- if err := opts.Validate(w); err != nil {
+// Grep performs grep on a repository.
+func (r *Repository) Grep(opts *GrepOptions) ([]GrepResult, error) {
+ if err := opts.validate(r); err != nil {
return nil, err
}
@@ -814,7 +814,7 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
var treeName string
if opts.ReferenceName != "" {
- ref, err := w.r.Reference(opts.ReferenceName, true)
+ ref, err := r.Reference(opts.ReferenceName, true)
if err != nil {
return nil, err
}
@@ -827,7 +827,7 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
// Obtain a tree from the commit hash and get a tracked files iterator from
// the tree.
- tree, err := w.getTreeFromCommitHash(commitHash)
+ tree, err := r.getTreeFromCommitHash(commitHash)
if err != nil {
return nil, err
}
@@ -836,6 +836,11 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
return findMatchInFiles(fileiter, treeName, opts)
}
+// Grep performs grep on a worktree.
+func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
+ return w.r.Grep(opts)
+}
+
// 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.
diff --git a/worktree_test.go b/worktree_test.go
index ac56a46..1d6a05f 100644
--- a/worktree_test.go
+++ b/worktree_test.go
@@ -2211,6 +2211,87 @@ func (s *WorktreeSuite) TestGrep(c *C) {
}
}
+func (s *WorktreeSuite) TestGrepBare(c *C) {
+ cases := []struct {
+ name string
+ options GrepOptions
+ wantResult []GrepResult
+ dontWantResult []GrepResult
+ wantError error
+ }{
+ {
+ name: "basic word match",
+ options: GrepOptions{
+ Patterns: []*regexp.Regexp{regexp.MustCompile("import")},
+ CommitHash: plumbing.ZeroHash,
+ },
+ wantResult: []GrepResult{
+ {
+ FileName: "go/example.go",
+ LineNumber: 3,
+ Content: "import (",
+ TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
+ },
+ {
+ FileName: "vendor/foo.go",
+ LineNumber: 3,
+ Content: "import \"fmt\"",
+ TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
+ },
+ },
+ },
+ }
+
+ path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
+
+ dir, clean := s.TemporalDir()
+ defer clean()
+
+ r, err := PlainClone(dir, true, &CloneOptions{
+ URL: path,
+ })
+ c.Assert(err, IsNil)
+
+ for _, tc := range cases {
+ gr, err := r.Grep(&tc.options)
+ if tc.wantError != nil {
+ c.Assert(err, Equals, tc.wantError)
+ } else {
+ c.Assert(err, IsNil)
+ }
+
+ // Iterate through the results and check if the wanted result is present
+ // in the got result.
+ for _, wantResult := range tc.wantResult {
+ found := false
+ for _, gotResult := range gr {
+ if wantResult == gotResult {
+ found = true
+ break
+ }
+ }
+ if !found {
+ c.Errorf("unexpected grep results for %q, expected result to contain: %v", tc.name, wantResult)
+ }
+ }
+
+ // Iterate through the results and check if the not wanted result is
+ // present in the got result.
+ for _, dontWantResult := range tc.dontWantResult {
+ found := false
+ for _, gotResult := range gr {
+ if dontWantResult == gotResult {
+ found = true
+ break
+ }
+ }
+ if found {
+ c.Errorf("unexpected grep results for %q, expected result to NOT contain: %v", tc.name, dontWantResult)
+ }
+ }
+ }
+}
+
func (s *WorktreeSuite) TestResetLingeringDirectories(c *C) {
dir, clean := s.TemporalDir()
defer clean()