diff options
author | Santiago M. Mola <santi@mola.io> | 2016-11-25 15:48:20 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-11-25 15:48:20 +0100 |
commit | f9adb3565b36ba1573102f954d0ee916009efac2 (patch) | |
tree | abc5b98e61b5851a985a215f7265ce2e9eb131f7 /plumbing/protocol/packp/advrefs_encoder.go | |
parent | c1e0932c6cbcc55a78f338d437b9f13d89f33552 (diff) | |
download | go-git-f9adb3565b36ba1573102f954d0ee916009efac2.tar.gz |
move: format/packp -> protocol/packp (#141)
* move: format/packp -> protocol/packp
* format/packp -> protocol/packp
* format/packp/pktline -> format/pktline.
* move: protocol/packp/ulreq/* -> protocol/packp/*
* protocol/packp: rename UlReq types to make them unique.
* * protocol/packp: namespace UlReq encoder.
* protocol/packp: namespace UlReq decoder.
* protocol/packp: fix example names
* move: protocol/packp/advrefs/* -> protocol/packp/*
* further ulreq namespacing
* protocol/packp: namespace AdvRefs types.
Diffstat (limited to 'plumbing/protocol/packp/advrefs_encoder.go')
-rw-r--r-- | plumbing/protocol/packp/advrefs_encoder.go | 154 |
1 files changed, 154 insertions, 0 deletions
diff --git a/plumbing/protocol/packp/advrefs_encoder.go b/plumbing/protocol/packp/advrefs_encoder.go new file mode 100644 index 0000000..cdba188 --- /dev/null +++ b/plumbing/protocol/packp/advrefs_encoder.go @@ -0,0 +1,154 @@ +package packp + +import ( + "bytes" + "io" + "sort" + + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/format/pktline" +) + +// An AdvRefsEncoder writes AdvRefs values to an output stream. +type AdvRefsEncoder struct { + data *AdvRefs // data to encode + pe *pktline.Encoder // where to write the encoded data + err error // sticky error +} + +// NewAdvRefsEncoder returns a new encoder that writes to w. +func NewAdvRefsEncoder(w io.Writer) *AdvRefsEncoder { + return &AdvRefsEncoder{ + pe: pktline.NewEncoder(w), + } +} + +// Encode writes the AdvRefs encoding of v to the stream. +// +// All the payloads will end with a newline character. Capabilities, +// references and shallows are writen in alphabetical order, except for +// peeled references that always follow their corresponding references. +func (e *AdvRefsEncoder) Encode(v *AdvRefs) error { + e.data = v + + for state := encodePrefix; state != nil; { + state = state(e) + } + + return e.err +} + +type encoderStateFn func(*AdvRefsEncoder) encoderStateFn + +func encodePrefix(e *AdvRefsEncoder) encoderStateFn { + for _, p := range e.data.Prefix { + if bytes.Equal(p, pktline.Flush) { + if e.err = e.pe.Flush(); e.err != nil { + return nil + } + continue + } + if e.err = e.pe.Encodef("%s\n", string(p)); e.err != nil { + return nil + } + } + + return encodeFirstLine +} + +// Adds the first pkt-line payload: head hash, head ref and capabilities. +// Also handle the special case when no HEAD ref is found. +func encodeFirstLine(e *AdvRefsEncoder) encoderStateFn { + head := formatHead(e.data.Head) + separator := formatSeparator(e.data.Head) + capabilities := formatCaps(e.data.Capabilities) + + if e.err = e.pe.Encodef("%s %s\x00%s\n", head, separator, capabilities); e.err != nil { + return nil + } + + return encodeRefs +} + +func formatHead(h *plumbing.Hash) string { + if h == nil { + return plumbing.ZeroHash.String() + } + + return h.String() +} + +func formatSeparator(h *plumbing.Hash) string { + if h == nil { + return noHead + } + + return head +} + +func formatCaps(c *Capabilities) string { + if c == nil { + return "" + } + + c.Sort() + + return c.String() +} + +// Adds the (sorted) refs: hash SP refname EOL +// and their peeled refs if any. +func encodeRefs(e *AdvRefsEncoder) encoderStateFn { + refs := sortRefs(e.data.References) + for _, r := range refs { + hash, _ := e.data.References[r] + if e.err = e.pe.Encodef("%s %s\n", hash.String(), r); e.err != nil { + return nil + } + + if hash, ok := e.data.Peeled[r]; ok { + if e.err = e.pe.Encodef("%s %s^{}\n", hash.String(), r); e.err != nil { + return nil + } + } + } + + return encodeShallow +} + +func sortRefs(m map[string]plumbing.Hash) []string { + ret := make([]string, 0, len(m)) + for k := range m { + ret = append(ret, k) + } + sort.Strings(ret) + + return ret +} + +// Adds the (sorted) shallows: "shallow" SP hash EOL +func encodeShallow(e *AdvRefsEncoder) encoderStateFn { + sorted := sortShallows(e.data.Shallows) + for _, hash := range sorted { + if e.err = e.pe.Encodef("shallow %s\n", hash); e.err != nil { + return nil + } + } + + return encodeFlush +} + +func sortShallows(c []plumbing.Hash) []string { + ret := []string{} + for _, h := range c { + ret = append(ret, h.String()) + } + sort.Strings(ret) + + return ret +} + +func encodeFlush(e *AdvRefsEncoder) encoderStateFn { + e.err = e.pe.Flush() + return nil +} |