| 1 |
// Package server implements the git server protocol. For most use cases, the |
| 2 |
// transport-specific implementations should be used. |
| 3 |
package server |
| 4 |
|
| 5 |
import ( |
| 6 |
"context" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"io" |
| 10 |
|
| 11 |
"github.com/go-git/go-git/v5/plumbing" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp" |
| 14 |
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability" |
| 15 |
"github.com/go-git/go-git/v5/plumbing/revlist" |
| 16 |
"github.com/go-git/go-git/v5/plumbing/storer" |
| 17 |
"github.com/go-git/go-git/v5/plumbing/transport" |
| 18 |
"github.com/go-git/go-git/v5/utils/ioutil" |
| 19 |
) |
| 20 |
|
| 21 |
var DefaultServer = NewServer(DefaultLoader) |
| 22 |
|
| 23 |
type server struct { |
| 24 |
loader Loader |
| 25 |
handler *handler |
| 26 |
} |
| 27 |
|
| 28 |
// NewServer returns a transport.Transport implementing a git server, |
| 29 |
// independent of transport. Each transport must wrap this. |
| 30 |
func NewServer(loader Loader) transport.Transport { |
| 31 |
return &server{ |
| 32 |
loader, |
| 33 |
&handler{asClient: false}, |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// NewClient returns a transport.Transport implementing a client with an |
| 38 |
// embedded server. |
| 39 |
func NewClient(loader Loader) transport.Transport { |
| 40 |
return &server{ |
| 41 |
loader, |
| 42 |
&handler{asClient: true}, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func (s *server) NewUploadPackSession(ep *transport.Endpoint, auth transport.AuthMethod) (transport.UploadPackSession, error) { |
| 47 |
sto, err := s.loader.Load(ep) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
|
| 52 |
return s.handler.NewUploadPackSession(sto) |
| 53 |
} |
| 54 |
|
| 55 |
func (s *server) NewReceivePackSession(ep *transport.Endpoint, auth transport.AuthMethod) (transport.ReceivePackSession, error) { |
| 56 |
sto, err := s.loader.Load(ep) |
| 57 |
if err != nil { |
| 58 |
return nil, err |
| 59 |
} |
| 60 |
|
| 61 |
return s.handler.NewReceivePackSession(sto) |
| 62 |
} |
| 63 |
|
| 64 |
type handler struct { |
| 65 |
asClient bool |
| 66 |
} |
| 67 |
|
| 68 |
func (h *handler) NewUploadPackSession(s storer.Storer) (transport.UploadPackSession, error) { |
| 69 |
return &upSession{ |
| 70 |
session: session{storer: s, asClient: h.asClient}, |
| 71 |
}, nil |
| 72 |
} |
| 73 |
|
| 74 |
func (h *handler) NewReceivePackSession(s storer.Storer) (transport.ReceivePackSession, error) { |
| 75 |
return &rpSession{ |
| 76 |
session: session{storer: s, asClient: h.asClient}, |
| 77 |
cmdStatus: map[plumbing.ReferenceName]error{}, |
| 78 |
}, nil |
| 79 |
} |
| 80 |
|
| 81 |
type session struct { |
| 82 |
storer storer.Storer |
| 83 |
caps *capability.List |
| 84 |
asClient bool |
| 85 |
} |
| 86 |
|
| 87 |
func (s *session) Close() error { |
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
func (s *session) SetAuth(transport.AuthMethod) error { |
| 92 |
//TODO: deprecate |
| 93 |
return nil |
| 94 |
} |
| 95 |
|
| 96 |
func (s *session) checkSupportedCapabilities(cl *capability.List) error { |
| 97 |
for _, c := range cl.All() { |
| 98 |
if !s.caps.Supports(c) { |
| 99 |
return fmt.Errorf("unsupported capability: %s", c) |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
type upSession struct { |
| 107 |
session |
| 108 |
} |
| 109 |
|
| 110 |
func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) { |
| 111 |
return s.AdvertisedReferencesContext(context.TODO()) |
| 112 |
} |
| 113 |
|
| 114 |
func (s *upSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { |
| 115 |
ar := packp.NewAdvRefs() |
| 116 |
|
| 117 |
if err := s.setSupportedCapabilities(ar.Capabilities); err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
|
| 121 |
s.caps = ar.Capabilities |
| 122 |
|
| 123 |
if err := setReferences(s.storer, ar); err != nil { |
| 124 |
return nil, err |
| 125 |
} |
| 126 |
|
| 127 |
if err := setHEAD(s.storer, ar); err != nil { |
| 128 |
return nil, err |
| 129 |
} |
| 130 |
|
| 131 |
if s.asClient && len(ar.References) == 0 { |
| 132 |
return nil, transport.ErrEmptyRemoteRepository |
| 133 |
} |
| 134 |
|
| 135 |
return ar, nil |
| 136 |
} |
| 137 |
|
| 138 |
func (s *upSession) UploadPack(ctx context.Context, req *packp.UploadPackRequest) (*packp.UploadPackResponse, error) { |
| 139 |
if req.IsEmpty() { |
| 140 |
return nil, transport.ErrEmptyUploadPackRequest |
| 141 |
} |
| 142 |
|
| 143 |
if err := req.Validate(); err != nil { |
| 144 |
return nil, err |
| 145 |
} |
| 146 |
|
| 147 |
if s.caps == nil { |
| 148 |
s.caps = capability.NewList() |
| 149 |
if err := s.setSupportedCapabilities(s.caps); err != nil { |
| 150 |
return nil, err |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
if err := s.checkSupportedCapabilities(req.Capabilities); err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
|
| 158 |
s.caps = req.Capabilities |
| 159 |
|
| 160 |
if len(req.Shallows) > 0 { |
| 161 |
return nil, fmt.Errorf("shallow not supported") |
| 162 |
} |
| 163 |
|
| 164 |
objs, err := s.objectsToUpload(req) |
| 165 |
if err != nil { |
| 166 |
return nil, err |
| 167 |
} |
| 168 |
|
| 169 |
pr, pw := io.Pipe() |
| 170 |
e := packfile.NewEncoder(pw, s.storer, false) |
| 171 |
go func() { |
| 172 |
// TODO: plumb through a pack window. |
| 173 |
_, err := e.Encode(objs, 10) |
| 174 |
pw.CloseWithError(err) |
| 175 |
}() |
| 176 |
|
| 177 |
return packp.NewUploadPackResponseWithPackfile(req, |
| 178 |
ioutil.NewContextReadCloser(ctx, pr), |
| 179 |
), nil |
| 180 |
} |
| 181 |
|
| 182 |
func (s *upSession) objectsToUpload(req *packp.UploadPackRequest) ([]plumbing.Hash, error) { |
| 183 |
haves, err := revlist.Objects(s.storer, req.Haves, nil) |
| 184 |
if err != nil { |
| 185 |
return nil, err |
| 186 |
} |
| 187 |
|
| 188 |
return revlist.Objects(s.storer, req.Wants, haves) |
| 189 |
} |
| 190 |
|
| 191 |
func (*upSession) setSupportedCapabilities(c *capability.List) error { |
| 192 |
if err := c.Set(capability.Agent, capability.DefaultAgent()); err != nil { |
| 193 |
return err |
| 194 |
} |
| 195 |
|
| 196 |
if err := c.Set(capability.OFSDelta); err != nil { |
| 197 |
return err |
| 198 |
} |
| 199 |
|
| 200 |
return nil |
| 201 |
} |
| 202 |
|
| 203 |
type rpSession struct { |
| 204 |
session |
| 205 |
cmdStatus map[plumbing.ReferenceName]error |
| 206 |
firstErr error |
| 207 |
unpackErr error |
| 208 |
} |
| 209 |
|
| 210 |
func (s *rpSession) AdvertisedReferences() (*packp.AdvRefs, error) { |
| 211 |
return s.AdvertisedReferencesContext(context.TODO()) |
| 212 |
} |
| 213 |
|
| 214 |
func (s *rpSession) AdvertisedReferencesContext(ctx context.Context) (*packp.AdvRefs, error) { |
| 215 |
ar := packp.NewAdvRefs() |
| 216 |
|
| 217 |
if err := s.setSupportedCapabilities(ar.Capabilities); err != nil { |
| 218 |
return nil, err |
| 219 |
} |
| 220 |
|
| 221 |
s.caps = ar.Capabilities |
| 222 |
|
| 223 |
if err := setReferences(s.storer, ar); err != nil { |
| 224 |
return nil, err |
| 225 |
} |
| 226 |
|
| 227 |
if err := setHEAD(s.storer, ar); err != nil { |
| 228 |
return nil, err |
| 229 |
} |
| 230 |
|
| 231 |
return ar, nil |
| 232 |
} |
| 233 |
|
| 234 |
var ( |
| 235 |
ErrUpdateReference = errors.New("failed to update ref") |
| 236 |
) |
| 237 |
|
| 238 |
func (s *rpSession) ReceivePack(ctx context.Context, req *packp.ReferenceUpdateRequest) (*packp.ReportStatus, error) { |
| 239 |
if s.caps == nil { |
| 240 |
s.caps = capability.NewList() |
| 241 |
if err := s.setSupportedCapabilities(s.caps); err != nil { |
| 242 |
return nil, err |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if err := s.checkSupportedCapabilities(req.Capabilities); err != nil { |
| 247 |
return nil, err |
| 248 |
} |
| 249 |
|
| 250 |
s.caps = req.Capabilities |
| 251 |
|
| 252 |
//TODO: Implement 'atomic' update of references. |
| 253 |
|
| 254 |
if req.Packfile != nil { |
| 255 |
r := ioutil.NewContextReadCloser(ctx, req.Packfile) |
| 256 |
if err := s.writePackfile(r); err != nil { |
| 257 |
s.unpackErr = err |
| 258 |
s.firstErr = err |
| 259 |
return s.reportStatus(), err |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
s.updateReferences(req) |
| 264 |
return s.reportStatus(), s.firstErr |
| 265 |
} |
| 266 |
|
| 267 |
func (s *rpSession) updateReferences(req *packp.ReferenceUpdateRequest) { |
| 268 |
for _, cmd := range req.Commands { |
| 269 |
exists, err := referenceExists(s.storer, cmd.Name) |
| 270 |
if err != nil { |
| 271 |
s.setStatus(cmd.Name, err) |
| 272 |
continue |
| 273 |
} |
| 274 |
|
| 275 |
switch cmd.Action() { |
| 276 |
case packp.Create: |
| 277 |
if exists { |
| 278 |
s.setStatus(cmd.Name, ErrUpdateReference) |
| 279 |
continue |
| 280 |
} |
| 281 |
|
| 282 |
ref := plumbing.NewHashReference(cmd.Name, cmd.New) |
| 283 |
err := s.storer.SetReference(ref) |
| 284 |
s.setStatus(cmd.Name, err) |
| 285 |
case packp.Delete: |
| 286 |
if !exists { |
| 287 |
s.setStatus(cmd.Name, ErrUpdateReference) |
| 288 |
continue |
| 289 |
} |
| 290 |
|
| 291 |
err := s.storer.RemoveReference(cmd.Name) |
| 292 |
s.setStatus(cmd.Name, err) |
| 293 |
case packp.Update: |
| 294 |
if !exists { |
| 295 |
s.setStatus(cmd.Name, ErrUpdateReference) |
| 296 |
continue |
| 297 |
} |
| 298 |
|
| 299 |
ref := plumbing.NewHashReference(cmd.Name, cmd.New) |
| 300 |
err := s.storer.SetReference(ref) |
| 301 |
s.setStatus(cmd.Name, err) |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
func (s *rpSession) writePackfile(r io.ReadCloser) error { |
| 307 |
if r == nil { |
| 308 |
return nil |
| 309 |
} |
| 310 |
|
| 311 |
if err := packfile.UpdateObjectStorage(s.storer, r); err != nil { |
| 312 |
_ = r.Close() |
| 313 |
return err |
| 314 |
} |
| 315 |
|
| 316 |
return r.Close() |
| 317 |
} |
| 318 |
|
| 319 |
func (s *rpSession) setStatus(ref plumbing.ReferenceName, err error) { |
| 320 |
s.cmdStatus[ref] = err |
| 321 |
if s.firstErr == nil && err != nil { |
| 322 |
s.firstErr = err |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
func (s *rpSession) reportStatus() *packp.ReportStatus { |
| 327 |
if !s.caps.Supports(capability.ReportStatus) { |
| 328 |
return nil |
| 329 |
} |
| 330 |
|
| 331 |
rs := packp.NewReportStatus() |
| 332 |
rs.UnpackStatus = "ok" |
| 333 |
|
| 334 |
if s.unpackErr != nil { |
| 335 |
rs.UnpackStatus = s.unpackErr.Error() |
| 336 |
} |
| 337 |
|
| 338 |
if s.cmdStatus == nil { |
| 339 |
return rs |
| 340 |
} |
| 341 |
|
| 342 |
for ref, err := range s.cmdStatus { |
| 343 |
msg := "ok" |
| 344 |
if err != nil { |
| 345 |
msg = err.Error() |
| 346 |
} |
| 347 |
status := &packp.CommandStatus{ |
| 348 |
ReferenceName: ref, |
| 349 |
Status: msg, |
| 350 |
} |
| 351 |
rs.CommandStatuses = append(rs.CommandStatuses, status) |
| 352 |
} |
| 353 |
|
| 354 |
return rs |
| 355 |
} |
| 356 |
|
| 357 |
func (*rpSession) setSupportedCapabilities(c *capability.List) error { |
| 358 |
if err := c.Set(capability.Agent, capability.DefaultAgent()); err != nil { |
| 359 |
return err |
| 360 |
} |
| 361 |
|
| 362 |
if err := c.Set(capability.OFSDelta); err != nil { |
| 363 |
return err |
| 364 |
} |
| 365 |
|
| 366 |
if err := c.Set(capability.DeleteRefs); err != nil { |
| 367 |
return err |
| 368 |
} |
| 369 |
|
| 370 |
return c.Set(capability.ReportStatus) |
| 371 |
} |
| 372 |
|
| 373 |
func setHEAD(s storer.Storer, ar *packp.AdvRefs) error { |
| 374 |
ref, err := s.Reference(plumbing.HEAD) |
| 375 |
if err == plumbing.ErrReferenceNotFound { |
| 376 |
return nil |
| 377 |
} |
| 378 |
|
| 379 |
if err != nil { |
| 380 |
return err |
| 381 |
} |
| 382 |
|
| 383 |
if ref.Type() == plumbing.SymbolicReference { |
| 384 |
if err := ar.AddReference(ref); err != nil { |
| 385 |
return nil |
| 386 |
} |
| 387 |
|
| 388 |
ref, err = storer.ResolveReference(s, ref.Target()) |
| 389 |
if err == plumbing.ErrReferenceNotFound { |
| 390 |
return nil |
| 391 |
} |
| 392 |
|
| 393 |
if err != nil { |
| 394 |
return err |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
if ref.Type() != plumbing.HashReference { |
| 399 |
return plumbing.ErrInvalidType |
| 400 |
} |
| 401 |
|
| 402 |
h := ref.Hash() |
| 403 |
ar.Head = &h |
| 404 |
|
| 405 |
return nil |
| 406 |
} |
| 407 |
|
| 408 |
func setReferences(s storer.Storer, ar *packp.AdvRefs) error { |
| 409 |
//TODO: add peeled references. |
| 410 |
iter, err := s.IterReferences() |
| 411 |
if err != nil { |
| 412 |
return err |
| 413 |
} |
| 414 |
|
| 415 |
return iter.ForEach(func(ref *plumbing.Reference) error { |
| 416 |
if ref.Type() != plumbing.HashReference { |
| 417 |
return nil |
| 418 |
} |
| 419 |
|
| 420 |
ar.References[ref.Name().String()] = ref.Hash() |
| 421 |
return nil |
| 422 |
}) |
| 423 |
} |
| 424 |
|
| 425 |
func referenceExists(s storer.ReferenceStorer, n plumbing.ReferenceName) (bool, error) { |
| 426 |
_, err := s.Reference(n) |
| 427 |
if err == plumbing.ErrReferenceNotFound { |
| 428 |
return false, nil |
| 429 |
} |
| 430 |
|
| 431 |
return err == nil, err |
| 432 |
} |