diff options
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/protocol/packp/updreq.go | 1 | ||||
-rw-r--r-- | plumbing/transport/server/receive_pack_test.go | 5 | ||||
-rw-r--r-- | plumbing/transport/server/server.go | 75 | ||||
-rw-r--r-- | plumbing/transport/server/server_test.go | 8 | ||||
-rw-r--r-- | plumbing/transport/server/upload_pack_test.go | 17 | ||||
-rw-r--r-- | plumbing/transport/test/receive_pack.go | 4 |
6 files changed, 66 insertions, 44 deletions
diff --git a/plumbing/protocol/packp/updreq.go b/plumbing/protocol/packp/updreq.go index 0624930..b246613 100644 --- a/plumbing/protocol/packp/updreq.go +++ b/plumbing/protocol/packp/updreq.go @@ -42,6 +42,7 @@ func NewReferenceUpdateRequest() *ReferenceUpdateRequest { // - report-status // - ofs-delta // - ref-delta +// - delete-refs // It leaves up to the user to add the following capabilities later: // - atomic // - ofs-delta diff --git a/plumbing/transport/server/receive_pack_test.go b/plumbing/transport/server/receive_pack_test.go index 73ba60b..54c2fba 100644 --- a/plumbing/transport/server/receive_pack_test.go +++ b/plumbing/transport/server/receive_pack_test.go @@ -27,11 +27,6 @@ func (s *ReceivePackSuite) TearDownTest(c *C) { s.Suite.TearDownSuite(c) } -// TODO -func (s *ReceivePackSuite) TestSendPackAddDeleteReference(c *C) { - c.Skip("delete reference not supported yet") -} - // Overwritten, server returns error earlier. func (s *ReceivePackSuite) TestAdvertisedReferencesNotExists(c *C) { r, err := s.Client.NewReceivePackSession(s.NonExistentEndpoint, s.EmptyAuth) diff --git a/plumbing/transport/server/server.go b/plumbing/transport/server/server.go index 89fce5f..7c78afe 100644 --- a/plumbing/transport/server/server.go +++ b/plumbing/transport/server/server.go @@ -26,7 +26,19 @@ type server struct { // NewServer returns a transport.Transport implementing a git server, // independent of transport. Each transport must wrap this. func NewServer(loader Loader) transport.Transport { - return &server{loader, &handler{}} + return &server{ + loader, + &handler{asClient: false}, + } +} + +// NewClient returns a transport.Transport implementing a client with an +// embedded server. +func NewClient(loader Loader) transport.Transport { + return &server{ + loader, + &handler{asClient: true}, + } } func (s *server) NewUploadPackSession(ep transport.Endpoint, auth transport.AuthMethod) (transport.UploadPackSession, error) { @@ -47,24 +59,27 @@ func (s *server) NewReceivePackSession(ep transport.Endpoint, auth transport.Aut return s.handler.NewReceivePackSession(sto) } -type handler struct{} +type handler struct { + asClient bool +} func (h *handler) NewUploadPackSession(s storer.Storer) (transport.UploadPackSession, error) { return &upSession{ - session: session{storer: s}, + session: session{storer: s, asClient: h.asClient}, }, nil } func (h *handler) NewReceivePackSession(s storer.Storer) (transport.ReceivePackSession, error) { return &rpSession{ - session: session{storer: s}, + session: session{storer: s, asClient: h.asClient}, cmdStatus: map[plumbing.ReferenceName]error{}, }, nil } type session struct { - storer storer.Storer - caps *capability.List + storer storer.Storer + caps *capability.List + asClient bool } func (s *session) Close() error { @@ -107,6 +122,10 @@ func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) { return nil, err } + if s.asClient && len(ar.References) == 0 { + return nil, transport.ErrEmptyRemoteRepository + } + return ar, nil } @@ -225,31 +244,11 @@ func (s *rpSession) ReceivePack(req *packp.ReferenceUpdateRequest) (*packp.Repor return s.reportStatus(), err } - updatedRefs := s.updatedReferences(req) - - if s.caps.Supports(capability.Atomic) && s.firstErr != nil { - //TODO: add support for 'atomic' once we have reference - // transactions, currently we do not announce it. - rs := s.reportStatus() - for _, cs := range rs.CommandStatuses { - if cs.Error() == nil { - cs.Status = "" - } - } - } - - for name, ref := range updatedRefs { - //TODO: add support for 'delete-refs' once we can delete - // references, currently we do not announce it. - err := s.storer.SetReference(ref) - s.setStatus(name, err) - } - + s.updateReferences(req) return s.reportStatus(), s.firstErr } -func (s *rpSession) updatedReferences(req *packp.ReferenceUpdateRequest) map[plumbing.ReferenceName]*plumbing.Reference { - refs := map[plumbing.ReferenceName]*plumbing.Reference{} +func (s *rpSession) updateReferences(req *packp.ReferenceUpdateRequest) { for _, cmd := range req.Commands { exists, err := referenceExists(s.storer, cmd.Name) if err != nil { @@ -265,19 +264,16 @@ func (s *rpSession) updatedReferences(req *packp.ReferenceUpdateRequest) map[plu } ref := plumbing.NewHashReference(cmd.Name, cmd.New) - refs[ref.Name()] = ref + err := s.storer.SetReference(ref) + s.setStatus(cmd.Name, err) case packp.Delete: if !exists { s.setStatus(cmd.Name, ErrUpdateReference) continue } - if !s.caps.Supports(capability.DeleteRefs) { - s.setStatus(cmd.Name, fmt.Errorf("delete not supported")) - continue - } - - refs[cmd.Name] = nil + err := s.storer.RemoveReference(cmd.Name) + s.setStatus(cmd.Name, err) case packp.Update: if !exists { s.setStatus(cmd.Name, ErrUpdateReference) @@ -290,11 +286,10 @@ func (s *rpSession) updatedReferences(req *packp.ReferenceUpdateRequest) map[plu } ref := plumbing.NewHashReference(cmd.Name, cmd.New) - refs[ref.Name()] = ref + err := s.storer.SetReference(ref) + s.setStatus(cmd.Name, err) } } - - return refs } func (s *rpSession) failAtomicUpdate() (*packp.ReportStatus, error) { @@ -368,6 +363,10 @@ func (*rpSession) setSupportedCapabilities(c *capability.List) error { return err } + if err := c.Set(capability.DeleteRefs); err != nil { + return err + } + return c.Set(capability.ReportStatus) } diff --git a/plumbing/transport/server/server_test.go b/plumbing/transport/server/server_test.go index 0f7201c..7912768 100644 --- a/plumbing/transport/server/server_test.go +++ b/plumbing/transport/server/server_test.go @@ -20,12 +20,18 @@ type BaseSuite struct { loader server.MapLoader client transport.Transport clientBackup transport.Transport + asClient bool } func (s *BaseSuite) SetUpSuite(c *C) { s.Suite.SetUpSuite(c) s.loader = server.MapLoader{} - s.client = server.NewServer(s.loader) + if s.asClient { + s.client = server.NewClient(s.loader) + } else { + s.client = server.NewServer(s.loader) + } + s.clientBackup = client.Protocols["file"] client.Protocols["file"] = s.client } diff --git a/plumbing/transport/server/upload_pack_test.go b/plumbing/transport/server/upload_pack_test.go index 137f887..bd2b791 100644 --- a/plumbing/transport/server/upload_pack_test.go +++ b/plumbing/transport/server/upload_pack_test.go @@ -38,3 +38,20 @@ func (s *UploadPackSuite) TestAdvertisedReferencesNotExists(c *C) { c.Assert(err, Equals, transport.ErrRepositoryNotFound) c.Assert(r, IsNil) } + +// Tests server with `asClient = true`. This is recommended when using a server +// registered directly with `client.InstallProtocol`. +type ClientLikeUploadPackSuite struct { + UploadPackSuite +} + +var _ = Suite(&ClientLikeUploadPackSuite{}) + +func (s *ClientLikeUploadPackSuite) SetUpSuite(c *C) { + s.asClient = true + s.UploadPackSuite.SetUpSuite(c) +} + +func (s *ClientLikeUploadPackSuite) TestAdvertisedReferencesEmpty(c *C) { + s.UploadPackSuite.UploadPackSuite.TestAdvertisedReferencesEmpty(c) +} diff --git a/plumbing/transport/test/receive_pack.go b/plumbing/transport/test/receive_pack.go index bb1c58a..15172c8 100644 --- a/plumbing/transport/test/receive_pack.go +++ b/plumbing/transport/test/receive_pack.go @@ -308,6 +308,10 @@ func (s *ReceivePackSuite) testSendPackDeleteReference(c *C) { req.Capabilities.Set(capability.ReportStatus) } + if !ar.Capabilities.Supports(capability.DeleteRefs) { + c.Fatal("capability delete-refs not supported") + } + c.Assert(r.Close(), IsNil) s.receivePack(c, s.Endpoint, req, nil, false) |