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

Add port to SCP Endpoints #608

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions plumbing/transport/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,22 @@ func (e urlEndpoint) Path() string {
type scpEndpoint struct {
user string
host string
port string
path string
}

func (e *scpEndpoint) Protocol() string { return "ssh" }
func (e *scpEndpoint) User() string { return e.user }
func (e *scpEndpoint) Password() string { return "" }
func (e *scpEndpoint) Host() string { return e.host }
func (e *scpEndpoint) Port() int { return 22 }
func (e *scpEndpoint) Path() string { return e.path }
func (e *scpEndpoint) Port() int {
i, err := strconv.Atoi(e.port)
if err != nil {
return 22
}
return i
}

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

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

func parseSCPLike(endpoint string) (Endpoint, bool) {
Expand All @@ -232,7 +239,8 @@ func parseSCPLike(endpoint string) (Endpoint, bool) {
return &scpEndpoint{
user: m[1],
host: m[2],
path: m[3],
port: m[3],
path: m[4],
}, true
}

Expand Down
12 changes: 12 additions & 0 deletions plumbing/transport/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ func (s *SuiteCommon) TestNewEndpointSCPLike(c *C) {
c.Assert(e.String(), Equals, "[email protected]:user/repository.git")
}

func (s *SuiteCommon) TestNewEndpointSCPLikeWithPort(c *C) {
e, err := NewEndpoint("[email protected]:9999/user/repository.git")
c.Assert(err, IsNil)
c.Assert(e.Protocol(), Equals, "ssh")
c.Assert(e.User(), Equals, "git")
c.Assert(e.Password(), Equals, "")
c.Assert(e.Host(), Equals, "github.com")
c.Assert(e.Port(), Equals, 9999)
c.Assert(e.Path(), Equals, "user/repository.git")
c.Assert(e.String(), Equals, "[email protected]:user/repository.git")
}

func (s *SuiteCommon) TestNewEndpointFileAbs(c *C) {
e, err := NewEndpoint("/foo.git")
c.Assert(err, IsNil)
Expand Down