| 1 |
package packp |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
|
| 7 |
"github.com/go-git/go-git/v5/plumbing" |
| 8 |
"github.com/go-git/go-git/v5/plumbing/format/pktline" |
| 9 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 10 |
) |
| 11 |
|
| 12 |
// Encode writes the ReferenceUpdateRequest encoding to the stream. |
| 13 |
func (req *ReferenceUpdateRequest) Encode(w io.Writer) error { |
| 14 |
if err := req.validate(); err != nil { |
| 15 |
return err |
| 16 |
} |
| 17 |
|
| 18 |
e := pktline.NewEncoder(w) |
| 19 |
|
| 20 |
if err := req.encodeShallow(e, req.Shallow); err != nil { |
| 21 |
return err |
| 22 |
} |
| 23 |
|
| 24 |
if err := req.encodeCommands(e, req.Commands, req.Capabilities); err != nil { |
| 25 |
return err |
| 26 |
} |
| 27 |
|
| 28 |
if req.Capabilities.Supports(capability.PushOptions) { |
| 29 |
if err := req.encodeOptions(e, req.Options); err != nil { |
| 30 |
return err |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
if req.Packfile != nil { |
| 35 |
if _, err := io.Copy(w, req.Packfile); err != nil { |
| 36 |
return err |
| 37 |
} |
| 38 |
|
| 39 |
return req.Packfile.Close() |
| 40 |
} |
| 41 |
|
| 42 |
return nil |
| 43 |
} |
| 44 |
|
| 45 |
func (req *ReferenceUpdateRequest) encodeShallow(e *pktline.Encoder, |
| 46 |
h *plumbing.Hash) error { |
| 47 |
|
| 48 |
if h == nil { |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
objId := []byte(h.String()) |
| 53 |
return e.Encodef("%s%s", shallow, objId) |
| 54 |
} |
| 55 |
|
| 56 |
func (req *ReferenceUpdateRequest) encodeCommands(e *pktline.Encoder, |
| 57 |
cmds []*Command, cap *capability.List) error { |
| 58 |
|
| 59 |
if err := e.Encodef("%s\x00%s", |
| 60 |
formatCommand(cmds[0]), cap.String()); err != nil { |
| 61 |
return err |
| 62 |
} |
| 63 |
|
| 64 |
for _, cmd := range cmds[1:] { |
| 65 |
if err := e.Encodef("%s", formatCommand(cmd)); err != nil { |
| 66 |
return err |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return e.Flush() |
| 71 |
} |
| 72 |
|
| 73 |
func formatCommand(cmd *Command) string { |
| 74 |
o := cmd.Old.String() |
| 75 |
n := cmd.New.String() |
| 76 |
return fmt.Sprintf("%s %s %s", o, n, cmd.Name) |
| 77 |
} |
| 78 |
|
| 79 |
func (req *ReferenceUpdateRequest) encodeOptions(e *pktline.Encoder, |
| 80 |
opts []*Option) error { |
| 81 |
|
| 82 |
for _, opt := range opts { |
| 83 |
if err := e.Encodef("%s=%s", opt.Key, opt.Value); err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return e.Flush() |
| 89 |
} |