aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/updreq_encode.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2016-12-05 11:59:49 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2016-12-05 11:59:49 +0100
commit0042bb031676a20ffc789f94e332a6da70e2756d (patch)
tree22a142575c6f12894b4faec73807b57afc287529 /plumbing/protocol/packp/updreq_encode.go
parent19f59e782b92d32cc430619c77053c764a3180f9 (diff)
downloadgo-git-0042bb031676a20ffc789f94e332a6da70e2756d.tar.gz
protocol/packp: add reference update request encoder. (#147)
* add ReferenceUpdateRequest struct. * add ReferenceUpdateRequest decoder. * add ReferenceUpdateRequest encoder.
Diffstat (limited to 'plumbing/protocol/packp/updreq_encode.go')
-rw-r--r--plumbing/protocol/packp/updreq_encode.go67
1 files changed, 67 insertions, 0 deletions
diff --git a/plumbing/protocol/packp/updreq_encode.go b/plumbing/protocol/packp/updreq_encode.go
new file mode 100644
index 0000000..b2b7944
--- /dev/null
+++ b/plumbing/protocol/packp/updreq_encode.go
@@ -0,0 +1,67 @@
+package packp
+
+import (
+ "fmt"
+ "io"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/format/pktline"
+ "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
+)
+
+var (
+ zeroHashString = plumbing.ZeroHash.String()
+)
+
+// Encode writes the ReferenceUpdateRequest encoding to the stream.
+func (r *ReferenceUpdateRequest) Encode(w io.Writer) error {
+ if err := r.validate(); err != nil {
+ return err
+ }
+
+ e := pktline.NewEncoder(w)
+
+ if err := r.encodeShallow(e, r.Shallow); err != nil {
+ return err
+ }
+
+ if err := r.encodeCommands(e, r.Commands, r.Capabilities); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+func (r *ReferenceUpdateRequest) encodeShallow(e *pktline.Encoder,
+ h *plumbing.Hash) error {
+
+ if h == nil {
+ return nil
+ }
+
+ objId := []byte(h.String())
+ return e.Encodef("%s%s", shallow, objId)
+}
+
+func (r *ReferenceUpdateRequest) encodeCommands(e *pktline.Encoder,
+ cmds []*Command, cap *capability.List) error {
+
+ if err := e.Encodef("%s\x00%s",
+ formatCommand(cmds[0]), cap.String()); err != nil {
+ return err
+ }
+
+ for _, cmd := range cmds[1:] {
+ if err := e.Encodef(formatCommand(cmd)); err != nil {
+ return err
+ }
+ }
+
+ return e.Flush()
+}
+
+func formatCommand(cmd *Command) string {
+ o := cmd.Old.String()
+ n := cmd.New.String()
+ return fmt.Sprintf("%s %s %s", o, n, cmd.Name)
+}