|
4 | 4 | "bytes" |
5 | 5 | "io" |
6 | 6 | "io/ioutil" |
7 | | - "os" |
8 | 7 | "path/filepath" |
9 | | - "strings" |
| 8 | + "sync" |
10 | 9 |
|
11 | 10 | "github.com/augmentable-dev/lege" |
12 | 11 | "github.com/src-d/enry/v2" |
@@ -95,69 +94,43 @@ func SearchFile(filePath string, reader io.ReadCloser) (Comments, error) { |
95 | 94 | return comments, nil |
96 | 95 | } |
97 | 96 |
|
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 | | - |
140 | 97 | // SearchCommit searches all files in the tree of a given commit |
141 | 98 | func SearchCommit(commit *object.Commit) (Comments, error) { |
142 | | - c := make(Comments, 0) |
| 99 | + found := make(Comments, 0) |
143 | 100 | t, err := commit.Tree() |
144 | 101 | if err != nil { |
145 | 102 | return nil, err |
146 | 103 | } |
| 104 | + |
| 105 | + var wg sync.WaitGroup |
| 106 | + errs := make(chan error) |
| 107 | + |
147 | 108 | fileIter := t.Files() |
148 | 109 | fileIter.ForEach(func(file *object.File) error { |
149 | 110 | 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 | + }() |
159 | 130 | } |
160 | 131 | return nil |
161 | 132 | }) |
162 | | - return c, nil |
| 133 | + |
| 134 | + wg.Wait() |
| 135 | + return found, nil |
163 | 136 | } |
0 commit comments