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 83 84 85 86 87 88 89 90
|
package git
import (
"bufio"
"context"
"fmt"
"io"
"github.com/github/git-sizer/internal/pipe"
)
// ReferenceIter is an iterator that interates over references.
type ReferenceIter struct {
refCh chan Reference
errCh chan error
}
// NewReferenceIter returns an iterator that iterates over all of the
// references in `repo`.
func (repo *Repository) NewReferenceIter(ctx context.Context) (*ReferenceIter, error) {
iter := ReferenceIter{
refCh: make(chan Reference),
errCh: make(chan error),
}
p := pipe.New()
p.Add(
// Output all references and their values:
pipe.CommandStage(
"git-for-each-ref",
repo.GitCommand(
"for-each-ref",
"--format=%(objectname) %(objecttype) %(objectsize) %(refname)",
),
),
// Read the references and send them to `iter.refCh`, then close
// the channel.
pipe.Function(
"parse-refs",
func(ctx context.Context, env pipe.Env, stdin io.Reader, stdout io.Writer) error {
defer close(iter.refCh)
in := bufio.NewReader(stdin)
for {
line, err := in.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return nil
}
return fmt.Errorf("reading 'git for-each-ref' output: %w", err)
}
ref, err := ParseReference(string(line[:len(line)-1]))
if err != nil {
return fmt.Errorf("parsing 'git for-each-ref' output: %w", err)
}
select {
case iter.refCh <- ref:
case <-ctx.Done():
return ctx.Err()
}
}
},
),
)
err := p.Start(ctx)
if err != nil {
return nil, err
}
go func() {
iter.errCh <- p.Wait()
}()
return &iter, nil
}
// Next returns either the next reference or a boolean `false` value
// indicating that the iteration is over. On errors, return an error
// (in this case, the caller must still call `Close()`).
func (iter *ReferenceIter) Next() (Reference, bool, error) {
ref, ok := <-iter.refCh
if !ok {
return Reference{}, false, <-iter.errCh
}
return ref, true, nil
}
|