NewObjectIter returns an iterator that iterates over objects in `repo`. The arguments are passed to `git rev-list --objects`. The second return value is the stdin of the `rev-list` command. The caller can feed values into it but must close it in any case.
(ctx context.Context)
| 23 | // second return value is the stdin of the `rev-list` command. The |
| 24 | // caller can feed values into it but must close it in any case. |
| 25 | func (repo *Repository) NewObjectIter(ctx context.Context) (*ObjectIter, error) { |
| 26 | iter := ObjectIter{ |
| 27 | ctx: ctx, |
| 28 | p: pipe.New(), |
| 29 | oidCh: make(chan OID), |
| 30 | errCh: make(chan error), |
| 31 | headerCh: make(chan BatchHeader), |
| 32 | } |
| 33 | |
| 34 | iter.p.Add( |
| 35 | // Read OIDs from `iter.oidCh` and write them to `git |
| 36 | // rev-list`: |
| 37 | pipe.Function( |
| 38 | "request-objects", |
| 39 | func(ctx context.Context, _ pipe.Env, _ io.Reader, stdout io.Writer) error { |
| 40 | out := bufio.NewWriter(stdout) |
| 41 | |
| 42 | for { |
| 43 | select { |
| 44 | case oid, ok := <-iter.oidCh: |
| 45 | if !ok { |
| 46 | return out.Flush() |
| 47 | } |
| 48 | if _, err := fmt.Fprintln(out, oid.String()); err != nil { |
| 49 | return fmt.Errorf("writing to 'git cat-file': %w", err) |
| 50 | } |
| 51 | case <-ctx.Done(): |
| 52 | return ctx.Err() |
| 53 | } |
| 54 | } |
| 55 | }, |
| 56 | ), |
| 57 | |
| 58 | // Walk starting at the OIDs on `stdin` and output the OIDs |
| 59 | // (possibly followed by paths) of all of the Git objects |
| 60 | // found. |
| 61 | pipe.CommandStage( |
| 62 | "git-rev-list", |
| 63 | repo.GitCommand("rev-list", "--objects", "--stdin", "--date-order"), |
| 64 | ), |
| 65 | |
| 66 | // Read the output of `git rev-list --objects`, strip off any |
| 67 | // trailing information, and write the OIDs to `git cat-file`: |
| 68 | pipe.LinewiseFunction( |
| 69 | "copy-oids", |
| 70 | func(_ context.Context, _ pipe.Env, line []byte, stdout *bufio.Writer) error { |
| 71 | if len(line) < 40 { |
| 72 | return fmt.Errorf("line too short: '%s'", line) |
| 73 | } |
| 74 | if _, err := stdout.Write(line[:40]); err != nil { |
| 75 | return fmt.Errorf("writing OID to 'git cat-file': %w", err) |
| 76 | } |
| 77 | if err := stdout.WriteByte('\n'); err != nil { |
| 78 | return fmt.Errorf("writing LF to 'git cat-file': %w", err) |
| 79 | } |
| 80 | return nil |
| 81 | }, |
| 82 | ), |
no test coverage detected