aboutsummaryrefslogtreecommitdiffstats
path: root/worktree.go
diff options
context:
space:
mode:
authorAyman Bagabas <ayman.bagabas@gmail.com>2023-03-29 23:26:53 -0400
committerAyman Bagabas <ayman.bagabas@gmail.com>2023-04-26 09:07:37 -0400
commit3aa7575a4d660a250edd06864d6401a302951fab (patch)
tree5f4767d4b986989ed208314113f4d36bb1de105c /worktree.go
parentb154dcce7059e4e02f8798db158b6a76ffc4a63e (diff)
downloadgo-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.go')
-rw-r--r--worktree.go21
1 files changed, 13 insertions, 8 deletions
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.