Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit eb57cb0

Browse files
committed
iters: add IsClosed field, fixes #4
1 parent 491de64 commit eb57cb0

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

commit.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,28 @@ func (i *CommitIter) Next() (*Commit, error) {
113113
type iter struct {
114114
ch chan core.Object
115115
r *Repository
116+
117+
IsClosed bool
116118
}
117119

118120
func newIter(r *Repository) iter {
119121
ch := make(chan core.Object, 1)
120-
return iter{ch, r}
122+
return iter{ch: ch, r: r}
121123
}
122124

123125
func (i *iter) Add(o core.Object) {
126+
if i.IsClosed {
127+
return
128+
}
129+
124130
i.ch <- o
125131
}
126132

127133
func (i *iter) Close() {
134+
if i.IsClosed {
135+
return
136+
}
137+
138+
defer func() { i.IsClosed = true }()
128139
close(i.ch)
129140
}

commit_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package git
2+
3+
import (
4+
. "gopkg.in/check.v1"
5+
"gopkg.in/src-d/go-git.v2/core"
6+
)
7+
8+
type CommitCommon struct{}
9+
10+
var _ = Suite(&CommitCommon{})
11+
12+
func (s *CommitCommon) TestIterClose(c *C) {
13+
i := &iter{ch: make(chan core.Object, 1)}
14+
i.Close()
15+
i.Close()
16+
}

repository_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ func (s *SuiteRepository) TestCommits(c *C) {
6969

7070
c.Assert(count, Equals, 8)
7171
}
72+
73+
func (s *SuiteRepository) TestCommitIterClosePanic(c *C) {
74+
r, err := NewRepository(RepositoryFixture, nil)
75+
r.Remotes["origin"].upSrv = &MockGitUploadPackService{}
76+
77+
c.Assert(err, IsNil)
78+
c.Assert(r.Pull("origin", "refs/heads/master"), IsNil)
79+
80+
commits := r.Commits()
81+
commits.Close()
82+
}

0 commit comments

Comments
 (0)