Welcome to mirror list, hosted at ThFree Co, Russian Federation.

catfile_object_iterator.go « gitpipe « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3aa6ef0256bb5ba5276ec16df7e4bc0310c8bfa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package gitpipe

import (
	"context"

	"gitlab.com/gitlab-org/gitaly/v16/internal/git"
)

// CatfileObjectIterator is an iterator returned by the Revlist function.
type CatfileObjectIterator interface {
	ObjectIterator
	// Result returns the current item.
	Result() CatfileObjectResult
}

// NewCatfileObjectIterator returns a new CatfileObjectIterator for the given items.
func NewCatfileObjectIterator(ctx context.Context, items []CatfileObjectResult) CatfileObjectIterator {
	itemChan := make(chan CatfileObjectResult, len(items))
	for _, item := range items {
		itemChan <- item
	}
	close(itemChan)

	return &catfileObjectIterator{
		ctx: ctx,
		ch:  itemChan,
	}
}

type catfileObjectIterator struct {
	ctx    context.Context
	ch     <-chan CatfileObjectResult
	result CatfileObjectResult
}

func (it *catfileObjectIterator) Next() bool {
	if it.result.err != nil {
		return false
	}

	// Prioritize context cancellation errors so that we don't try to fetch results anymore when
	// the context is done.
	select {
	case <-it.ctx.Done():
		it.result = CatfileObjectResult{err: it.ctx.Err()}
		return false
	default:
	}

	select {
	case <-it.ctx.Done():
		it.result = CatfileObjectResult{err: it.ctx.Err()}
		return false
	case result, ok := <-it.ch:
		if !ok {
			return false
		}

		it.result = result
		if result.err != nil {
			return false
		}

		return true
	}
}

func (it *catfileObjectIterator) Err() error {
	return it.result.err
}

func (it *catfileObjectIterator) Result() CatfileObjectResult {
	return it.result
}

func (it *catfileObjectIterator) ObjectID() git.ObjectID {
	return it.result.ObjectID()
}

func (it *catfileObjectIterator) ObjectName() []byte {
	return it.result.ObjectName
}