aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/advrefs.go
diff options
context:
space:
mode:
authorSantiago M. Mola <santi@mola.io>2016-11-28 09:57:38 +0100
committerGitHub <noreply@github.com>2016-11-28 09:57:38 +0100
commit68893edf9ddc3de181431f1552e3b773cb66f080 (patch)
treef45e5fa18e10168a278b7d0ed7dea984ff9969f7 /plumbing/protocol/packp/advrefs.go
parentf9adb3565b36ba1573102f954d0ee916009efac2 (diff)
downloadgo-git-68893edf9ddc3de181431f1552e3b773cb66f080.tar.gz
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.
Diffstat (limited to 'plumbing/protocol/packp/advrefs.go')
-rw-r--r--plumbing/protocol/packp/advrefs.go69
1 files changed, 69 insertions, 0 deletions
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)
+}