Skip to content

Commit 2c8ebdf

Browse files
committed
concurrent SearchCommit
1 parent 53b22aa commit 2c8ebdf

File tree

2 files changed

+42
-56
lines changed

2 files changed

+42
-56
lines changed

cmd/commands/todos.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/augmentable-dev/tickgit/pkg/comments"
88
"github.com/augmentable-dev/tickgit/pkg/todos"
99
"github.com/spf13/cobra"
10+
"gopkg.in/src-d/go-git.v4"
1011
)
1112

1213
func init() {
@@ -28,9 +29,21 @@ var todosCmd = &cobra.Command{
2829
handleError(err)
2930
}
3031

31-
comments, err := comments.SearchDir(dir)
32+
r, err := git.PlainOpen(dir)
3233
handleError(err)
3334

35+
ref, err := r.Head()
36+
handleError(err)
37+
38+
commit, err := r.CommitObject(ref.Hash())
39+
handleError(err)
40+
41+
comments, err := comments.SearchCommit(commit)
42+
handleError(err)
43+
44+
// comments, err := comments.SearchDir(dir)
45+
// handleError(err)
46+
3447
t := todos.NewToDos(comments)
3548
todos.WriteTodos(t, os.Stdout)
3649
},

pkg/comments/comments.go

Lines changed: 28 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@ import (
44
"bytes"
55
"io"
66
"io/ioutil"
7-
"os"
87
"path/filepath"
9-
"strings"
8+
"sync"
109

1110
"github.com/augmentable-dev/lege"
1211
"github.com/src-d/enry/v2"
@@ -95,69 +94,43 @@ func SearchFile(filePath string, reader io.ReadCloser) (Comments, error) {
9594
return comments, nil
9695
}
9796

98-
// SearchDir searches a directory for comments
99-
func SearchDir(dirPath string) (Comments, error) {
100-
found := make(Comments, 0)
101-
// TODO let's see what we can do concurrently here to speed up the processing
102-
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
103-
localPath, err := filepath.Rel(dirPath, path)
104-
if err != nil {
105-
return err
106-
}
107-
pathComponents := strings.Split(localPath, string(os.PathSeparator))
108-
// let's ignore git directories TODO: figure out a more generic way to set ignores
109-
matched, err := filepath.Match(".git", pathComponents[0])
110-
if err != nil {
111-
return err
112-
}
113-
if matched {
114-
return nil
115-
}
116-
if !info.IsDir() {
117-
p, err := filepath.Abs(path)
118-
if err != nil {
119-
return err
120-
}
121-
f, err := os.Open(p)
122-
if err != nil {
123-
return err
124-
}
125-
t, err := SearchFile(p, f)
126-
if err != nil {
127-
return err
128-
}
129-
c := Comments(t)
130-
found = append(found, c...)
131-
}
132-
return nil
133-
})
134-
if err != nil {
135-
return nil, err
136-
}
137-
return found, nil
138-
}
139-
14097
// SearchCommit searches all files in the tree of a given commit
14198
func SearchCommit(commit *object.Commit) (Comments, error) {
142-
c := make(Comments, 0)
99+
found := make(Comments, 0)
143100
t, err := commit.Tree()
144101
if err != nil {
145102
return nil, err
146103
}
104+
105+
var wg sync.WaitGroup
106+
errs := make(chan error)
107+
147108
fileIter := t.Files()
148109
fileIter.ForEach(func(file *object.File) error {
149110
if file.Mode.IsFile() {
150-
r, err := file.Reader()
151-
if err != nil {
152-
return err
153-
}
154-
found, err := SearchFile(file.Name, r)
155-
if err != nil {
156-
return err
157-
}
158-
c = append(c, found...)
111+
wg.Add(1)
112+
go func() {
113+
defer wg.Done()
114+
115+
r, err := file.Reader()
116+
if err != nil {
117+
errs <- err
118+
return
119+
}
120+
c, err := SearchFile(file.Name, r)
121+
if err != nil {
122+
errs <- err
123+
return
124+
}
125+
126+
for _, comment := range c {
127+
found = append(found, comment)
128+
}
129+
}()
159130
}
160131
return nil
161132
})
162-
return c, nil
133+
134+
wg.Wait()
135+
return found, nil
163136
}

0 commit comments

Comments
 (0)