diff options
author | Ayman Bagabas <ayman.bagabas@gmail.com> | 2023-03-29 23:26:53 -0400 |
---|---|---|
committer | Ayman Bagabas <ayman.bagabas@gmail.com> | 2023-04-26 09:07:37 -0400 |
commit | 3aa7575a4d660a250edd06864d6401a302951fab (patch) | |
tree | 5f4767d4b986989ed208314113f4d36bb1de105c /worktree_test.go | |
parent | b154dcce7059e4e02f8798db158b6a76ffc4a63e (diff) | |
download | go-git-3aa7575a4d660a250edd06864d6401a302951fab.tar.gz |
fix: git grep bare repositories
Perform grep on `*Repository` instead of `*Worktree`.
Fixes: https://github.com/go-git/go-git/issues/68
Diffstat (limited to 'worktree_test.go')
-rw-r--r-- | worktree_test.go | 81 |
1 files changed, 81 insertions, 0 deletions
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() |