diff options
-rw-r--r-- | client.go | 181 | ||||
-rw-r--r-- | clients/common.go | 3 | ||||
-rw-r--r-- | clients/common/common.go | 46 | ||||
-rw-r--r-- | clients/common/common_test.go | 25 | ||||
-rw-r--r-- | clients/http/git_upload_pack.go | 23 | ||||
-rw-r--r-- | clients/http/git_upload_pack_test.go | 22 | ||||
-rw-r--r-- | pktline/decoder.go | 4 | ||||
-rw-r--r-- | remote.go | 14 | ||||
-rw-r--r-- | remote_test.go | 21 |
9 files changed, 145 insertions, 194 deletions
diff --git a/client.go b/client.go deleted file mode 100644 index 8855989..0000000 --- a/client.go +++ /dev/null @@ -1,181 +0,0 @@ -package git - -import ( - "fmt" - "io" - "net/http" - "net/url" - "strings" - - "gopkg.in/src-d/go-git.v2/pktline" - - "github.com/sourcegraph/go-vcsurl" -) - -type Client struct { - url string - client *http.Client -} - -func NewClient(url string) *Client { - vcs, _ := vcsurl.Parse(url) - - return &Client{url: vcs.Link(), client: &http.Client{}} -} - -func (c *Client) Refs() (*Refs, error) { - req, _ := c.buildRequest( - "GET", - fmt.Sprintf("%s/info/refs?service=git-upload-pack", c.url), - nil, - ) - - res, err := c.client.Do(req) - if err != nil { - return nil, err - } - - if res.StatusCode >= 400 { - return nil, &ErrNotFound{c.url} - } - - defer res.Body.Close() - d := pktline.NewDecoder(res.Body) - - content, err := d.ReadAll() - if err != nil { - return nil, err - } - - return c.buildRefsFromContent(content), nil -} - -func (c *Client) buildRefsFromContent(content []string) *Refs { - refs := &Refs{branches: make(map[string]string, 0)} - for _, line := range content { - if line[0] == '#' { - continue - } - - if refs.defaultBranch == "" { - refs.defaultBranch = c.getDefaultBranchFromLine(line) - } else { - commit, branch := c.getCommitAndBranch(line) - refs.branches[branch] = commit - } - } - - return refs -} - -func (c *Client) getDefaultBranchFromLine(line string) string { - args, _ := url.ParseQuery(strings.Replace(line, " ", "&", -1)) - - link, ok := args["symref"] - if !ok { - return "" - } - - parts := strings.Split(link[0], ":") - if len(parts) != 2 || parts[0] != "HEAD" { - return "" - } - - return parts[1] -} - -func (c *Client) getCommitAndBranch(line string) (string, string) { - parts := strings.Split(strings.Trim(line, " \n"), " ") - if len(parts) != 2 { - return "", "" - } - - return parts[0], parts[1] -} - -func (c *Client) PackFile(want string) (io.ReadCloser, error) { - e := pktline.NewEncoder() - e.AddLine(fmt.Sprintf("want %s", want)) - e.AddFlush() - e.AddLine("done") - - req, err := c.buildRequest( - "POST", - fmt.Sprintf("%s/git-upload-pack", c.url), - e.GetReader(), - ) - if err != nil { - return nil, err - } - - res, err := c.client.Do(req) - 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 (c *Client) buildRequest(method, url string, content *strings.Reader) (*http.Request, error) { - var req *http.Request - var err error - if content == nil { - req, err = http.NewRequest(method, url, nil) - } else { - req, err = http.NewRequest(method, url, content) - } - - if err != nil { - return nil, err - } - - c.applyHeadersToRequest(req, content) - return req, nil -} - -func (c *Client) applyHeadersToRequest(req *http.Request, content *strings.Reader) { - req.Header.Add("User-Agent", "git/1.0") - req.Header.Add("Host", "github.com") - - if content == nil { - req.Header.Add("Accept", "*/*") - } else { - req.Header.Add("Accept", "application/x-git-upload-pack-result") - req.Header.Add("Content-Type", "application/x-git-upload-pack-request") - req.Header.Add("Content-Length", string(content.Len())) - } -} - -type Refs struct { - defaultBranch string - branches map[string]string -} - -func (r *Refs) DefaultBranch() string { - return r.defaultBranch -} - -func (r *Refs) DefaultBranchCommit() string { - return r.branches[r.defaultBranch] -} - -func (r *Refs) Branches() map[string]string { - return r.branches -} - -type ErrNotFound struct { - url string -} - -func (e *ErrNotFound) Url() string { - return e.url -} - -func (e *ErrNotFound) Error() string { - return fmt.Sprintf("Unable to find %q", e.url) -} 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) +} diff --git a/pktline/decoder.go b/pktline/decoder.go index db3fd72..76be108 100644 --- a/pktline/decoder.go +++ b/pktline/decoder.go @@ -2,6 +2,7 @@ package pktline import ( "errors" + "fmt" "io" "strconv" ) @@ -25,9 +26,10 @@ func (d *Decoder) readLine() (string, error) { if _, err := d.r.Read(raw); err != nil { return "", err } - header, err := strconv.ParseInt(string(raw), 16, 16) if err != nil { + fmt.Println(err) + return "", ErrInvalidHeader } @@ -1,6 +1,8 @@ package git import ( + "io" + "gopkg.in/src-d/go-git.v2/clients" "gopkg.in/src-d/go-git.v2/clients/common" ) @@ -48,3 +50,15 @@ func (r *Remote) Capabilities() common.Capabilities { func (r *Remote) DefaultBranch() string { return r.upInfo.Capabilities.SymbolicReference("HEAD") } + +// Fetch returns a reader using the request +func (r *Remote) Fetch(req *common.GitUploadPackRequest) (io.ReadCloser, error) { + return r.upSrv.Fetch(req) +} + +// FetchDefaultBranch returns a reader for the default branch +func (r *Remote) FetchDefaultBranch() (io.ReadCloser, error) { + return r.Fetch(&common.GitUploadPackRequest{ + Want: []string{r.upInfo.Refs[r.DefaultBranch()].Id}, + }) +} diff --git a/remote_test.go b/remote_test.go index 511b690..3426fce 100644 --- a/remote_test.go +++ b/remote_test.go @@ -1,6 +1,9 @@ package git -import . "gopkg.in/check.v1" +import ( + . "gopkg.in/check.v1" + "gopkg.in/src-d/go-git.v2/packfile" +) type SuiteRemote struct{} @@ -27,3 +30,19 @@ func (s *SuiteRemote) TestCapabilities(c *C) { c.Assert(r.Connect(), IsNil) c.Assert(r.Capabilities().Get("agent"), HasLen, 1) } + +func (s *SuiteRemote) TestFetchDefaultBranch(c *C) { + r, err := NewRemote(RepositoryFixture) + c.Assert(err, IsNil) + c.Assert(r.Connect(), IsNil) + + reader, err := r.FetchDefaultBranch() + c.Assert(err, IsNil) + + pr, err := packfile.NewPackfileReader(reader, 8<<20, nil) + c.Assert(err, IsNil) + + pf, err := pr.Read() + c.Assert(err, IsNil) + c.Assert(pf.ObjectCount, Equals, 28) +} |