@@ -2,13 +2,15 @@ package comments
22
33import (
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