Skip to content

Commit

Permalink
Merge pull request #4609 from jedevc/is-git-transport
Browse files Browse the repository at this point in the history
chore: refactor IsGitTransport to avoid duplication
  • Loading branch information
tonistiigi authored Feb 1, 2024
2 parents 485ffeb + 3c6f6e4 commit 08d844e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
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

0 comments on commit 08d844e

Please sign in to comment.