Skip to content

Commit 1a26767

Browse files
committed
gitbase: use general CommitIter with filter
Added a new iterator (CommitsByHashIter) that can use a list of hashes to iterate only of those commits. If the hash list is empty it does a normal full scan. Change commits and commit_trees table to use it. Signed-off-by: Javi Fontan <[email protected]>
1 parent 575c790 commit 1a26767

File tree

2 files changed

+86
-50
lines changed

2 files changed

+86
-50
lines changed

commit_trees.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (i *commitTreesIter) NewIterator(repo *Repository) (RowRepoIter, error) {
115115
var commits object.CommitIter
116116
if len(i.repos) == 0 || stringContains(i.repos, repo.ID) {
117117
var err error
118-
commits, err = repo.Repo.CommitObjects()
118+
commits, err = NewCommitsByHashIter(repo, i.commitHashes)
119119
if err != nil {
120120
return nil, err
121121
}
@@ -130,14 +130,6 @@ func (i *commitTreesIter) NewIterator(repo *Repository) (RowRepoIter, error) {
130130
}, nil
131131
}
132132

133-
func (i *commitTreesIter) shouldVisitCommit(commit *object.Commit) bool {
134-
if len(i.commitHashes) > 0 && !stringContains(i.commitHashes, commit.Hash.String()) {
135-
return false
136-
}
137-
138-
return true
139-
}
140-
141133
func (i *commitTreesIter) Next() (sql.Row, error) {
142134
s, ok := i.ctx.Session.(*Session)
143135
if !ok {
@@ -164,10 +156,6 @@ func (i *commitTreesIter) Next() (sql.Row, error) {
164156
return nil, err
165157
}
166158

167-
if !i.shouldVisitCommit(commit) {
168-
continue
169-
}
170-
171159
tree, err := commit.Tree()
172160
if err != nil {
173161
if s.SkipGitErrors {

commits.go

Lines changed: 85 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (r *commitsTable) WithProjectAndFilters(
9999
return nil, err
100100
}
101101

102-
return &commitsByHashIter{hashes: hashes}, nil
102+
return &commitIter{hashes: hashes}, nil
103103
},
104104
)
105105

@@ -114,10 +114,11 @@ func (r *commitsTable) WithProjectAndFilters(
114114
type commitIter struct {
115115
repoID string
116116
iter object.CommitIter
117+
hashes []string
117118
}
118119

119120
func (i *commitIter) NewIterator(repo *Repository) (RowRepoIter, error) {
120-
iter, err := repo.Repo.CommitObjects()
121+
iter, err := NewCommitsByHashIter(repo, i.hashes)
121122
if err != nil {
122123
return nil, err
123124
}
@@ -142,41 +143,6 @@ func (i *commitIter) Close() error {
142143
return nil
143144
}
144145

145-
type commitsByHashIter struct {
146-
repo *Repository
147-
pos int
148-
hashes []string
149-
}
150-
151-
func (i *commitsByHashIter) NewIterator(repo *Repository) (RowRepoIter, error) {
152-
return &commitsByHashIter{repo, 0, i.hashes}, nil
153-
}
154-
155-
func (i *commitsByHashIter) Next() (sql.Row, error) {
156-
for {
157-
if i.pos >= len(i.hashes) {
158-
return nil, io.EOF
159-
}
160-
161-
hash := plumbing.NewHash(i.hashes[i.pos])
162-
i.pos++
163-
commit, err := i.repo.Repo.CommitObject(hash)
164-
if err == plumbing.ErrObjectNotFound {
165-
continue
166-
}
167-
168-
if err != nil {
169-
return nil, err
170-
}
171-
172-
return commitToRow(i.repo.ID, commit), nil
173-
}
174-
}
175-
176-
func (i *commitsByHashIter) Close() error {
177-
return nil
178-
}
179-
180146
func commitToRow(repoID string, c *object.Commit) sql.Row {
181147
return sql.NewRow(
182148
repoID,
@@ -201,3 +167,85 @@ func getParentHashes(c *object.Commit) []interface{} {
201167

202168
return parentHashes
203169
}
170+
171+
type commitsByHashIter struct {
172+
repo *Repository
173+
hashes []string
174+
pos int
175+
commitIter object.CommitIter
176+
}
177+
178+
// NewCommitsByHashIter creates a CommitIter that can use a list of hashes
179+
// to iterate. If the list is empty it scans all commits.
180+
func NewCommitsByHashIter(
181+
repo *Repository,
182+
hashes []string,
183+
) (object.CommitIter, error) {
184+
var commitIter object.CommitIter
185+
var err error
186+
if len(hashes) == 0 {
187+
commitIter, err = repo.Repo.CommitObjects()
188+
if err != nil {
189+
return nil, err
190+
}
191+
}
192+
193+
return &commitsByHashIter{
194+
repo: repo,
195+
hashes: hashes,
196+
commitIter: commitIter,
197+
}, nil
198+
}
199+
200+
func (i *commitsByHashIter) Next() (*object.Commit, error) {
201+
if i.commitIter != nil {
202+
return i.nextScan()
203+
}
204+
205+
return i.nextList()
206+
}
207+
208+
func (i *commitsByHashIter) ForEach(f func(*object.Commit) error) error {
209+
for {
210+
c, err := i.Next()
211+
if err != nil {
212+
return err
213+
}
214+
215+
err = f(c)
216+
if err != nil {
217+
return err
218+
}
219+
}
220+
}
221+
222+
func (i *commitsByHashIter) Close() {
223+
if i.commitIter != nil {
224+
i.commitIter.Close()
225+
}
226+
}
227+
228+
func (i *commitsByHashIter) nextScan() (*object.Commit, error) {
229+
return i.commitIter.Next()
230+
}
231+
232+
func (i *commitsByHashIter) nextList() (*object.Commit, error) {
233+
for {
234+
if i.pos >= len(i.hashes) {
235+
return nil, io.EOF
236+
}
237+
238+
hash := plumbing.NewHash(i.hashes[i.pos])
239+
i.pos++
240+
commit, err := i.repo.Repo.CommitObject(hash)
241+
if err == plumbing.ErrObjectNotFound {
242+
continue
243+
}
244+
245+
if err != nil {
246+
return nil, err
247+
}
248+
249+
return commit, nil
250+
}
251+
}

0 commit comments

Comments
 (0)