1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package receivepack
import (
"fmt"
"slices"
"strings"
objectid "codeberg.org/lindenii/furgit/object/id"
)
// Capabilities describes one receive-pack capability set.
type Capabilities struct {
ReportStatus bool
ReportStatusV2 bool
DeleteRefs bool
SideBand64K bool
Quiet bool
Atomic bool
OfsDelta bool
PushOptions bool
PushCertNonce string
ObjectFormat objectid.Algorithm
SessionID string
Agent string
}
// Normalize returns one normalized copy of caps.
func (caps Capabilities) Normalize(defaultAlgorithm objectid.Algorithm) Capabilities {
if caps.ObjectFormat == objectid.AlgorithmUnknown {
caps.ObjectFormat = defaultAlgorithm
}
return caps
}
// Tokens returns capabilities in Git advertisement order.
func (caps Capabilities) Tokens(defaultAlgorithm objectid.Algorithm) []string {
caps = caps.Normalize(defaultAlgorithm)
tokens := make([]string, 0, 11)
if caps.ReportStatus {
tokens = append(tokens, "report-status")
}
if caps.ReportStatusV2 {
tokens = append(tokens, "report-status-v2")
}
if caps.DeleteRefs {
tokens = append(tokens, "delete-refs")
}
if caps.SideBand64K {
tokens = append(tokens, "side-band-64k")
}
if caps.Quiet {
tokens = append(tokens, "quiet")
}
if caps.Atomic {
tokens = append(tokens, "atomic")
}
if caps.OfsDelta {
tokens = append(tokens, "ofs-delta")
}
if caps.PushCertNonce != "" {
tokens = append(tokens, "push-cert="+caps.PushCertNonce)
}
if caps.PushOptions {
tokens = append(tokens, "push-options")
}
if caps.SessionID != "" {
tokens = append(tokens, "session-id="+caps.SessionID)
}
if caps.ObjectFormat != objectid.AlgorithmUnknown {
tokens = append(tokens, "object-format="+caps.ObjectFormat.String())
}
if caps.Agent != "" {
tokens = append(tokens, "agent="+caps.Agent)
}
return tokens
}
func (caps Capabilities) supportsToken(token string, defaultAlgorithm objectid.Algorithm) bool {
name, value, _ := strings.Cut(token, "=")
switch name {
case "report-status":
return caps.ReportStatus && value == ""
case "report-status-v2":
return caps.ReportStatusV2 && value == ""
case "delete-refs":
return caps.DeleteRefs && value == ""
case "side-band-64k":
return caps.SideBand64K && value == ""
case "quiet":
return caps.Quiet && value == ""
case "atomic":
return caps.Atomic && value == ""
case "ofs-delta":
return caps.OfsDelta && value == ""
case "push-options":
return caps.PushOptions && value == ""
case "push-cert":
return caps.PushCertNonce != "" && value != ""
case "object-format":
if value == "" {
return false
}
algo, ok := objectid.ParseAlgorithm(value)
return ok && algo == caps.Normalize(defaultAlgorithm).ObjectFormat
case "session-id":
return caps.SessionID != "" && value != ""
case "agent":
return caps.Agent != "" && value != ""
default:
return false
}
}
func parseCapabilityList(s string) ([]string, error) {
s = strings.TrimSuffix(s, "\n")
if s == "" {
return nil, nil
}
tokens := strings.Fields(s)
if slices.Contains(tokens, "") {
return nil, &ProtocolError{Reason: "empty capability token"}
}
return tokens, nil
}
func parseRequestedCapabilities(
tokens []string,
supported Capabilities,
defaultAlgorithm objectid.Algorithm,
) (Capabilities, error) {
var requested Capabilities
requested.ObjectFormat = defaultAlgorithm
for _, token := range tokens {
if !supported.supportsToken(token, defaultAlgorithm) {
return Capabilities{}, &ProtocolError{
Reason: fmt.Sprintf("unsupported capability %q", token),
}
}
name, value, _ := strings.Cut(token, "=")
switch name {
case "report-status":
requested.ReportStatus = true
case "report-status-v2":
requested.ReportStatusV2 = true
case "delete-refs":
requested.DeleteRefs = true
case "side-band-64k":
requested.SideBand64K = true
case "quiet":
requested.Quiet = true
case "atomic":
requested.Atomic = true
case "ofs-delta":
requested.OfsDelta = true
case "push-options":
requested.PushOptions = true
case "push-cert":
requested.PushCertNonce = value
case "object-format":
algo, _ := objectid.ParseAlgorithm(value)
requested.ObjectFormat = algo
case "session-id":
requested.SessionID = value
case "agent":
requested.Agent = value
}
}
return requested, nil
}
|