11// Package ssh implements a ssh client for go-git.
2- //
3- // The Connect() method is not allowed in ssh, use ConnectWithAuth() instead.
42package ssh
53
64import (
@@ -10,13 +8,12 @@ import (
108 "fmt"
119 "io"
1210 "io/ioutil"
13- "net/url "
11+ "strings "
1412
1513 "gopkg.in/src-d/go-git.v4/clients/common"
1614 "gopkg.in/src-d/go-git.v4/formats/pktline"
1715
1816 "golang.org/x/crypto/ssh"
19- "gopkg.in/sourcegraph/go-vcsurl.v1"
2017)
2118
2219// New errors introduced by this package.
@@ -32,79 +29,79 @@ var (
3229
3330// GitUploadPackService holds the service information.
3431// The zero value is safe to use.
35- // TODO: remove NewGitUploadPackService().
3632type GitUploadPackService struct {
3733 connected bool
3834 endpoint common.Endpoint
39- vcs * vcsurl.RepoInfo
4035 client * ssh.Client
4136 auth AuthMethod
4237}
4338
44- // NewGitUploadPackService initialises a GitUploadPackService.
39+ // NewGitUploadPackService initialises a GitUploadPackService,
4540func NewGitUploadPackService (endpoint common.Endpoint ) common.GitUploadPackService {
4641 return & GitUploadPackService {endpoint : endpoint }
4742}
4843
49- // Connect cannot be used with SSH clients and always return
50- // ErrAuthRequired. Use ConnectWithAuth instead.
44+ // Connect connects to the SSH server, unless a AuthMethod was set with SetAuth
45+ // method, by default uses an auth method based on PublicKeysCallback, it
46+ // connects to a SSH agent, using the address stored in the SSH_AUTH_SOCK
47+ // environment var
5148func (s * GitUploadPackService ) Connect () error {
52- auth , err := NewSSHAgentAuth ()
53- if err != nil {
54- return err
55- }
56-
57- return s .ConnectWithAuth (auth )
58- }
59-
60- // ConnectWithAuth connects to ep using SSH. Authentication is handled
61- // by auth.
62- func (s * GitUploadPackService ) ConnectWithAuth (auth common.AuthMethod ) (err error ) {
6349 if s .connected {
6450 return ErrAlreadyConnected
6551 }
6652
67- s .vcs , err = vcsurl .Parse (s .endpoint .String ())
68- if err != nil {
53+ if err := s .setAuthFromEndpoint (); err != nil {
6954 return err
7055 }
7156
72- url , err := vcsToURL (s .vcs )
57+ var err error
58+ s .client , err = ssh .Dial ("tcp" , s .getHostWithPort (), s .auth .clientConfig ())
7359 if err != nil {
74- return
60+ return err
7561 }
7662
77- var ok bool
78- s .auth , ok = auth .(AuthMethod )
79- if ! ok {
80- return ErrInvalidAuthMethod
63+ s .connected = true
64+ return nil
65+ }
66+
67+ func (s * GitUploadPackService ) getHostWithPort () string {
68+ host := s .endpoint .Host
69+ if strings .Index (s .endpoint .Host , ":" ) == - 1 {
70+ host += ":22"
8171 }
8272
83- s .client , err = ssh .Dial ("tcp" , url .Host , s .auth .clientConfig ())
73+ return host
74+ }
75+
76+ func (s * GitUploadPackService ) setAuthFromEndpoint () error {
77+ var u string
78+ if info := s .endpoint .User ; info != nil {
79+ u = info .Username ()
80+ }
81+
82+ var err error
83+ s .auth , err = NewSSHAgentAuth (u )
8484 if err != nil {
8585 return err
8686 }
8787
88- s .connected = true
89- return
88+ return nil
9089}
9190
92- func vcsToURL ( vcs * vcsurl. RepoInfo ) ( u * url. URL , err error ) {
93- if vcs . VCS != vcsurl . Git {
94- return nil , ErrUnsupportedVCS
95- }
96- if vcs . RepoHost != vcsurl . GitHub {
97- return nil , ErrUnsupportedRepo
91+ // SetAuth sets the AuthMethod
92+ func ( s * GitUploadPackService ) SetAuth ( auth common. AuthMethod ) error {
93+ var ok bool
94+ s . auth , ok = auth .( AuthMethod )
95+ if ! ok {
96+ return ErrInvalidAuthMethod
9897 }
99- s := "ssh://git@" + string (vcs .RepoHost ) + ":22/" + vcs .FullName
100- u , err = url .Parse (s )
101- return
98+
99+ return nil
102100}
103101
104- // Info returns the GitUploadPackInfo of the repository.
105- // The client must be connected with the repository (using
106- // the ConnectWithAuth() method) before using this
107- // method.
102+ // Info returns the GitUploadPackInfo of the repository. The client must be
103+ // connected with the repository (using the ConnectWithAuth() method) before
104+ // using this method.
108105func (s * GitUploadPackService ) Info () (i * common.GitUploadPackInfo , err error ) {
109106 if ! s .connected {
110107 return nil , ErrNotConnected
@@ -120,7 +117,7 @@ func (s *GitUploadPackService) Info() (i *common.GitUploadPackInfo, err error) {
120117 _ = session .Close ()
121118 }()
122119
123- out , err := session .Output ("git-upload-pack " + s . vcs . FullName + ".git" )
120+ out , err := session .Output (s . getCommand () )
124121 if err != nil {
125122 return nil , err
126123 }
@@ -169,7 +166,7 @@ func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (rc io.Read
169166 return nil , err
170167 }
171168
172- if err := session .Start ("git-upload-pack " + s . vcs . FullName + ".git" ); err != nil {
169+ if err := session .Start (s . getCommand () ); err != nil {
173170 return nil , err
174171 }
175172
@@ -209,3 +206,10 @@ func (s *GitUploadPackService) Fetch(r *common.GitUploadPackRequest) (rc io.Read
209206 buf := bytes .NewBuffer (data )
210207 return ioutil .NopCloser (buf ), nil
211208}
209+
210+ func (s * GitUploadPackService ) getCommand () string {
211+ directory := s .endpoint .Path
212+ directory = directory [1 :len (directory )]
213+
214+ return fmt .Sprintf ("git-upload-pack %s" , directory )
215+ }
0 commit comments