MCPcopy
hub / github.com/github/git-sizer / NewReferenceIter

Method NewReferenceIter

git/ref_iter.go:20–78  ·  view source on GitHub ↗

NewReferenceIter returns an iterator that iterates over all of the references in `repo`.

(ctx context.Context)

Source from the content-addressed store, hash-verified

18// NewReferenceIter returns an iterator that iterates over all of the
19// references in `repo`.
20func (repo *Repository) NewReferenceIter(ctx context.Context) (*ReferenceIter, error) {
21 iter := ReferenceIter{
22 refCh: make(chan Reference),
23 errCh: make(chan error),
24 }
25
26 p := pipe.New()
27 p.Add(
28 // Output all references and their values:
29 pipe.CommandStage(
30 "git-for-each-ref",
31 repo.GitCommand(
32 "for-each-ref",
33 "--format=%(objectname) %(objecttype) %(objectsize) %(refname)",
34 ),
35 ),
36
37 // Read the references and send them to `iter.refCh`, then close
38 // the channel.
39 pipe.Function(
40 "parse-refs",
41 func(ctx context.Context, env pipe.Env, stdin io.Reader, stdout io.Writer) error {
42 defer close(iter.refCh)
43
44 in := bufio.NewReader(stdin)
45 for {
46 line, err := in.ReadBytes('\n')
47 if err != nil {
48 if err == io.EOF {
49 return nil
50 }
51 return fmt.Errorf("reading 'git for-each-ref' output: %w", err)
52 }
53
54 ref, err := ParseReference(string(line[:len(line)-1]))
55 if err != nil {
56 return fmt.Errorf("parsing 'git for-each-ref' output: %w", err)
57 }
58 select {
59 case iter.refCh <- ref:
60 case <-ctx.Done():
61 return ctx.Err()
62 }
63 }
64 },
65 ),
66 )
67
68 err := p.Start(ctx)
69 if err != nil {
70 return nil, err
71 }
72
73 go func() {
74 iter.errCh <- p.Wait()
75 }()
76
77 return &iter, nil

Callers 1

ScanRepositoryUsingGraphFunction · 0.80

Calls 10

GitCommandMethod · 0.95
NewFunction · 0.92
CommandStageFunction · 0.92
FunctionFunction · 0.92
ParseReferenceFunction · 0.85
ErrMethod · 0.80
AddMethod · 0.65
DoneMethod · 0.65
StartMethod · 0.65
WaitMethod · 0.65

Tested by

no test coverage detected