Skip to content

Commit 0542a30

Browse files
authored
Merge pull request go-git#728 from aymanbagabas/grep-bare
fix: git grep bare repositories
2 parents cd1fd6b + 3aa7575 commit 0542a30

File tree

3 files changed

+101
-9
lines changed

3 files changed

+101
-9
lines changed

options.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,15 +650,21 @@ var (
650650
)
651651

652652
// Validate validates the fields and sets the default values.
653+
//
654+
// TODO: deprecate in favor of Validate(r *Repository) in v6.
653655
func (o *GrepOptions) Validate(w *Worktree) error {
656+
return o.validate(w.r)
657+
}
658+
659+
func (o *GrepOptions) validate(r *Repository) error {
654660
if !o.CommitHash.IsZero() && o.ReferenceName != "" {
655661
return ErrHashOrReference
656662
}
657663

658664
// If none of CommitHash and ReferenceName are provided, set commit hash of
659665
// the repository's head.
660666
if o.CommitHash.IsZero() && o.ReferenceName == "" {
661-
ref, err := w.r.Head()
667+
ref, err := r.Head()
662668
if err != nil {
663669
return err
664670
}

worktree.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ func (w *Worktree) ResetSparsely(opts *ResetOptions, dirs []string) error {
290290
return nil
291291
}
292292

293-
t, err := w.getTreeFromCommitHash(opts.Commit)
293+
t, err := w.r.getTreeFromCommitHash(opts.Commit)
294294
if err != nil {
295295
return err
296296
}
@@ -633,8 +633,8 @@ func (w *Worktree) addIndexFromFile(name string, h plumbing.Hash, idx *indexBuil
633633
return nil
634634
}
635635

636-
func (w *Worktree) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
637-
c, err := w.r.CommitObject(commit)
636+
func (r *Repository) getTreeFromCommitHash(commit plumbing.Hash) (*object.Tree, error) {
637+
c, err := r.CommitObject(commit)
638638
if err != nil {
639639
return nil, err
640640
}
@@ -802,9 +802,9 @@ func (gr GrepResult) String() string {
802802
return fmt.Sprintf("%s:%s:%d:%s", gr.TreeName, gr.FileName, gr.LineNumber, gr.Content)
803803
}
804804

805-
// Grep performs grep on a worktree.
806-
func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
807-
if err := opts.Validate(w); err != nil {
805+
// Grep performs grep on a repository.
806+
func (r *Repository) Grep(opts *GrepOptions) ([]GrepResult, error) {
807+
if err := opts.validate(r); err != nil {
808808
return nil, err
809809
}
810810

@@ -814,7 +814,7 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
814814
var treeName string
815815

816816
if opts.ReferenceName != "" {
817-
ref, err := w.r.Reference(opts.ReferenceName, true)
817+
ref, err := r.Reference(opts.ReferenceName, true)
818818
if err != nil {
819819
return nil, err
820820
}
@@ -827,7 +827,7 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
827827

828828
// Obtain a tree from the commit hash and get a tracked files iterator from
829829
// the tree.
830-
tree, err := w.getTreeFromCommitHash(commitHash)
830+
tree, err := r.getTreeFromCommitHash(commitHash)
831831
if err != nil {
832832
return nil, err
833833
}
@@ -836,6 +836,11 @@ func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
836836
return findMatchInFiles(fileiter, treeName, opts)
837837
}
838838

839+
// Grep performs grep on a worktree.
840+
func (w *Worktree) Grep(opts *GrepOptions) ([]GrepResult, error) {
841+
return w.r.Grep(opts)
842+
}
843+
839844
// findMatchInFiles takes a FileIter, worktree name and GrepOptions, and
840845
// returns a slice of GrepResult containing the result of regex pattern matching
841846
// in content of all the files.

worktree_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,87 @@ func (s *WorktreeSuite) TestGrep(c *C) {
22112211
}
22122212
}
22132213

2214+
func (s *WorktreeSuite) TestGrepBare(c *C) {
2215+
cases := []struct {
2216+
name string
2217+
options GrepOptions
2218+
wantResult []GrepResult
2219+
dontWantResult []GrepResult
2220+
wantError error
2221+
}{
2222+
{
2223+
name: "basic word match",
2224+
options: GrepOptions{
2225+
Patterns: []*regexp.Regexp{regexp.MustCompile("import")},
2226+
CommitHash: plumbing.ZeroHash,
2227+
},
2228+
wantResult: []GrepResult{
2229+
{
2230+
FileName: "go/example.go",
2231+
LineNumber: 3,
2232+
Content: "import (",
2233+
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
2234+
},
2235+
{
2236+
FileName: "vendor/foo.go",
2237+
LineNumber: 3,
2238+
Content: "import \"fmt\"",
2239+
TreeName: "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
2240+
},
2241+
},
2242+
},
2243+
}
2244+
2245+
path := fixtures.Basic().ByTag("worktree").One().Worktree().Root()
2246+
2247+
dir, clean := s.TemporalDir()
2248+
defer clean()
2249+
2250+
r, err := PlainClone(dir, true, &CloneOptions{
2251+
URL: path,
2252+
})
2253+
c.Assert(err, IsNil)
2254+
2255+
for _, tc := range cases {
2256+
gr, err := r.Grep(&tc.options)
2257+
if tc.wantError != nil {
2258+
c.Assert(err, Equals, tc.wantError)
2259+
} else {
2260+
c.Assert(err, IsNil)
2261+
}
2262+
2263+
// Iterate through the results and check if the wanted result is present
2264+
// in the got result.
2265+
for _, wantResult := range tc.wantResult {
2266+
found := false
2267+
for _, gotResult := range gr {
2268+
if wantResult == gotResult {
2269+
found = true
2270+
break
2271+
}
2272+
}
2273+
if !found {
2274+
c.Errorf("unexpected grep results for %q, expected result to contain: %v", tc.name, wantResult)
2275+
}
2276+
}
2277+
2278+
// Iterate through the results and check if the not wanted result is
2279+
// present in the got result.
2280+
for _, dontWantResult := range tc.dontWantResult {
2281+
found := false
2282+
for _, gotResult := range gr {
2283+
if dontWantResult == gotResult {
2284+
found = true
2285+
break
2286+
}
2287+
}
2288+
if found {
2289+
c.Errorf("unexpected grep results for %q, expected result to NOT contain: %v", tc.name, dontWantResult)
2290+
}
2291+
}
2292+
}
2293+
}
2294+
22142295
func (s *WorktreeSuite) TestResetLingeringDirectories(c *C) {
22152296
dir, clean := s.TemporalDir()
22162297
defer clean()

0 commit comments

Comments
 (0)