aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/updreq.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.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.go')
-rw-r--r--plumbing/protocol/packp/updreq.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/plumbing/protocol/packp/updreq.go b/plumbing/protocol/packp/updreq.go
new file mode 100644
index 0000000..90d6e09
--- /dev/null
+++ b/plumbing/protocol/packp/updreq.go
@@ -0,0 +1,84 @@
+package packp
+
+import (
+ "errors"
+
+ "gopkg.in/src-d/go-git.v4/plumbing"
+ "gopkg.in/src-d/go-git.v4/plumbing/protocol/packp/capability"
+)
+
+var (
+ ErrEmptyCommands = errors.New("commands cannot be empty")
+ ErrMalformedCommand = errors.New("malformed command")
+)
+
+// ReferenceUpdateRequest values represent reference upload requests.
+// Values from this type are not zero-value safe, use the New function instead.
+//
+// TODO: Add support for push-cert
+type ReferenceUpdateRequest struct {
+ Capabilities *capability.List
+ Commands []*Command
+ Shallow *plumbing.Hash
+}
+
+// New returns a pointer to a new ReferenceUpdateRequest value.
+func NewReferenceUpdateRequest() *ReferenceUpdateRequest {
+ return &ReferenceUpdateRequest{
+ Capabilities: capability.NewList(),
+ Commands: nil,
+ }
+}
+
+func (r *ReferenceUpdateRequest) validate() error {
+ if len(r.Commands) == 0 {
+ return ErrEmptyCommands
+ }
+
+ for _, c := range r.Commands {
+ if err := c.validate(); err != nil {
+ return err
+ }
+ }
+
+ return nil
+}
+
+type Action string
+
+const (
+ Create Action = "create"
+ Update = "update"
+ Delete = "delete"
+ Invalid = "invalid"
+)
+
+type Command struct {
+ Name string
+ Old plumbing.Hash
+ New plumbing.Hash
+}
+
+func (c *Command) Action() Action {
+ if c.Old == plumbing.ZeroHash && c.New == plumbing.ZeroHash {
+ return Invalid
+ }
+
+ if c.Old == plumbing.ZeroHash {
+ return Create
+ }
+
+ if c.New == plumbing.ZeroHash {
+ return Delete
+ }
+
+ return Update
+}
+
+func (c *Command) validate() error {
+ if c.Action() == Invalid {
+ return ErrMalformedCommand
+ }
+
+ return nil
+}