Skip to content

Commit 0cdf52a

Browse files
Merge pull request #23 from augmentable-dev/refactoring
Some general refactoring
2 parents bd0139d + e91861d commit 0cdf52a

29 files changed

+1407
-44
lines changed

cmd/commands/todos.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,9 @@ var todosCmd = &cobra.Command{
5151
commit, err := r.CommitObject(ref.Hash())
5252
handleError(err)
5353

54-
comments, err := comments.SearchCommit(commit)
54+
comments, err := comments.SearchDir(dir)
5555
handleError(err)
5656

57-
// comments, err := comments.SearchDir(dir)
58-
// handleError(err)
59-
6057
t := todos.NewToDos(comments)
6158

6259
ctx := context.Background()
@@ -67,7 +64,6 @@ var todosCmd = &cobra.Command{
6764
total := len(t)
6865
s.Suffix = fmt.Sprintf(" (%d/%d) %s: %s", total-remaining, total, commit.Hash, commit.Author.When)
6966
})
70-
7167
sort.Sort(&t)
7268

7369
handleError(err)

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ require (
99
github.com/dustin/go-humanize v1.0.0
1010
github.com/google/go-cmp v0.3.1 // indirect
1111
github.com/hashicorp/hcl/v2 v2.0.0
12+
github.com/karrick/godirwalk v1.13.0
1213
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
1314
github.com/spf13/cobra v0.0.5
1415
github.com/spf13/pflag v1.0.5 // indirect
1516
github.com/src-d/enry/v2 v2.1.0
17+
github.com/stretchr/testify v1.4.0 // indirect
1618
golang.org/x/crypto v0.0.0-20191029031824-8986dd9e96cf // indirect
1719
golang.org/x/net v0.0.0-20191028085509-fe3aa8a45271 // indirect
1820
golang.org/x/sys v0.0.0-20191029155521-f43be2a4598c // indirect
1921
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
2022
gopkg.in/src-d/go-billy.v4 v4.3.2
2123
gopkg.in/src-d/go-git.v4 v4.13.1
24+
gopkg.in/yaml.v2 v2.2.4 // indirect
2225
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt
5454
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
5555
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
5656
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
57+
github.com/karrick/godirwalk v1.13.0 h1:GJq8GHQEAPsjwqfGhLNXBO5P0dS2HYdDRVWe+P4E/EQ=
58+
github.com/karrick/godirwalk v1.13.0/go.mod h1:H5KPZjojv4lE+QYImBI8xVtrBRgYrIVsaRPx4tDPEn4=
5759
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
5860
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
5961
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -114,6 +116,8 @@ github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoH
114116
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
115117
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
116118
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
119+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
120+
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
117121
github.com/toqueteos/trie v1.0.0 h1:8i6pXxNUXNRAqP246iibb7w/pSFquNTQ+uNfriG7vlk=
118122
github.com/toqueteos/trie v1.0.0/go.mod h1:Ywk48QhEqhU1+DwhMkJ2x7eeGxDHiGkAdc9+0DYcbsM=
119123
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
@@ -173,3 +177,5 @@ gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
173177
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
174178
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
175179
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
180+
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
181+
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

pkg/comments/comments.go

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111

1212
"github.com/augmentable-dev/lege"
13+
"github.com/karrick/godirwalk"
1314
"github.com/src-d/enry/v2"
1415
"gopkg.in/src-d/go-git.v4/plumbing/object"
1516
)
@@ -70,7 +71,7 @@ type Comment struct {
7071
}
7172

7273
// SearchFile searches a file for comments. It infers the language
73-
func SearchFile(filePath string, reader io.ReadCloser) (Comments, error) {
74+
func SearchFile(filePath string, reader io.Reader) (Comments, error) {
7475
src, err := ioutil.ReadAll(reader)
7576
if err != nil {
7677
return nil, err
@@ -105,38 +106,41 @@ func SearchFile(filePath string, reader io.ReadCloser) (Comments, error) {
105106
// SearchDir searches a directory for comments
106107
func SearchDir(dirPath string) (Comments, error) {
107108
found := make(Comments, 0)
108-
// TODO let's see what we can do concurrently here to speed up the processing
109-
err := filepath.Walk(dirPath, func(path string, info os.FileInfo, err error) error {
110-
localPath, err := filepath.Rel(dirPath, path)
111-
if err != nil {
112-
return err
113-
}
114-
pathComponents := strings.Split(localPath, string(os.PathSeparator))
115-
// let's ignore git directories TODO: figure out a more generic way to set ignores
116-
matched, err := filepath.Match(".git", pathComponents[0])
117-
if err != nil {
118-
return err
119-
}
120-
if matched {
121-
return nil
122-
}
123-
if !info.IsDir() {
124-
p, err := filepath.Abs(path)
109+
err := godirwalk.Walk(dirPath, &godirwalk.Options{
110+
Callback: func(path string, de *godirwalk.Dirent) error {
111+
localPath, err := filepath.Rel(dirPath, path)
125112
if err != nil {
126113
return err
127114
}
128-
f, err := os.Open(p)
115+
pathComponents := strings.Split(localPath, string(os.PathSeparator))
116+
// let's ignore git directories TODO: figure out a more generic way to set ignores
117+
matched, err := filepath.Match(".git", pathComponents[0])
129118
if err != nil {
130119
return err
131120
}
132-
t, err := SearchFile(p, f)
133-
if err != nil {
134-
return err
121+
if matched {
122+
return nil
135123
}
136-
c := Comments(t)
137-
found = append(found, c...)
138-
}
139-
return nil
124+
if de.IsRegular() {
125+
p, err := filepath.Abs(path)
126+
if err != nil {
127+
return err
128+
}
129+
f, err := os.Open(p)
130+
if err != nil {
131+
return err
132+
}
133+
defer f.Close()
134+
t, err := SearchFile(p, f)
135+
if err != nil {
136+
return err
137+
}
138+
c := Comments(t)
139+
found = append(found, c...)
140+
}
141+
return nil
142+
},
143+
Unsorted: true,
140144
})
141145
if err != nil {
142146
return nil, err

pkg/todos/todos.go

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package todos
22

33
import (
44
"context"
5+
"fmt"
56
"regexp"
67
"strings"
78
"sync"
9+
"time"
810

911
"github.com/augmentable-dev/tickgit/pkg/comments"
1012
"github.com/dustin/go-humanize"
@@ -17,7 +19,24 @@ import (
1719
type ToDo struct {
1820
comments.Comment
1921
String string
20-
Commit *object.Commit
22+
Commit *Commit
23+
}
24+
25+
// Commit represents the commit a todo originated in
26+
type Commit struct {
27+
Hash string
28+
Author
29+
}
30+
31+
// Author represents the authoring of the commit a todo originated in
32+
type Author struct {
33+
Name string
34+
Email string
35+
When time.Time
36+
}
37+
38+
func (a *Author) String() string {
39+
return fmt.Sprintf("%s <%s>", a.Name, a.Email)
2140
}
2241

2342
// ToDos represents a list of ToDo items
@@ -58,30 +77,30 @@ func NewToDos(comments comments.Comments) ToDos {
5877
}
5978

6079
// Len returns the number of todos
61-
func (t *ToDos) Len() int {
62-
return len(*t)
80+
func (t ToDos) Len() int {
81+
return len(t)
6382
}
6483

6584
// Less compares two todos by their creation time
66-
func (t *ToDos) Less(i, j int) bool {
67-
first := (*t)[i]
68-
second := (*t)[j]
85+
func (t ToDos) Less(i, j int) bool {
86+
first := t[i]
87+
second := t[j]
6988
if first.Commit == nil || second.Commit == nil {
7089
return false
7190
}
7291
return first.Commit.Author.When.Before(second.Commit.Author.When)
7392
}
7493

7594
// Swap swaps two todoss
76-
func (t *ToDos) Swap(i, j int) {
77-
temp := (*t)[i]
78-
(*t)[i] = (*t)[j]
79-
(*t)[j] = temp
95+
func (t ToDos) Swap(i, j int) {
96+
temp := t[i]
97+
t[i] = t[j]
98+
t[j] = temp
8099
}
81100

82101
// CountWithCommits returns the number of todos with an associated commit (in which that todo was added)
83-
func (t *ToDos) CountWithCommits() (count int) {
84-
for _, todo := range *t {
102+
func (t ToDos) CountWithCommits() (count int) {
103+
for _, todo := range t {
85104
if todo.Commit != nil {
86105
count++
87106
}
@@ -143,7 +162,14 @@ func (t *ToDos) FindBlame(ctx context.Context, repo *git.Repository, from *objec
143162
}
144163
mux.Unlock()
145164
if !exists { // if the todo doesn't exist in this commit, it was added in the previous commit (previous wrt the iterator, more recent in time)
146-
todo.Commit = prevCommit
165+
todo.Commit = &Commit{
166+
Hash: prevCommit.Hash.String(),
167+
Author: Author{
168+
Name: prevCommit.Author.Name,
169+
Email: prevCommit.Author.Email,
170+
When: prevCommit.Author.When,
171+
},
172+
}
147173
} else { // if the todo does exist in this commit, add it to the new list of remaining todos
148174
newRemainingTodos = append(newRemainingTodos, todo)
149175
}

vendor/github.com/karrick/godirwalk/.gitignore

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/karrick/godirwalk/LICENSE

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)