Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor IsGitTransport to avoid duplication #4609

Merged
merged 1 commit into from
Feb 1, 2024
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
10 changes: 1 addition & 9 deletions source/git/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package git

import (
"path"
"strings"

"github.com/moby/buildkit/solver/llbsolver/provenance"
"github.com/moby/buildkit/source"
srctypes "github.com/moby/buildkit/source/types"
"github.com/moby/buildkit/util/gitutil"
"github.com/moby/buildkit/util/sshutil"
)

type GitIdentifier struct {
Expand All @@ -23,7 +21,7 @@ type GitIdentifier struct {
}

func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
if !isGitTransport(remoteURL) {
if !gitutil.IsGitTransport(remoteURL) {
remoteURL = "https://" + remoteURL
}
u, err := gitutil.ParseURL(remoteURL)
Expand Down Expand Up @@ -77,9 +75,3 @@ func (id *GitIdentifier) Capture(c *provenance.Capture, pin string) error {
}
return nil
}

// isGitTransport returns true if the provided str is a git transport by inspecting
// the prefix of the string for known protocols used in git.
func isGitTransport(str string) bool {
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "git://") || strings.HasPrefix(str, "ssh://") || sshutil.IsImplicitSSHTransport(str)
}
2 changes: 1 addition & 1 deletion source/git/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (gs *gitSource) Identifier(scheme, ref string, attrs map[string]string, pla
id.KeepGitDir = true
}
case pb.AttrFullRemoteURL:
if !isGitTransport(v) {
if !gitutil.IsGitTransport(v) {
v = "https://" + v
}
id.Remote = v
Expand Down
9 changes: 9 additions & 0 deletions util/gitutil/git_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ func ParseURL(remote string) (*GitURL, error) {
return nil, ErrUnknownProtocol
}

func IsGitTransport(remote string) bool {
if proto := protoRegexp.FindString(remote); proto != "" {
proto = strings.ToLower(strings.TrimSuffix(proto, "://"))
_, ok := supportedProtos[proto]
return ok
}
return sshutil.IsImplicitSSHTransport(remote)
}

func fromURL(url *url.URL) *GitURL {
withoutFragment := *url
withoutFragment.Fragment = ""
Expand Down
Loading