aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/protocol/packp/advrefs.go
diff options
context:
space:
mode:
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)
+}