From bdd9a92789d4a86b20a8d3df462df373f41acf23 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Fri, 23 Oct 2015 00:35:11 +0200 Subject: new remote abstraction --- common_test.go | 9 +++++++++ remote.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ remote_test.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 common_test.go create mode 100644 remote.go create mode 100644 remote_test.go diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..3f25ad9 --- /dev/null +++ b/common_test.go @@ -0,0 +1,9 @@ +package git + +import ( + "testing" + + . "gopkg.in/check.v1" +) + +func Test(t *testing.T) { TestingT(t) } diff --git a/remote.go b/remote.go new file mode 100644 index 0000000..100bc5c --- /dev/null +++ b/remote.go @@ -0,0 +1,50 @@ +package git + +import ( + "gopkg.in/src-d/go-git.v2/clients" + "gopkg.in/src-d/go-git.v2/clients/common" +) + +type Remote struct { + Endpoint common.Endpoint + + upSrv clients.GitUploadPackService + upInfo *common.GitUploadPackInfo +} + +// NewRemote returns a new Remote, using as client http.DefaultClient +func NewRemote(url string) (*Remote, error) { + end, err := common.NewEndpoint(url) + if err != nil { + return nil, err + } + + return &Remote{ + Endpoint: end, + upSrv: clients.NewGitUploadPackService(), + }, nil +} + +// Connect with the endpoint +func (r *Remote) Connect() error { + if err := r.upSrv.Connect(r.Endpoint); err != nil { + return err + } + + var err error + if r.upInfo, err = r.upSrv.Info(); err != nil { + return err + } + + return nil +} + +// Capabilities returns the remote capabilities +func (r *Remote) Capabilities() common.Capabilities { + return r.upInfo.Capabilities +} + +// DefaultBranch retrieve the name of the remote's default branch +func (r *Remote) DefaultBranch() string { + return r.upInfo.Capabilities.SymbolicReference("HEAD") +} diff --git a/remote_test.go b/remote_test.go new file mode 100644 index 0000000..511b690 --- /dev/null +++ b/remote_test.go @@ -0,0 +1,29 @@ +package git + +import . "gopkg.in/check.v1" + +type SuiteRemote struct{} + +var _ = Suite(&SuiteRemote{}) + +const RepositoryFixture = "https://github.com/tyba/git-fixture" + +func (s *SuiteRemote) TestConnect(c *C) { + r, err := NewRemote(RepositoryFixture) + c.Assert(err, IsNil) + c.Assert(r.Connect(), IsNil) +} + +func (s *SuiteRemote) TestDefaultBranch(c *C) { + r, err := NewRemote(RepositoryFixture) + c.Assert(err, IsNil) + c.Assert(r.Connect(), IsNil) + c.Assert(r.DefaultBranch(), Equals, "refs/heads/master") +} + +func (s *SuiteRemote) TestCapabilities(c *C) { + r, err := NewRemote(RepositoryFixture) + c.Assert(err, IsNil) + c.Assert(r.Connect(), IsNil) + c.Assert(r.Capabilities().Get("agent"), HasLen, 1) +} -- cgit