Skip to content

Commit f54ee6d

Browse files
committed
add NewCommitPathIterFromIter that accepts pathFilter func(string) bool
keep NewCommitFileIterFromIter for compatibilty for now Signed-off-by: Saeed Rasooli <[email protected]>
1 parent 1a7db85 commit f54ee6d

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

plumbing/object/commit_walker_file.go renamed to plumbing/object/commit_walker_path.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,39 @@ import (
88
"gopkg.in/src-d/go-git.v4/plumbing/storer"
99
)
1010

11-
type commitFileIter struct {
12-
fileName string
11+
type commitPathIter struct {
12+
pathFilter func(string) bool
1313
sourceIter CommitIter
1414
currentCommit *Commit
1515
checkParent bool
1616
}
1717

18-
// NewCommitFileIterFromIter returns a commit iterator which performs diffTree between
18+
// NewCommitPathIterFromIter returns a commit iterator which performs diffTree between
1919
// successive trees returned from the commit iterator from the argument. The purpose of this is
2020
// to find the commits that explain how the files that match the path came to be.
2121
// If checkParent is true then the function double checks if potential parent (next commit in a path)
2222
// 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)
2526
iterator.sourceIter = commitIter
26-
iterator.fileName = fileName
27+
iterator.pathFilter = pathFilter
2728
iterator.checkParent = checkParent
2829
return iterator
2930
}
3031

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) {
3244
if c.currentCommit == nil {
3345
var err error
3446
c.currentCommit, err = c.sourceIter.Next()
@@ -45,7 +57,7 @@ func (c *commitFileIter) Next() (*Commit, error) {
4557
return commit, commitErr
4658
}
4759

48-
func (c *commitFileIter) getNextFileCommit() (*Commit, error) {
60+
func (c *commitPathIter) getNextFileCommit() (*Commit, error) {
4961
for {
5062
// Parent-commit can be nil if the current-commit is the initial commit
5163
parentCommit, parentCommitErr := c.sourceIter.Next()
@@ -96,9 +108,9 @@ func (c *commitFileIter) getNextFileCommit() (*Commit, error) {
96108
}
97109
}
98110

99-
func (c *commitFileIter) hasFileChange(changes Changes, parent *Commit) bool {
111+
func (c *commitPathIter) hasFileChange(changes Changes, parent *Commit) bool {
100112
for _, change := range changes {
101-
if change.name() != c.fileName {
113+
if !c.pathFilter(change.name()) {
102114
continue
103115
}
104116

@@ -125,7 +137,7 @@ func isParentHash(hash plumbing.Hash, commit *Commit) bool {
125137
return false
126138
}
127139

128-
func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
140+
func (c *commitPathIter) ForEach(cb func(*Commit) error) error {
129141
for {
130142
commit, nextErr := c.Next()
131143
if nextErr == io.EOF {
@@ -144,6 +156,6 @@ func (c *commitFileIter) ForEach(cb func(*Commit) error) error {
144156
return nil
145157
}
146158

147-
func (c *commitFileIter) Close() {
159+
func (c *commitPathIter) Close() {
148160
c.sourceIter.Close()
149161
}

repository.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,13 @@ func (r *Repository) logAll(commitIterFunc func(*object.Commit) object.CommitIte
10991099
}
11001100

11011101
func (*Repository) logWithFile(fileName string, commitIter object.CommitIter, checkParent bool) object.CommitIter {
1102-
return object.NewCommitFileIterFromIter(fileName, commitIter, checkParent)
1102+
return object.NewCommitPathIterFromIter(
1103+
func(path string) bool {
1104+
return path == fileName
1105+
},
1106+
commitIter,
1107+
checkParent,
1108+
)
11031109
}
11041110

11051111
func (*Repository) logWithLimit(commitIter object.CommitIter, limitOptions object.LogLimitOptions) object.CommitIter {

repository_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1667,7 +1667,10 @@ func (m *mockErrCommitIter) Close() {}
16671667

16681668
func (s *RepositorySuite) TestLogFileWithError(c *C) {
16691669
fileName := "README"
1670-
cIter := object.NewCommitFileIterFromIter(fileName, &mockErrCommitIter{}, false)
1670+
pathIter := func(path string) bool {
1671+
return path == fileName
1672+
}
1673+
cIter := object.NewCommitPathIterFromIter(pathIter, &mockErrCommitIter{}, false)
16711674
defer cIter.Close()
16721675

16731676
err := cIter.ForEach(func(commit *object.Commit) error {
@@ -2615,9 +2618,9 @@ func (s *RepositorySuite) TestResolveRevisionWithErrors(c *C) {
26152618
c.Assert(err, IsNil)
26162619

26172620
datas := map[string]string{
2618-
"efs/heads/master~": "reference not found",
2619-
"HEAD^3": `Revision invalid : "3" found must be 0, 1 or 2 after "^"`,
2620-
"HEAD^{/whatever}": `No commit message match regexp : "whatever"`,
2621+
"efs/heads/master~": "reference not found",
2622+
"HEAD^3": `Revision invalid : "3" found must be 0, 1 or 2 after "^"`,
2623+
"HEAD^{/whatever}": `No commit message match regexp : "whatever"`,
26212624
"4e1243bd22c66e76c2ba9eddc1f91394e57f9f83": "reference not found",
26222625
}
26232626

0 commit comments

Comments
 (0)