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

Commit 376dc6a

Browse files
amlweemssmola
authored andcommitted
transport: remove SetAuth, fixes #206 (#210)
* remove SetAuth functions, implement at NewUploadPackSession/NewReceivePackSession level. * propagate transport.Auth from Fetch/Pull/Clone options to the transport API.
1 parent ce78ccb commit 376dc6a

22 files changed

+116
-127
lines changed

options.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var (
2222
type CloneOptions struct {
2323
// The (possibly remote) repository URL to clone from
2424
URL string
25-
// Auth credentials, if required, to uses with the remote repository
25+
// Auth credentials, if required, to use with the remote repository
2626
Auth transport.AuthMethod
2727
// Name of the remote to be added, by default `origin`
2828
RemoteName string
@@ -61,6 +61,8 @@ type PullOptions struct {
6161
SingleBranch bool
6262
// Limit fetching to the specified number of commits.
6363
Depth int
64+
// Auth credentials, if required, to use with the remote repository
65+
Auth transport.AuthMethod
6466
}
6567

6668
// Validate validate the fields and set the default values.
@@ -84,6 +86,8 @@ type FetchOptions struct {
8486
// Depth limit fetching to the specified number of commits from the tip of
8587
// each remote branch history.
8688
Depth int
89+
// Auth credentials, if required, to use with the remote repository
90+
Auth transport.AuthMethod
8791
}
8892

8993
// Validate validate the fields and set the default values
@@ -108,6 +112,8 @@ type PushOptions struct {
108112
// RefSpecs specify what destination ref to update with what source
109113
// object. A refspec with empty src can be used to delete a reference.
110114
RefSpecs []config.RefSpec
115+
// Auth credentials, if required, to use with the remote repository
116+
Auth transport.AuthMethod
111117
}
112118

113119
// Validate validate the fields and set the default values

plumbing/transport/client/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ type dummyClient struct {
5858
*http.Client
5959
}
6060

61-
func (*dummyClient) NewUploadPackSession(transport.Endpoint) (
61+
func (*dummyClient) NewUploadPackSession(transport.Endpoint, transport.AuthMethod) (
6262
transport.UploadPackSession, error) {
6363
return nil, nil
6464
}
6565

66-
func (*dummyClient) NewReceivePackSession(transport.Endpoint) (
66+
func (*dummyClient) NewReceivePackSession(transport.Endpoint, transport.AuthMethod) (
6767
transport.ReceivePackSession, error) {
6868
return nil, nil
6969
}

plumbing/transport/common.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var (
3030
ErrAuthorizationRequired = errors.New("authorization required")
3131
ErrEmptyUploadPackRequest = errors.New("empty git-upload-pack given")
3232
ErrInvalidAuthMethod = errors.New("invalid auth method")
33+
ErrAlreadyConnected = errors.New("session already established")
3334
)
3435

3536
const (
@@ -41,9 +42,9 @@ const (
4142
// It is implemented both by the client and the server, making this a RPC.
4243
type Transport interface {
4344
// NewUploadPackSession starts a git-upload-pack session for an endpoint.
44-
NewUploadPackSession(Endpoint) (UploadPackSession, error)
45+
NewUploadPackSession(Endpoint, AuthMethod) (UploadPackSession, error)
4546
// NewReceivePackSession starts a git-receive-pack session for an endpoint.
46-
NewReceivePackSession(Endpoint) (ReceivePackSession, error)
47+
NewReceivePackSession(Endpoint, AuthMethod) (ReceivePackSession, error)
4748
}
4849

4950
type Session interface {
@@ -52,8 +53,6 @@ type Session interface {
5253
// If the repository does not exist, returns ErrRepositoryNotFound.
5354
// If the repository exists, but is empty, returns ErrEmptyRemoteRepository.
5455
AdvertisedReferences() (*packp.AdvRefs, error)
55-
//TODO: Move to Client level.
56-
SetAuth(auth AuthMethod) error
5756
io.Closer
5857
}
5958

plumbing/transport/file/client.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@ func NewClient(uploadPackBin, receivePackBin string) transport.Transport {
2828
})
2929
}
3030

31-
func (r *runner) Command(cmd string, ep transport.Endpoint) (common.Command, error) {
31+
func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod) (common.Command, error) {
3232
switch cmd {
3333
case transport.UploadPackServiceName:
3434
cmd = r.UploadPackBin
3535
case transport.ReceivePackServiceName:
3636
cmd = r.ReceivePackBin
3737
}
38+
39+
if _, err := exec.LookPath(cmd); err != nil {
40+
return nil, err
41+
}
42+
3843
return &command{cmd: exec.Command(cmd, ep.Path)}, nil
3944
}
4045

@@ -43,14 +48,6 @@ type command struct {
4348
closed bool
4449
}
4550

46-
func (c *command) SetAuth(auth transport.AuthMethod) error {
47-
if auth != nil {
48-
return transport.ErrInvalidAuthMethod
49-
}
50-
51-
return nil
52-
}
53-
5451
func (c *command) Start() error {
5552
return c.cmd.Start()
5653
}

plumbing/transport/file/receive_pack_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (s *ReceivePackSuite) TestCommandNoOutput(c *C) {
4646
}
4747

4848
client := NewClient("true", "true")
49-
session, err := client.NewReceivePackSession(s.Endpoint)
49+
session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth)
5050
c.Assert(err, IsNil)
5151
ar, err := session.AdvertisedReferences()
5252
c.Assert(err, IsNil)
@@ -59,7 +59,7 @@ func (s *ReceivePackSuite) TestMalformedInputNoErrors(c *C) {
5959
}
6060

6161
client := NewClient("yes", "yes")
62-
session, err := client.NewReceivePackSession(s.Endpoint)
62+
session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth)
6363
c.Assert(err, IsNil)
6464
ar, err := session.AdvertisedReferences()
6565
c.Assert(err, NotNil)
@@ -69,7 +69,7 @@ func (s *ReceivePackSuite) TestMalformedInputNoErrors(c *C) {
6969
func (s *ReceivePackSuite) TestNonExistentCommand(c *C) {
7070
cmd := "/non-existent-git"
7171
client := NewClient(cmd, cmd)
72-
session, err := client.NewReceivePackSession(s.Endpoint)
72+
session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth)
7373
c.Assert(err, ErrorMatches, ".*no such file or directory.*")
7474
c.Assert(session, IsNil)
7575
}

plumbing/transport/file/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ func ServeUploadPack(path string) error {
1919
return err
2020
}
2121

22-
s, err := server.DefaultServer.NewUploadPackSession(ep)
22+
// TODO: define and implement a server-side AuthMethod
23+
s, err := server.DefaultServer.NewUploadPackSession(ep, nil)
2324
if err != nil {
2425
return fmt.Errorf("error creating session: %s", err)
2526
}
@@ -36,7 +37,8 @@ func ServeReceivePack(path string) error {
3637
return err
3738
}
3839

39-
s, err := server.DefaultServer.NewReceivePackSession(ep)
40+
// TODO: define and implement a server-side AuthMethod
41+
s, err := server.DefaultServer.NewReceivePackSession(ep, nil)
4042
if err != nil {
4143
return fmt.Errorf("error creating session: %s", err)
4244
}

plumbing/transport/file/upload_pack_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (s *UploadPackSuite) TestCommandNoOutput(c *C) {
5252
}
5353

5454
client := NewClient("true", "true")
55-
session, err := client.NewUploadPackSession(s.Endpoint)
55+
session, err := client.NewUploadPackSession(s.Endpoint, s.EmptyAuth)
5656
c.Assert(err, IsNil)
5757
ar, err := session.AdvertisedReferences()
5858
c.Assert(err, IsNil)
@@ -65,7 +65,7 @@ func (s *UploadPackSuite) TestMalformedInputNoErrors(c *C) {
6565
}
6666

6767
client := NewClient("yes", "yes")
68-
session, err := client.NewUploadPackSession(s.Endpoint)
68+
session, err := client.NewUploadPackSession(s.Endpoint, s.EmptyAuth)
6969
c.Assert(err, IsNil)
7070
ar, err := session.AdvertisedReferences()
7171
c.Assert(err, NotNil)
@@ -75,7 +75,7 @@ func (s *UploadPackSuite) TestMalformedInputNoErrors(c *C) {
7575
func (s *UploadPackSuite) TestNonExistentCommand(c *C) {
7676
cmd := "/non-existent-git"
7777
client := NewClient(cmd, cmd)
78-
session, err := client.NewUploadPackSession(s.Endpoint)
78+
session, err := client.NewUploadPackSession(s.Endpoint, s.EmptyAuth)
7979
c.Assert(err, ErrorMatches, ".*no such file or directory.*")
8080
c.Assert(session, IsNil)
8181
}

plumbing/transport/git/common.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package git
22

33
import (
4-
"errors"
54
"fmt"
65
"io"
76
"net"
@@ -13,22 +12,21 @@ import (
1312
"gopkg.in/src-d/go-git.v4/utils/ioutil"
1413
)
1514

16-
var (
17-
errAlreadyConnected = errors.New("tcp connection already connected")
18-
)
19-
2015
// DefaultClient is the default git client.
2116
var DefaultClient = common.NewClient(&runner{})
2217

2318
type runner struct{}
2419

2520
// Command returns a new Command for the given cmd in the given Endpoint
26-
func (r *runner) Command(cmd string, ep transport.Endpoint) (common.Command, error) {
21+
func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod) (common.Command, error) {
22+
// auth not allowed since git protocol doesn't support authentication
23+
if auth != nil {
24+
return nil, transport.ErrInvalidAuthMethod
25+
}
2726
c := &command{command: cmd, endpoint: ep}
2827
if err := c.connect(); err != nil {
2928
return nil, err
3029
}
31-
3230
return c, nil
3331
}
3432

@@ -39,11 +37,6 @@ type command struct {
3937
endpoint transport.Endpoint
4038
}
4139

42-
// SetAuth cannot be called since git protocol doesn't support authentication
43-
func (c *command) SetAuth(auth transport.AuthMethod) error {
44-
return transport.ErrInvalidAuthMethod
45-
}
46-
4740
// Start executes the command sending the required message to the TCP connection
4841
func (c *command) Start() error {
4942
cmd := endpointToCommand(c.command, c.endpoint)
@@ -54,7 +47,7 @@ func (c *command) Start() error {
5447

5548
func (c *command) connect() error {
5649
if c.connected {
57-
return errAlreadyConnected
50+
return transport.ErrAlreadyConnected
5851
}
5952

6053
var err error

plumbing/transport/http/common.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@ func NewClient(c *http.Client) transport.Transport {
3535
}
3636
}
3737

38-
func (c *client) NewUploadPackSession(ep transport.Endpoint) (
38+
func (c *client) NewUploadPackSession(ep transport.Endpoint, auth transport.AuthMethod) (
3939
transport.UploadPackSession, error) {
4040

41-
return newUploadPackSession(c.c, ep), nil
41+
return newUploadPackSession(c.c, ep, auth)
4242
}
4343

44-
func (c *client) NewReceivePackSession(ep transport.Endpoint) (
44+
func (c *client) NewReceivePackSession(ep transport.Endpoint, auth transport.AuthMethod) (
4545
transport.ReceivePackSession, error) {
4646

47-
return newReceivePackSession(c.c, ep), nil
47+
return newReceivePackSession(c.c, ep, auth)
4848
}
4949

5050
type session struct {
@@ -54,16 +54,6 @@ type session struct {
5454
advRefs *packp.AdvRefs
5555
}
5656

57-
func (s *session) SetAuth(auth transport.AuthMethod) error {
58-
a, ok := auth.(AuthMethod)
59-
if !ok {
60-
return transport.ErrInvalidAuthMethod
61-
}
62-
63-
s.auth = a
64-
return nil
65-
}
66-
6757
func (*session) Close() error {
6858
return nil
6959
}

plumbing/transport/http/common_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
func Test(t *testing.T) { TestingT(t) }
1414

1515
type ClientSuite struct {
16-
Endpoint transport.Endpoint
16+
Endpoint transport.Endpoint
17+
EmptyAuth transport.AuthMethod
1718
}
1819

1920
var _ = Suite(&ClientSuite{})
@@ -76,9 +77,8 @@ func (s *ClientSuite) testNewHTTPError(c *C, code int, msg string) {
7677

7778
func (s *ClientSuite) TestSetAuth(c *C) {
7879
auth := &BasicAuth{}
79-
r, err := DefaultClient.NewUploadPackSession(s.Endpoint)
80+
r, err := DefaultClient.NewUploadPackSession(s.Endpoint, auth)
8081
c.Assert(err, IsNil)
81-
r.SetAuth(auth)
8282
c.Assert(auth, Equals, r.(*upSession).auth)
8383
}
8484

@@ -88,7 +88,6 @@ func (*mockAuth) Name() string { return "" }
8888
func (*mockAuth) String() string { return "" }
8989

9090
func (s *ClientSuite) TestSetAuthWrongType(c *C) {
91-
r, err := DefaultClient.NewUploadPackSession(s.Endpoint)
92-
c.Assert(err, IsNil)
93-
c.Assert(r.SetAuth(&mockAuth{}), Equals, transport.ErrInvalidAuthMethod)
91+
_, err := DefaultClient.NewUploadPackSession(s.Endpoint, &mockAuth{})
92+
c.Assert(err, Equals, transport.ErrInvalidAuthMethod)
9493
}

0 commit comments

Comments
 (0)