Debian Codegraph @

plumbing/protocol/packp/advrefs.go

raw
1 package packp
2
3 import (
4 "fmt"
5 "sort"
6 "strings"
7
8 "github.com/go-git/go-git/v5/plumbing"
9 "github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
10 "github.com/go-git/go-git/v5/plumbing/storer"
11 "github.com/go-git/go-git/v5/storage/memory"
12 )
13
14 // AdvRefs values represent the information transmitted on an
15 // advertised-refs message. Values from this type are not zero-value
16 // safe, use the New function instead.
17 type AdvRefs struct {
18 // Prefix stores prefix payloads.
19 //
20 // When using this message over (smart) HTTP, you have to add a pktline
21 // before the whole thing with the following payload:
22 //
23 // '# service=$servicename" LF
24 //
25 // Moreover, some (all) git HTTP smart servers will send a flush-pkt
26 // just after the first pkt-line.
27 //
28 // To accommodate both situations, the Prefix field allow you to store
29 // any data you want to send before the actual pktlines. It will also
30 // be filled up with whatever is found on the line.
31 Prefix [][]byte
32 // Head stores the resolved HEAD reference if present.
33 // This can be present with git-upload-pack, not with git-receive-pack.
34 Head *plumbing.Hash
35 // Capabilities are the capabilities.
36 Capabilities *capability.List
37 // References are the hash references.
38 References map[string]plumbing.Hash
39 // Peeled are the peeled hash references.
40 Peeled map[string]plumbing.Hash
41 // Shallows are the shallow object ids.
42 Shallows []plumbing.Hash
43 }
44
45 // NewAdvRefs returns a pointer to a new AdvRefs value, ready to be used.
46 func NewAdvRefs() *AdvRefs {
47 return &AdvRefs{
48 Prefix: [][]byte{},
49 Capabilities: capability.NewList(),
50 References: make(map[string]plumbing.Hash),
51 Peeled: make(map[string]plumbing.Hash),
52 Shallows: []plumbing.Hash{},
53 }
54 }
55
56 func (a *AdvRefs) AddReference(r *plumbing.Reference) error {
57 switch r.Type() {
58 case plumbing.SymbolicReference:
59 v := fmt.Sprintf("%s:%s", r.Name().String(), r.Target().String())
60 return a.Capabilities.Add(capability.SymRef, v)
61 case plumbing.HashReference:
62 a.References[r.Name().String()] = r.Hash()
63 default:
64 return plumbing.ErrInvalidType
65 }
66
67 return nil
68 }
69
70 func (a *AdvRefs) AllReferences() (memory.ReferenceStorage, error) {
71 s := memory.ReferenceStorage{}
72 if err := a.addRefs(s); err != nil {
73 return s, plumbing.NewUnexpectedError(err)
74 }
75
76 return s, nil
77 }
78
79 func (a *AdvRefs) addRefs(s storer.ReferenceStorer) error {
80 for name, hash := range a.References {
81 ref := plumbing.NewReferenceFromStrings(name, hash.String())
82 if err := s.SetReference(ref); err != nil {
83 return err
84 }
85 }
86
87 if a.supportSymrefs() {
88 return a.addSymbolicRefs(s)
89 }
90
91 return a.resolveHead(s)
92 }
93
94 // If the server does not support symrefs capability,
95 // we need to guess the reference where HEAD is pointing to.
96 //
97 // Git versions prior to 1.8.4.3 has an special procedure to get
98 // the reference where is pointing to HEAD:
99 // - Check if a reference called master exists. If exists and it
100 // has the same hash as HEAD hash, we can say that HEAD is pointing to master
101 // - If master does not exists or does not have the same hash as HEAD,
102 // order references and check in that order if that reference has the same
103 // hash than HEAD. If yes, set HEAD pointing to that branch hash
104 // - If no reference is found, throw an error
105 func (a *AdvRefs) resolveHead(s storer.ReferenceStorer) error {
106 if a.Head == nil {
107 return nil
108 }
109
110 ref, err := s.Reference(plumbing.Master)
111
112 // check first if HEAD is pointing to master
113 if err == nil {
114 ok, err := a.createHeadIfCorrectReference(ref, s)
115 if err != nil {
116 return err
117 }
118
119 if ok {
120 return nil
121 }
122 }
123
124 if err != nil && err != plumbing.ErrReferenceNotFound {
125 return err
126 }
127
128 // From here we are trying to guess the branch that HEAD is pointing
129 refIter, err := s.IterReferences()
130 if err != nil {
131 return err
132 }
133
134 var refNames []string
135 err = refIter.ForEach(func(r *plumbing.Reference) error {
136 refNames = append(refNames, string(r.Name()))
137 return nil
138 })
139 if err != nil {
140 return err
141 }
142
143 sort.Strings(refNames)
144
145 var headSet bool
146 for _, refName := range refNames {
147 ref, err := s.Reference(plumbing.ReferenceName(refName))
148 if err != nil {
149 return err
150 }
151 ok, err := a.createHeadIfCorrectReference(ref, s)
152 if err != nil {
153 return err
154 }
155 if ok {
156 headSet = true
157 break
158 }
159 }
160
161 if !headSet {
162 return plumbing.ErrReferenceNotFound
163 }
164
165 return nil
166 }
167
168 func (a *AdvRefs) createHeadIfCorrectReference(
169 reference *plumbing.Reference,
170 s storer.ReferenceStorer) (bool, error) {
171 if reference.Hash() == *a.Head {
172 headRef := plumbing.NewSymbolicReference(plumbing.HEAD, reference.Name())
173 if err := s.SetReference(headRef); err != nil {
174 return false, err
175 }
176
177 return true, nil
178 }
179
180 return false, nil
181 }
182
183 func (a *AdvRefs) addSymbolicRefs(s storer.ReferenceStorer) error {
184 for _, symref := range a.Capabilities.Get(capability.SymRef) {
185 chunks := strings.Split(symref, ":")
186 if len(chunks) != 2 {
187 err := fmt.Errorf("bad number of `:` in symref value (%q)", symref)
188 return plumbing.NewUnexpectedError(err)
189 }
190 name := plumbing.ReferenceName(chunks[0])
191 target := plumbing.ReferenceName(chunks[1])
192 ref := plumbing.NewSymbolicReference(name, target)
193 if err := s.SetReference(ref); err != nil {
194 return nil
195 }
196 }
197
198 return nil
199 }
200
201 func (a *AdvRefs) supportSymrefs() bool {
202 return a.Capabilities.Supports(capability.SymRef)
203 }
204
205 // IsEmpty returns true if doesn't contain any reference.
206 func (a *AdvRefs) IsEmpty() bool {
207 return a.Head == nil &&
208 len(a.References) == 0 &&
209 len(a.Peeled) == 0 &&
210 len(a.Shallows) == 0
211 }