| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"io" |
| 6 |
|
| 7 |
"bufio" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 10 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 11 |
) |
| 12 |
|
| 13 |
// ErrUploadPackResponseNotDecoded is returned if Read is called without |
| 14 |
// decoding first |
| 15 |
var ErrUploadPackResponseNotDecoded = errors.New("upload-pack-response should be decoded") |
| 16 |
|
| 17 |
// UploadPackResponse contains all the information responded by the upload-pack |
| 18 |
// service, the response implements io.ReadCloser that allows to read the |
| 19 |
// packfile directly from it. |
| 20 |
type UploadPackResponse struct { |
| 21 |
ShallowUpdate |
| 22 |
ServerResponse |
| 23 |
|
| 24 |
r io.ReadCloser |
| 25 |
isShallow bool |
| 26 |
isMultiACK bool |
| 27 |
} |
| 28 |
|
| 29 |
// NewUploadPackResponse create a new UploadPackResponse instance, the request |
| 30 |
// being responded by the response is required. |
| 31 |
func NewUploadPackResponse(req *UploadPackRequest) *UploadPackResponse { |
| 32 |
isShallow := !req.Depth.IsZero() |
| 33 |
isMultiACK := req.Capabilities.Supports(capability.MultiACK) || |
| 34 |
req.Capabilities.Supports(capability.MultiACKDetailed) |
| 35 |
|
| 36 |
return &UploadPackResponse{ |
| 37 |
isShallow: isShallow, |
| 38 |
isMultiACK: isMultiACK, |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
// NewUploadPackResponseWithPackfile creates a new UploadPackResponse instance, |
| 43 |
// and sets its packfile reader. |
| 44 |
func NewUploadPackResponseWithPackfile(req *UploadPackRequest, |
| 45 |
pf io.ReadCloser) *UploadPackResponse { |
| 46 |
|
| 47 |
r := NewUploadPackResponse(req) |
| 48 |
r.r = pf |
| 49 |
return r |
| 50 |
} |
| 51 |
|
| 52 |
// Decode decodes all the responses sent by upload-pack service into the struct |
| 53 |
// and prepares it to read the packfile using the Read method |
| 54 |
func (r *UploadPackResponse) Decode(reader io.ReadCloser) error { |
| 55 |
buf := bufio.NewReader(reader) |
| 56 |
|
| 57 |
if r.isShallow { |
| 58 |
if err := r.ShallowUpdate.Decode(buf); err != nil { |
| 59 |
return err |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
if err := r.ServerResponse.Decode(buf, r.isMultiACK); err != nil { |
| 64 |
return err |
| 65 |
} |
| 66 |
|
| 67 |
// now the reader is ready to read the packfile content |
| 68 |
r.r = ioutil.NewReadCloser(buf, reader) |
| 69 |
|
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
// Encode encodes an UploadPackResponse. |
| 74 |
func (r *UploadPackResponse) Encode(w io.Writer) (err error) { |
| 75 |
if r.isShallow { |
| 76 |
if err := r.ShallowUpdate.Encode(w); err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if err := r.ServerResponse.Encode(w, r.isMultiACK); err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
|
| 85 |
defer ioutil.CheckClose(r.r, &err) |
| 86 |
_, err = io.Copy(w, r.r) |
| 87 |
return err |
| 88 |
} |
| 89 |
|
| 90 |
// Read reads the packfile data, if the request was done with any Sideband |
| 91 |
// capability the content read should be demultiplexed. If the methods wasn't |
| 92 |
// called before the ErrUploadPackResponseNotDecoded will be return |
| 93 |
func (r *UploadPackResponse) Read(p []byte) (int, error) { |
| 94 |
if r.r == nil { |
| 95 |
return 0, ErrUploadPackResponseNotDecoded |
| 96 |
} |
| 97 |
|
| 98 |
return r.r.Read(p) |
| 99 |
} |
| 100 |
|
| 101 |
// Close the underlying reader, if any |
| 102 |
func (r *UploadPackResponse) Close() error { |
| 103 |
if r.r == nil { |
| 104 |
return nil |
| 105 |
} |
| 106 |
|
| 107 |
return r.r.Close() |
| 108 |
} |