-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
git: update parsing to clarify between scp-style urls
This should also resolve the ports parsing issue. Signed-off-by: Justin Chadwell <me@jedevc.com>
- Loading branch information
Showing
5 changed files
with
160 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,73 @@ | ||
package gitutil | ||
|
||
import ( | ||
"net/url" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/containerd/containerd/errdefs" | ||
"github.com/moby/buildkit/util/sshutil" | ||
) | ||
|
||
type Protocol string | ||
|
||
const ( | ||
HTTPProtocol = iota + 1 | ||
HTTPSProtocol | ||
SSHProtocol | ||
GitProtocol | ||
UnknownProtocol | ||
HTTPProtocol Protocol = "http" | ||
HTTPSProtocol Protocol = "https" | ||
SSHProtocol Protocol = "ssh" | ||
GitProtocol Protocol = "git" | ||
UnknownProtocol Protocol = "unknown" | ||
) | ||
|
||
// ParseProtocol parses a git URL and returns the remote url and protocol type | ||
func ParseProtocol(remote string) (string, int) { | ||
prefixes := map[string]int{ | ||
"http://": HTTPProtocol, | ||
"https://": HTTPSProtocol, | ||
"git://": GitProtocol, | ||
"ssh://": SSHProtocol, | ||
} | ||
protocolType := UnknownProtocol | ||
for prefix, potentialType := range prefixes { | ||
if strings.HasPrefix(remote, prefix) { | ||
remote = strings.TrimPrefix(remote, prefix) | ||
protocolType = potentialType | ||
var supportedProtos = map[Protocol]struct{}{ | ||
HTTPProtocol: {}, | ||
HTTPSProtocol: {}, | ||
SSHProtocol: {}, | ||
GitProtocol: {}, | ||
} | ||
|
||
var protoRegexp = regexp.MustCompile(`^[a-z0-9]+://`) | ||
|
||
// ParseProtocol parses a git URL and returns the protocol type, the remote, | ||
// and the path. | ||
// | ||
// ParseProtocol understands implicit ssh URLs such as "git@host:repo", and | ||
// returns the same response as if the URL were "ssh://git@host/repo". | ||
func ParseProtocol(remote string) (Protocol, string, string, error) { | ||
if proto := protoRegexp.FindString(remote); proto != "" { | ||
proto := Protocol(strings.TrimSuffix(proto, "://")) | ||
_, ok := supportedProtos[proto] | ||
if !ok { | ||
return proto, remote, "", errdefs.ErrInvalidArgument | ||
} | ||
} | ||
|
||
if protocolType == UnknownProtocol && sshutil.IsImplicitSSHTransport(remote) { | ||
protocolType = SSHProtocol | ||
} | ||
u, err := url.Parse(remote) | ||
if err != nil { | ||
return proto, remote, "", err | ||
} | ||
if Protocol(u.Scheme) != proto { | ||
// this should never happen | ||
return proto, remote, "", errdefs.ErrInvalidArgument | ||
} | ||
|
||
host := u.Host | ||
if u.User != nil { | ||
host = u.User.String() + "@" + host | ||
} | ||
|
||
// remove name from ssh | ||
if protocolType == SSHProtocol { | ||
parts := strings.SplitN(remote, "@", 2) | ||
if len(parts) == 2 { | ||
remote = parts[1] | ||
path := u.Path | ||
path = strings.TrimPrefix(path, "/") | ||
if u.Fragment != "" { | ||
path += "#" + u.Fragment | ||
} | ||
|
||
return proto, host, path, nil | ||
} | ||
|
||
if sshutil.IsImplicitSSHTransport(remote) { | ||
remote, path, _ := strings.Cut(remote, ":") | ||
return SSHProtocol, remote, path, nil | ||
} | ||
|
||
return remote, protocolType | ||
return UnknownProtocol, remote, "", errdefs.ErrInvalidArgument | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters