aboutsummaryrefslogtreecommitdiffstats
path: root/clients
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2015-10-23 02:26:15 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2015-10-23 02:26:15 +0200
commite1d8866ffa78fa16d2f39b0ba5344a7269ee5371 (patch)
tree4602b0ca341187aad77048f807cde423af410554 /clients
parent2275fa7d0c75d20103f90b0e1616937d5a9fc5e6 (diff)
downloadgo-git-e1d8866ffa78fa16d2f39b0ba5344a7269ee5371.tar.gz
clients and remote: fetch implementation
Diffstat (limited to 'clients')
-rw-r--r--clients/common.go3
-rw-r--r--clients/common/common.go46
-rw-r--r--clients/common/common_test.go25
-rw-r--r--clients/http/git_upload_pack.go23
-rw-r--r--clients/http/git_upload_pack_test.go22
5 files changed, 108 insertions, 11 deletions
diff --git a/clients/common.go b/clients/common.go
index 720de86..8003d07 100644
--- a/clients/common.go
+++ b/clients/common.go
@@ -1,6 +1,8 @@
package clients
import (
+ "io"
+
"gopkg.in/src-d/go-git.v2/clients/common"
"gopkg.in/src-d/go-git.v2/clients/http"
)
@@ -8,6 +10,7 @@ import (
type GitUploadPackService interface {
Connect(url common.Endpoint) error
Info() (*common.GitUploadPackInfo, error)
+ Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error)
}
func NewGitUploadPackService() GitUploadPackService {
diff --git a/clients/common/common.go b/clients/common/common.go
index 3527d7c..c3d15c1 100644
--- a/clients/common/common.go
+++ b/clients/common/common.go
@@ -2,6 +2,7 @@ package common
import (
"fmt"
+ "io/ioutil"
"net/url"
"strings"
@@ -72,9 +73,14 @@ func (r Capabilities) SymbolicReference(sym string) string {
return ""
}
+type RemoteHead struct {
+ Id string
+ Name string
+}
+
type GitUploadPackInfo struct {
Capabilities Capabilities
- Branches map[string]string
+ Refs map[string]*RemoteHead
}
func NewGitUploadPackInfo(d *pktline.Decoder) (*GitUploadPackInfo, error) {
@@ -92,7 +98,7 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error {
return err
}
- r.Branches = map[string]string{}
+ r.Refs = map[string]*RemoteHead{}
for _, line := range lines {
if !r.isValidLine(line) {
continue
@@ -118,15 +124,41 @@ func (r *GitUploadPackInfo) isValidLine(line string) bool {
}
func (r *GitUploadPackInfo) readLine(line string) {
- commit, branch := r.getCommitAndBranch(line)
- r.Branches[branch] = commit
+ rh := r.getRemoteHead(line)
+ r.Refs[rh.Name] = rh
}
-func (r *GitUploadPackInfo) getCommitAndBranch(line string) (string, string) {
+func (r *GitUploadPackInfo) getRemoteHead(line string) *RemoteHead {
parts := strings.Split(strings.Trim(line, " \n"), " ")
if len(parts) != 2 {
- return "", ""
+ return nil
}
- return parts[0], parts[1]
+ return &RemoteHead{parts[0], parts[1]}
+}
+
+type GitUploadPackRequest struct {
+ Want []string
+ Have []string
+}
+
+func (r *GitUploadPackRequest) String() string {
+ b, _ := ioutil.ReadAll(r.Reader())
+ return string(b)
+}
+
+func (r *GitUploadPackRequest) Reader() *strings.Reader {
+ e := pktline.NewEncoder()
+ for _, want := range r.Want {
+ e.AddLine(fmt.Sprintf("want %s", want))
+ }
+
+ for _, have := range r.Have {
+ e.AddLine(fmt.Sprintf("have %s", have))
+ }
+
+ e.AddFlush()
+ e.AddLine("done")
+
+ return e.GetReader()
}
diff --git a/clients/common/common_test.go b/clients/common/common_test.go
index 0b9b60a..37d4ff9 100644
--- a/clients/common/common_test.go
+++ b/clients/common/common_test.go
@@ -1,9 +1,12 @@
package common
import (
+ "bytes"
+ "encoding/base64"
"testing"
. "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-git.v2/pktline"
)
func Test(t *testing.T) { TestingT(t) }
@@ -35,3 +38,25 @@ func (s *SuiteCommon) TestCapabilitiesSymbolicReference(c *C) {
cap := parseCapabilities(CapabilitiesFixture)
c.Assert(cap.SymbolicReference("HEAD"), Equals, "refs/heads/master")
}
+
+const GitUploadPackInfoFixture = "MDAxZSMgc2VydmljZT1naXQtdXBsb2FkLXBhY2sKMDAwMDAxMGM2ZWNmMGVmMmMyZGZmYjc5NjAzM2U1YTAyMjE5YWY4NmVjNjU4NGU1IEhFQUQAbXVsdGlfYWNrIHRoaW4tcGFjayBzaWRlLWJhbmQgc2lkZS1iYW5kLTY0ayBvZnMtZGVsdGEgc2hhbGxvdyBuby1wcm9ncmVzcyBpbmNsdWRlLXRhZyBtdWx0aV9hY2tfZGV0YWlsZWQgbm8tZG9uZSBzeW1yZWY9SEVBRDpyZWZzL2hlYWRzL21hc3RlciBhZ2VudD1naXQvMjoyLjQuOH5kYnVzc2luay1maXgtZW50ZXJwcmlzZS10b2tlbnMtY29tcGlsYXRpb24tMTE2Ny1nYzcwMDZjZgowMDNmZThkM2ZmYWI1NTI4OTVjMTliOWZjZjdhYTI2NGQyNzdjZGUzMzg4MSByZWZzL2hlYWRzL2JyYW5jaAowMDNmNmVjZjBlZjJjMmRmZmI3OTYwMzNlNWEwMjIxOWFmODZlYzY1ODRlNSByZWZzL2hlYWRzL21hc3RlcgowMDNlYjhlNDcxZjU4YmNiY2E2M2IwN2JkYTIwZTQyODE5MDQwOWMyZGI0NyByZWZzL3B1bGwvMS9oZWFkCjAwMDA="
+
+func (s *SuiteCommon) TestGitUploadPackInfo(c *C) {
+ b, _ := base64.StdEncoding.DecodeString(GitUploadPackInfoFixture)
+ info, err := NewGitUploadPackInfo(pktline.NewDecoder(bytes.NewBuffer(b)))
+ c.Assert(err, IsNil)
+
+ ref := info.Capabilities.SymbolicReference("HEAD")
+ c.Assert(ref, Equals, "refs/heads/master")
+ c.Assert(info.Refs[ref].Id, Equals, "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
+ c.Assert(info.Refs[ref].Name, Equals, "refs/heads/master")
+}
+
+func (s *SuiteCommon) TestGitUploadPackRequest(c *C) {
+ r := &GitUploadPackRequest{
+ Want: []string{"foo", "qux"},
+ Have: []string{"bar"},
+ }
+
+ c.Assert(r.String(), Equals, "000dwant foo\n000dwant qux\n000dhave bar\n00000009done\n")
+}
diff --git a/clients/http/git_upload_pack.go b/clients/http/git_upload_pack.go
index 96dbdce..b128c1c 100644
--- a/clients/http/git_upload_pack.go
+++ b/clients/http/git_upload_pack.go
@@ -1,6 +1,7 @@
package http
import (
+ "fmt"
"io"
"net/http"
"strings"
@@ -28,7 +29,8 @@ func (s *GitUploadPackService) Connect(url common.Endpoint) error {
}
func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
- res, err := s.doRequest("GET", common.GitUploadPackServiceName, nil)
+ url := fmt.Sprintf("%s/info/refs?service=%s", s.endpoint, common.GitUploadPackServiceName)
+ res, err := s.doRequest("GET", url, nil)
if err != nil {
return nil, err
}
@@ -39,13 +41,28 @@ func (s *GitUploadPackService) Info() (*common.GitUploadPackInfo, error) {
return common.NewGitUploadPackInfo(dec)
}
-func (s *GitUploadPackService) doRequest(method, service string, content *strings.Reader) (*http.Response, error) {
+func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (io.ReadCloser, error) {
+ url := fmt.Sprintf("%s/%s", s.endpoint, common.GitUploadPackServiceName)
+ res, err := s.doRequest("POST", url, r.Reader())
+ if err != nil {
+ return nil, err
+ }
+
+ h := make([]byte, 8)
+ if _, err := res.Body.Read(h); err != nil {
+ return nil, err
+ }
+
+ return res.Body, nil
+}
+
+func (s *GitUploadPackService) doRequest(method, url string, content *strings.Reader) (*http.Response, error) {
var body io.Reader
if content != nil {
body = content
}
- req, err := http.NewRequest(method, s.endpoint.Service(service), body)
+ req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
diff --git a/clients/http/git_upload_pack_test.go b/clients/http/git_upload_pack_test.go
index b478445..603cd1b 100644
--- a/clients/http/git_upload_pack_test.go
+++ b/clients/http/git_upload_pack_test.go
@@ -1,6 +1,11 @@
package http
-import . "gopkg.in/check.v1"
+import (
+ "io/ioutil"
+
+ . "gopkg.in/check.v1"
+ "gopkg.in/src-d/go-git.v2/clients/common"
+)
type SuiteRemote struct{}
@@ -30,3 +35,18 @@ func (s *SuiteRemote) TestCapabilities(c *C) {
c.Assert(err, IsNil)
c.Assert(info.Capabilities.Get("agent"), HasLen, 1)
}
+
+func (s *SuiteRemote) TestFetch(c *C) {
+ r := NewGitUploadPackService()
+ c.Assert(r.Connect(RepositoryFixture), IsNil)
+
+ reader, err := r.Fetch(&common.GitUploadPackRequest{
+ Want: []string{"6ecf0ef2c2dffb796033e5a02219af86ec6584e5"},
+ })
+
+ c.Assert(err, IsNil)
+
+ b, err := ioutil.ReadAll(reader)
+ c.Assert(err, IsNil)
+ c.Assert(b, HasLen, 85374)
+}