aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2015-10-23 00:35:11 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2015-10-23 00:35:11 +0200
commitbdd9a92789d4a86b20a8d3df462df373f41acf23 (patch)
tree02d06216eba1b85a5b3c10af0865c6f4761a6464
parentb359f11ea09e642695edcd114b463da4395b10c1 (diff)
downloadgo-git-bdd9a92789d4a86b20a8d3df462df373f41acf23.tar.gz
new remote abstraction
-rw-r--r--common_test.go9
-rw-r--r--remote.go50
-rw-r--r--remote_test.go29
3 files changed, 88 insertions, 0 deletions
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)
+}