@@ -8,27 +8,39 @@ import (
8
8
"gopkg.in/src-d/go-git.v4/plumbing/storer"
9
9
)
10
10
11
- type commitFileIter struct {
12
- fileName string
11
+ type commitPathIter struct {
12
+ pathFilter func ( string ) bool
13
13
sourceIter CommitIter
14
14
currentCommit * Commit
15
15
checkParent bool
16
16
}
17
17
18
- // NewCommitFileIterFromIter returns a commit iterator which performs diffTree between
18
+ // NewCommitPathIterFromIter returns a commit iterator which performs diffTree between
19
19
// successive trees returned from the commit iterator from the argument. The purpose of this is
20
20
// to find the commits that explain how the files that match the path came to be.
21
21
// If checkParent is true then the function double checks if potential parent (next commit in a path)
22
22
// is one of the parents in the tree (it's used by `git log --all`).
23
- func NewCommitFileIterFromIter (fileName string , commitIter CommitIter , checkParent bool ) CommitIter {
24
- iterator := new (commitFileIter )
23
+ // pathFilter is a function that takes path of file as argument and returns true if we want it
24
+ func NewCommitPathIterFromIter (pathFilter func (string ) bool , commitIter CommitIter , checkParent bool ) CommitIter {
25
+ iterator := new (commitPathIter )
25
26
iterator .sourceIter = commitIter
26
- iterator .fileName = fileName
27
+ iterator .pathFilter = pathFilter
27
28
iterator .checkParent = checkParent
28
29
return iterator
29
30
}
30
31
31
- func (c * commitFileIter ) Next () (* Commit , error ) {
32
+ // this function is kept for compatibilty, can be replaced with NewCommitPathIterFromIter
33
+ func NewCommitFileIterFromIter (fileName string , commitIter CommitIter , checkParent bool ) CommitIter {
34
+ return NewCommitPathIterFromIter (
35
+ func (path string ) bool {
36
+ return path == fileName
37
+ },
38
+ commitIter ,
39
+ checkParent ,
40
+ )
41
+ }
42
+
43
+ func (c * commitPathIter ) Next () (* Commit , error ) {
32
44
if c .currentCommit == nil {
33
45
var err error
34
46
c .currentCommit , err = c .sourceIter .Next ()
@@ -45,7 +57,7 @@ func (c *commitFileIter) Next() (*Commit, error) {
45
57
return commit , commitErr
46
58
}
47
59
48
- func (c * commitFileIter ) getNextFileCommit () (* Commit , error ) {
60
+ func (c * commitPathIter ) getNextFileCommit () (* Commit , error ) {
49
61
for {
50
62
// Parent-commit can be nil if the current-commit is the initial commit
51
63
parentCommit , parentCommitErr := c .sourceIter .Next ()
@@ -96,9 +108,9 @@ func (c *commitFileIter) getNextFileCommit() (*Commit, error) {
96
108
}
97
109
}
98
110
99
- func (c * commitFileIter ) hasFileChange (changes Changes , parent * Commit ) bool {
111
+ func (c * commitPathIter ) hasFileChange (changes Changes , parent * Commit ) bool {
100
112
for _ , change := range changes {
101
- if change .name () != c . fileName {
113
+ if ! c . pathFilter ( change .name ()) {
102
114
continue
103
115
}
104
116
@@ -125,7 +137,7 @@ func isParentHash(hash plumbing.Hash, commit *Commit) bool {
125
137
return false
126
138
}
127
139
128
- func (c * commitFileIter ) ForEach (cb func (* Commit ) error ) error {
140
+ func (c * commitPathIter ) ForEach (cb func (* Commit ) error ) error {
129
141
for {
130
142
commit , nextErr := c .Next ()
131
143
if nextErr == io .EOF {
@@ -144,6 +156,6 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
144
156
return nil
145
157
}
146
158
147
- func (c * commitFileIter ) Close () {
159
+ func (c * commitPathIter ) Close () {
148
160
c .sourceIter .Close ()
149
161
}
0 commit comments