Skip to content

Commit 59ba107

Browse files
committed
read todos from a git tree as well as a local directory
1 parent f8604e0 commit 59ba107

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

cmd/commands/todos.go

Lines changed: 11 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,7 +29,16 @@ var todosCmd = &cobra.Command{
2829
handleError(err)
2930
}
3031

31-
comments, err := comments.SearchDir(dir)
32+
r, err := git.PlainOpen(dir)
33+
handleError(err)
34+
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)
3242
handleError(err)
3343

3444
t := todos.NewToDos(comments)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/spf13/cobra v0.0.5
99
github.com/spf13/pflag v1.0.5 // indirect
1010
github.com/src-d/enry/v2 v2.1.0
11+
github.com/src-d/go-git v4.7.0+incompatible
1112
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
1213
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
1314
gopkg.in/src-d/go-billy.v4 v4.3.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ github.com/src-d/enry/v2 v2.1.0 h1:z1L8t+B8bh3mmjPkJrgOTnVRpFGmTPJsplHX9wAn6BI=
9191
github.com/src-d/enry/v2 v2.1.0/go.mod h1:qQeCMRwzMF3ckeGr+h0tJLdxXnq+NVZsIDMELj0t028=
9292
github.com/src-d/gcfg v1.4.0 h1:xXbNR5AlLSA315x2UO+fTSSAXCDf+Ar38/6oyGbDKQ4=
9393
github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jWoI=
94+
github.com/src-d/go-git v4.7.0+incompatible h1:IYSSnbAHeKmsfbQFi9ozbid+KNh0bKjlorMfQehQbcE=
95+
github.com/src-d/go-git v4.7.0+incompatible/go.mod h1:1bQciz+hn0jzPQNsYj0hDFZHLJBdV7gXE2mWhC7EkFk=
9496
github.com/src-d/go-oniguruma v1.1.0 h1:EG+Nm5n2JqWUaCjtM0NtutPxU7ZN5Tp50GWrrV8bTww=
9597
github.com/src-d/go-oniguruma v1.1.0/go.mod h1:chVbff8kcVtmrhxtZ3yBVLLquXbzCS6DrxQaAK/CeqM=
9698
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

pkg/comments/comments.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package comments
22

33
import (
44
"bytes"
5+
"io"
56
"io/ioutil"
67
"os"
78
"path/filepath"
89
"strings"
910

1011
"github.com/augmentable-dev/lege"
1112
"github.com/src-d/enry/v2"
13+
"gopkg.in/src-d/go-git.v4/plumbing/object"
1214
)
1315

1416
// CStyleCommentOptions ...
@@ -61,8 +63,8 @@ type Comment struct {
6163
}
6264

6365
// SearchFile searches a file for comments. It infers the language
64-
func SearchFile(filePath string) (Comments, error) {
65-
src, err := ioutil.ReadFile(filePath)
66+
func SearchFile(filePath string, reader io.ReadCloser) (Comments, error) {
67+
src, err := ioutil.ReadAll(reader)
6668
if err != nil {
6769
return nil, err
6870
}
@@ -71,7 +73,7 @@ func SearchFile(filePath string) (Comments, error) {
7173
return nil, nil
7274
}
7375
options, ok := LanguageParseOptions[lang]
74-
if !ok {
76+
if !ok { // TODO provide a default parse option?
7577
return nil, nil
7678
}
7779
commentParser, err := lege.NewParser(options)
@@ -116,7 +118,11 @@ func SearchDir(dirPath string) (Comments, error) {
116118
if err != nil {
117119
return err
118120
}
119-
t, err := SearchFile(p)
121+
f, err := os.Open(p)
122+
if err != nil {
123+
return err
124+
}
125+
t, err := SearchFile(p, f)
120126
if err != nil {
121127
return err
122128
}
@@ -130,3 +136,28 @@ func SearchDir(dirPath string) (Comments, error) {
130136
}
131137
return found, nil
132138
}
139+
140+
// SearchCommit searches all files in the tree of a given commit
141+
func SearchCommit(commit *object.Commit) (Comments, error) {
142+
c := make(Comments, 0)
143+
t, err := commit.Tree()
144+
if err != nil {
145+
return nil, err
146+
}
147+
fileIter := t.Files()
148+
fileIter.ForEach(func(file *object.File) error {
149+
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...)
159+
}
160+
return nil
161+
})
162+
return c, nil
163+
}

0 commit comments

Comments
 (0)