Skip to content

Commit 6ed2e33

Browse files
committed
home grown blame for todos
1 parent 9092f80 commit 6ed2e33

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

cmd/commands/todos.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ var todosCmd = &cobra.Command{
5656

5757
t := todos.NewToDos(comments)
5858
for i, todo := range t { // TODO: can we do this concurrently
59-
todo.FindBlame(commit)
59+
todo.FindBlame(r, commit)
6060
s.Suffix = fmt.Sprintf(" (%d/%d) %s: %s", i, len(t), filepath.Base(todo.FilePath), todo.String)
6161
// time.Sleep(100 * time.Millisecond)
6262
}

pkg/todos/todos.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/dustin/go-humanize"
1010
"gopkg.in/src-d/go-git.v4"
1111
"gopkg.in/src-d/go-git.v4/plumbing/object"
12+
"gopkg.in/src-d/go-git.v4/plumbing/storer"
1213
)
1314

1415
// ToDo represents a ToDo item
@@ -55,15 +56,40 @@ func (t *ToDo) TimeAgo() string {
5556
}
5657

5758
// FindBlame sets the Added and Author fields on the ToDo
58-
func (t *ToDo) FindBlame(commit *object.Commit) error {
59-
blame, err := git.Blame(commit, t.FilePath)
59+
// TODO: find ways to optimize this, set a ceiling to stop searching the history after some time
60+
// run this against a batch of todos so that git history is not traversed per-todo
61+
func (t *ToDo) FindBlame(repo *git.Repository, from *object.Commit) error {
62+
commitIter, err := repo.Log(&git.LogOptions{
63+
From: from.Hash,
64+
})
6065
if err != nil {
6166
return err
6267
}
63-
line := blame.Lines[t.StartLocation.Line]
64-
added := line.Date
65-
t.Added = &added
66-
t.Author = line.Author
68+
defer commitIter.Close()
69+
70+
prevCommit := from
71+
err = commitIter.ForEach(func(commit *object.Commit) error {
72+
prevCommit = commit
73+
f, err := commit.File(t.FilePath)
74+
if err != nil {
75+
return err
76+
}
77+
c, err := f.Contents()
78+
if err != nil {
79+
return err
80+
}
81+
todoPresent := strings.Contains(c, t.String)
82+
83+
if !todoPresent {
84+
return storer.ErrStop
85+
}
86+
return nil
87+
})
88+
t.Author = prevCommit.Author.String()
89+
t.Added = &(prevCommit.Author.When)
90+
if err != nil {
91+
return nil
92+
}
6793
return nil
6894
}
6995

0 commit comments

Comments
 (0)