Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit fcdabab

Browse files
committed
clients: new Endpoint implementation and InstallProtocol function
1 parent 998511e commit fcdabab

File tree

9 files changed

+129
-162
lines changed

9 files changed

+129
-162
lines changed

clients/common.go

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,34 @@ package clients
1313

1414
import (
1515
"fmt"
16-
"net/url"
1716

1817
"gopkg.in/src-d/go-git.v4/clients/common"
1918
"gopkg.in/src-d/go-git.v4/clients/http"
2019
"gopkg.in/src-d/go-git.v4/clients/ssh"
2120
)
2221

23-
// DefaultProtocols are the protocols supported by default.
24-
var DefaultProtocols = map[string]common.GitUploadPackService{
25-
"http": http.NewGitUploadPackService(),
26-
"https": http.NewGitUploadPackService(),
27-
"ssh": ssh.NewGitUploadPackService(),
28-
}
29-
30-
// KnownProtocols holds the current set of known protocols. Initially
31-
// it gets its contents from `DefaultProtocols`. See `InstallProtocol`
32-
// below to add or modify this variable.
33-
var KnownProtocols = make(map[string]common.GitUploadPackService, len(DefaultProtocols))
22+
type GitUploadPackServiceFactory func(common.Endpoint) common.GitUploadPackService
3423

35-
func init() {
36-
for k, v := range DefaultProtocols {
37-
InstallProtocol(k, v)
38-
}
24+
// Protocols are the protocols supported by default.
25+
var Protocols = map[string]GitUploadPackServiceFactory{
26+
"http": http.NewGitUploadPackService,
27+
"https": http.NewGitUploadPackService,
28+
"ssh": ssh.NewGitUploadPackService,
3929
}
4030

4131
// InstallProtocol adds or modifies an existing protocol.
42-
func InstallProtocol(scheme string, service common.GitUploadPackService) {
43-
if service == nil {
44-
panic("nil service")
45-
}
46-
47-
KnownProtocols[scheme] = service
32+
func InstallProtocol(scheme string, f GitUploadPackServiceFactory) {
33+
Protocols[scheme] = f
4834
}
4935

5036
// NewGitUploadPackService returns the appropriate upload pack service
5137
// among of the set of known protocols: HTTP, SSH. See `InstallProtocol`
5238
// to add or modify protocols.
53-
func NewGitUploadPackService(repoURL string) (common.GitUploadPackService, error) {
54-
u, err := url.Parse(repoURL)
55-
if err != nil {
56-
return nil, fmt.Errorf("invalid url %q", repoURL)
57-
}
58-
s, ok := KnownProtocols[u.Scheme]
39+
func NewGitUploadPackService(endpoint common.Endpoint) (common.GitUploadPackService, error) {
40+
f, ok := Protocols[endpoint.Scheme]
5941
if !ok {
60-
return nil, fmt.Errorf("unsupported scheme %q", u.Scheme)
42+
return nil, fmt.Errorf("unsupported scheme %q", endpoint.Scheme)
6143
}
6244

63-
return s, nil
45+
return f(endpoint), nil
6446
}

clients/common/common.go

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,54 @@ import (
66
"fmt"
77
"io"
88
"io/ioutil"
9+
"net/url"
910
"strings"
1011

1112
"gopkg.in/src-d/go-git.v4/core"
1213
"gopkg.in/src-d/go-git.v4/formats/pktline"
1314
"gopkg.in/src-d/go-git.v4/storage/memory"
14-
15-
"gopkg.in/sourcegraph/go-vcsurl.v1"
1615
)
1716

1817
var (
19-
NotFoundErr = errors.New("repository not found")
20-
EmptyGitUploadPackErr = errors.New("empty git-upload-pack given")
18+
ErrNotFound = errors.New("repository not found")
19+
ErrEmptyGitUploadPack = errors.New("empty git-upload-pack given")
20+
ErrInvalidAuthMethod = errors.New("invalid auth method")
2121
)
2222

2323
const GitUploadPackServiceName = "git-upload-pack"
2424

2525
type GitUploadPackService interface {
26-
Connect(url Endpoint) error
27-
ConnectWithAuth(url Endpoint, auth AuthMethod) error
26+
Connect() error
27+
ConnectWithAuth(AuthMethod) error
2828
Info() (*GitUploadPackInfo, error)
29-
Fetch(r *GitUploadPackRequest) (io.ReadCloser, error)
29+
Fetch(*GitUploadPackRequest) (io.ReadCloser, error)
3030
}
3131

3232
type AuthMethod interface {
3333
Name() string
3434
String() string
3535
}
3636

37-
type Endpoint string
37+
type Endpoint url.URL
3838

39-
func NewEndpoint(url string) (Endpoint, error) {
40-
vcs, err := vcsurl.Parse(url)
39+
func NewEndpoint(endpoint string) (Endpoint, error) {
40+
u, err := url.Parse(endpoint)
4141
if err != nil {
42-
return "", core.NewPermanentError(err)
42+
return Endpoint{}, core.NewPermanentError(err)
4343
}
4444

45-
link := vcs.Link()
46-
if !strings.HasSuffix(link, ".git") {
47-
link += ".git"
45+
if !u.IsAbs() {
46+
return Endpoint{}, core.NewPermanentError(fmt.Errorf(
47+
"invalid endpoint: %s", endpoint,
48+
))
4849
}
4950

50-
return Endpoint(link), nil
51+
return Endpoint(*u), nil
5152
}
5253

53-
func (e Endpoint) Service(name string) string {
54-
return fmt.Sprintf("%s/info/refs?service=%s", e, name)
54+
func (e *Endpoint) String() string {
55+
u := url.URL(*e)
56+
return u.String()
5557
}
5658

5759
// Capabilities contains all the server capabilities
@@ -190,7 +192,7 @@ func NewGitUploadPackInfo() *GitUploadPackInfo {
190192

191193
func (r *GitUploadPackInfo) Decode(d *pktline.Decoder) error {
192194
if err := r.read(d); err != nil {
193-
if err == EmptyGitUploadPackErr {
195+
if err == ErrEmptyGitUploadPack {
194196
return core.NewPermanentError(err)
195197
}
196198

@@ -223,7 +225,7 @@ func (r *GitUploadPackInfo) read(d *pktline.Decoder) error {
223225
}
224226

225227
if isEmpty {
226-
return EmptyGitUploadPackErr
228+
return ErrEmptyGitUploadPack
227229
}
228230

229231
return nil

clients/common/common_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,15 @@ type SuiteCommon struct{}
1717
var _ = Suite(&SuiteCommon{})
1818

1919
func (s *SuiteCommon) TestNewEndpoint(c *C) {
20-
e, err := NewEndpoint("[email protected]:user/repository.git")
20+
e, err := NewEndpoint("ssh://[email protected]/user/repository.git")
2121
c.Assert(err, IsNil)
22-
c.Assert(e, Equals, Endpoint("https://github.com/user/repository.git"))
22+
c.Assert(e.String(), Equals, "ssh://git@github.com/user/repository.git")
2323
}
2424

2525
func (s *SuiteCommon) TestNewEndpointWrongForgat(c *C) {
2626
e, err := NewEndpoint("foo")
2727
c.Assert(err, Not(IsNil))
28-
c.Assert(e, Equals, Endpoint(""))
29-
}
30-
31-
func (s *SuiteCommon) TestEndpointService(c *C) {
32-
e, _ := NewEndpoint("[email protected]:user/repository.git")
33-
c.Assert(e.Service("foo"), Equals, "https://github.com/user/repository.git/info/refs?service=foo")
28+
c.Assert(e.Host, Equals, "")
3429
}
3530

3631
const CapabilitiesFixture = "6ecf0ef2c2dffb796033e5a02219af86ec6584e5 HEADmulti_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed no-done symref=HEAD:refs/heads/master agent=git/2:2.4.8~dbussink-fix-enterprise-tokens-compilation-1167-gc7006cf"

clients/common_test.go

Lines changed: 33 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,68 @@ package clients
33
import (
44
"fmt"
55
"io"
6-
"os"
76
"testing"
87

98
"gopkg.in/src-d/go-git.v4/clients/common"
109

11-
"github.com/alcortesm/tgz"
1210
. "gopkg.in/check.v1"
1311
)
1412

1513
func Test(t *testing.T) { TestingT(t) }
1614

17-
type SuiteCommon struct {
18-
dirFixturePath string
19-
}
15+
type SuiteCommon struct{}
2016

2117
var _ = Suite(&SuiteCommon{})
2218

23-
const fixtureTGZ = "../storage/filesystem/internal/dotgit/fixtures/spinnaker-gc.tgz"
19+
func (s *SuiteCommon) TestNewGitUploadPackServiceHTTP(c *C) {
20+
e, err := common.NewEndpoint("http://github.com/src-d/go-git")
21+
c.Assert(err, IsNil)
22+
23+
output, err := NewGitUploadPackService(e)
24+
c.Assert(err, IsNil)
25+
c.Assert(typeAsString(output), Equals, "*http.GitUploadPackService")
26+
27+
e, err = common.NewEndpoint("https://github.com/src-d/go-git")
28+
c.Assert(err, IsNil)
29+
30+
output, err = NewGitUploadPackService(e)
31+
c.Assert(err, IsNil)
32+
c.Assert(typeAsString(output), Equals, "*http.GitUploadPackService")
33+
}
34+
35+
func (s *SuiteCommon) TestNewGitUploadPackServiceSSH(c *C) {
36+
e, err := common.NewEndpoint("ssh://github.com/src-d/go-git")
37+
c.Assert(err, IsNil)
2438

25-
func (s *SuiteCommon) SetUpSuite(c *C) {
26-
var err error
27-
s.dirFixturePath, err = tgz.Extract(fixtureTGZ)
39+
output, err := NewGitUploadPackService(e)
2840
c.Assert(err, IsNil)
41+
c.Assert(typeAsString(output), Equals, "*ssh.GitUploadPackService")
2942
}
3043

31-
func (s *SuiteCommon) TearDownSuite(c *C) {
32-
err := os.RemoveAll(s.dirFixturePath)
44+
func (s *SuiteCommon) TestNewGitUploadPackServiceUnknown(c *C) {
45+
e, err := common.NewEndpoint("unknown://github.com/src-d/go-git")
3346
c.Assert(err, IsNil)
47+
48+
_, err = NewGitUploadPackService(e)
49+
c.Assert(err, NotNil)
3450
}
3551

36-
func (s *SuiteCommon) TestNewGitUploadPackService(c *C) {
37-
var tests = [...]struct {
38-
input string
39-
err bool
40-
exp string
41-
}{
42-
{"://example.com", true, "<nil>"},
43-
{"badscheme://github.com/src-d/go-git", true, "<nil>"},
44-
{"http://github.com/src-d/go-git", false, "*http.GitUploadPackService"},
45-
{"https://github.com/src-d/go-git", false, "*http.GitUploadPackService"},
46-
{"ssh://github.com/src-d/go-git", false, "*ssh.GitUploadPackService"},
47-
}
48-
49-
for i, t := range tests {
50-
output, err := NewGitUploadPackService(t.input)
51-
c.Assert(err != nil, Equals, t.err,
52-
Commentf("%d) %q: wrong error value (was: %s)", i, t.input, err))
53-
c.Assert(typeAsString(output), Equals, t.exp,
54-
Commentf("%d) %q: wrong type", i, t.input))
55-
}
52+
func (s *SuiteCommon) TestInstallProtocol(c *C) {
53+
InstallProtocol("newscheme", newDummyProtocolService)
54+
c.Assert(Protocols["newscheme"], NotNil)
5655
}
5756

5857
type dummyProtocolService struct{}
5958

60-
func newDummyProtocolService() common.GitUploadPackService {
59+
func newDummyProtocolService(common.Endpoint) common.GitUploadPackService {
6160
return &dummyProtocolService{}
6261
}
6362

64-
func (s *dummyProtocolService) Connect(url common.Endpoint) error {
63+
func (s *dummyProtocolService) Connect() error {
6564
return nil
6665
}
6766

68-
func (s *dummyProtocolService) ConnectWithAuth(url common.Endpoint, auth common.AuthMethod) error {
67+
func (s *dummyProtocolService) ConnectWithAuth(auth common.AuthMethod) error {
6968
return nil
7069
}
7170

@@ -77,32 +76,6 @@ func (s *dummyProtocolService) Fetch(r *common.GitUploadPackRequest) (io.ReadClo
7776
return nil, nil
7877
}
7978

80-
func (s *SuiteCommon) TestInstallProtocol(c *C) {
81-
var tests = [...]struct {
82-
scheme string
83-
service common.GitUploadPackService
84-
panic bool
85-
}{
86-
{"panic", nil, true},
87-
{"newscheme", newDummyProtocolService(), false},
88-
{"http", newDummyProtocolService(), false},
89-
}
90-
91-
for i, t := range tests {
92-
if t.panic {
93-
c.Assert(func() { InstallProtocol(t.scheme, t.service) }, PanicMatches, `nil service`)
94-
continue
95-
}
96-
97-
InstallProtocol(t.scheme, t.service)
98-
c.Assert(typeAsString(KnownProtocols[t.scheme]), Equals, typeAsString(t.service), Commentf("%d) wrong service", i))
99-
// reset to default protocols after installing
100-
if v, ok := DefaultProtocols[t.scheme]; ok {
101-
InstallProtocol(t.scheme, v)
102-
}
103-
}
104-
}
105-
10679
func typeAsString(v interface{}) string {
10780
return fmt.Sprintf("%T", v)
10881
}

clients/http/common.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@
22
package http
33

44
import (
5-
"errors"
65
"fmt"
76
"net/http"
87

98
"gopkg.in/src-d/go-git.v4/clients/common"
109
"gopkg.in/src-d/go-git.v4/core"
1110
)
1211

13-
var InvalidAuthMethodErr = errors.New("invalid http auth method: a http.HTTPAuthMethod should be provided.")
14-
1512
type HTTPAuthMethod interface {
1613
common.AuthMethod
1714
setAuth(r *http.Request)
@@ -53,7 +50,7 @@ func NewHTTPError(r *http.Response) error {
5350

5451
err := &HTTPError{r}
5552
if r.StatusCode == 404 || r.StatusCode == 401 {
56-
return core.NewPermanentError(common.NotFoundErr)
53+
return core.NewPermanentError(common.ErrNotFound)
5754
}
5855

5956
return core.NewUnexpectedError(err)

clients/http/common_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ type SuiteCommon struct{}
1313

1414
var _ = Suite(&SuiteCommon{})
1515

16+
func (s *SuiteCommon) TestNewBasicAuth(c *C) {
17+
a := NewBasicAuth("foo", "qux")
18+
19+
c.Assert(a.Name(), Equals, "http-basic-auth")
20+
c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")
21+
}
22+
1623
func (s *SuiteCommon) TestNewHTTPError200(c *C) {
1724
res := &http.Response{StatusCode: 200}
1825
res.StatusCode = 200

0 commit comments

Comments
 (0)