From 68893edf9ddc3de181431f1552e3b773cb66f080 Mon Sep 17 00:00:00 2001 From: "Santiago M. Mola" Date: Mon, 28 Nov 2016 09:57:38 +0100 Subject: remove old types from transport and use packp (#142) * protocol: move UploadPackRequest to protocol. * UploadPackRequest is now defined as an embedding of UploadRequest and UploadHaves. * Move http encoding specific code from UploadPackRequest to transport/http. * rename UlReq to UploadRequest * packp: move AdvRefs Encoder/Decoder to Encode/Decode methods. * packp: move UploadRequest Encoder/Decoder to Encode/Decode methods. * packp: Remove transport.UploadPackInfo in favor of packp. AdvRefs. --- plumbing/protocol/packp/advrefs.go | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'plumbing/protocol/packp/advrefs.go') diff --git a/plumbing/protocol/packp/advrefs.go b/plumbing/protocol/packp/advrefs.go index c54f9d8..b36b180 100644 --- a/plumbing/protocol/packp/advrefs.go +++ b/plumbing/protocol/packp/advrefs.go @@ -1,7 +1,16 @@ package packp import ( + "fmt" + "strings" + "gopkg.in/src-d/go-git.v4/plumbing" + "gopkg.in/src-d/go-git.v4/plumbing/storer" + "gopkg.in/src-d/go-git.v4/storage/memory" +) + +const ( + symref = "symref" ) // AdvRefs values represent the information transmitted on an @@ -38,3 +47,63 @@ func NewAdvRefs() *AdvRefs { Shallows: []plumbing.Hash{}, } } + +func (a *AdvRefs) AddReference(r *plumbing.Reference) error { + switch r.Type() { + case plumbing.SymbolicReference: + v := fmt.Sprintf("%s:%s", r.Name().String(), r.Target().String()) + a.Capabilities.Add(symref, v) + case plumbing.HashReference: + a.References[r.Name().String()] = r.Hash() + default: + return plumbing.ErrInvalidType + } + + return nil +} + +func (a *AdvRefs) AllReferences() (memory.ReferenceStorage, error) { + s := memory.ReferenceStorage{} + if err := addRefs(s, a); err != nil { + return s, plumbing.NewUnexpectedError(err) + } + + return s, nil +} + +func addRefs(s storer.ReferenceStorer, ar *AdvRefs) error { + for name, hash := range ar.References { + ref := plumbing.NewReferenceFromStrings(name, hash.String()) + if err := s.SetReference(ref); err != nil { + return err + } + } + + return addSymbolicRefs(s, ar) +} + +func addSymbolicRefs(s storer.ReferenceStorer, ar *AdvRefs) error { + if !hasSymrefs(ar) { + return nil + } + + for _, symref := range ar.Capabilities.Get(symref).Values { + chunks := strings.Split(symref, ":") + if len(chunks) != 2 { + err := fmt.Errorf("bad number of `:` in symref value (%q)", symref) + return plumbing.NewUnexpectedError(err) + } + name := plumbing.ReferenceName(chunks[0]) + target := plumbing.ReferenceName(chunks[1]) + ref := plumbing.NewSymbolicReference(name, target) + if err := s.SetReference(ref); err != nil { + return nil + } + } + + return nil +} + +func hasSymrefs(ar *AdvRefs) bool { + return ar.Capabilities.Supports(symref) +} -- cgit