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

Commit 30a99a2

Browse files
authored
Merge pull request #608 from balkian/master
Add port to SCP Endpoints
2 parents 7d1595f + 0ddf8f5 commit 30a99a2

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

plumbing/transport/common.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,22 @@ func (e urlEndpoint) Path() string {
187187
type scpEndpoint struct {
188188
user string
189189
host string
190+
port string
190191
path string
191192
}
192193

193194
func (e *scpEndpoint) Protocol() string { return "ssh" }
194195
func (e *scpEndpoint) User() string { return e.user }
195196
func (e *scpEndpoint) Password() string { return "" }
196197
func (e *scpEndpoint) Host() string { return e.host }
197-
func (e *scpEndpoint) Port() int { return 22 }
198198
func (e *scpEndpoint) Path() string { return e.path }
199+
func (e *scpEndpoint) Port() int {
200+
i, err := strconv.Atoi(e.port)
201+
if err != nil {
202+
return 22
203+
}
204+
return i
205+
}
199206

200207
func (e *scpEndpoint) String() string {
201208
var user string
@@ -220,7 +227,7 @@ func (e *fileEndpoint) String() string { return e.path }
220227

221228
var (
222229
isSchemeRegExp = regexp.MustCompile(`^[^:]+://`)
223-
scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?P<path>[^\\].*)$`)
230+
scpLikeUrlRegExp = regexp.MustCompile(`^(?:(?P<user>[^@]+)@)?(?P<host>[^:\s]+):(?:(?P<port>[0-9]{1,5})/)?(?P<path>[^\\].*)$`)
224231
)
225232

226233
func parseSCPLike(endpoint string) (Endpoint, bool) {
@@ -232,7 +239,8 @@ func parseSCPLike(endpoint string) (Endpoint, bool) {
232239
return &scpEndpoint{
233240
user: m[1],
234241
host: m[2],
235-
path: m[3],
242+
port: m[3],
243+
path: m[4],
236244
}, true
237245
}
238246

plumbing/transport/common_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ func (s *SuiteCommon) TestNewEndpointSCPLike(c *C) {
7474
c.Assert(e.String(), Equals, "[email protected]:user/repository.git")
7575
}
7676

77+
func (s *SuiteCommon) TestNewEndpointSCPLikeWithPort(c *C) {
78+
e, err := NewEndpoint("[email protected]:9999/user/repository.git")
79+
c.Assert(err, IsNil)
80+
c.Assert(e.Protocol(), Equals, "ssh")
81+
c.Assert(e.User(), Equals, "git")
82+
c.Assert(e.Password(), Equals, "")
83+
c.Assert(e.Host(), Equals, "github.com")
84+
c.Assert(e.Port(), Equals, 9999)
85+
c.Assert(e.Path(), Equals, "user/repository.git")
86+
c.Assert(e.String(), Equals, "[email protected]:user/repository.git")
87+
}
88+
7789
func (s *SuiteCommon) TestNewEndpointFileAbs(c *C) {
7890
e, err := NewEndpoint("/foo.git")
7991
c.Assert(err, IsNil)

0 commit comments

Comments
 (0)