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

git context: fix support for empty git ref with subdir #3596

Merged
merged 4 commits into from
Feb 8, 2023

Conversation

crazy-max
Copy link
Member

@crazy-max crazy-max commented Feb 8, 2023

fixes docker/buildx#1604

Git context detection in the frontend

g, err := gitutil.ParseGitRef(ref)
if err != nil {
return nil, false
}

does not match myrepo.git#:myfolder but should be supported.

Also adds more test cases to be sure we don't miss anything else.

Looking at our docs in https://docs.docker.com/build/building/context/#git-repositories

image

@dvdksn "Commit Used" for myrepo.git and myrepo.git#:myfolder should be instead default branch imo.

I also wonder if we should mutualize the logic from

func NewGitIdentifier(remoteURL string) (*GitIdentifier, error) {
repo := GitIdentifier{}
if !isGitTransport(remoteURL) {
remoteURL = "https://" + remoteURL
}
var fragment string
if sshutil.IsImplicitSSHTransport(remoteURL) {
// implicit ssh urls such as "git@.." are not actually a URL, so cannot be parsed as URL
parts := strings.SplitN(remoteURL, "#", 2)
repo.Remote = parts[0]
if len(parts) == 2 {
fragment = parts[1]
}
repo.Ref, repo.Subdir = getRefAndSubdir(fragment)
} else {
u, err := url.Parse(remoteURL)
if err != nil {
return nil, err
}
repo.Ref, repo.Subdir = getRefAndSubdir(u.Fragment)
u.Fragment = ""
repo.Remote = u.String()
}
if sd := path.Clean(repo.Subdir); sd == "/" || sd == "." {
repo.Subdir = ""
}
return &repo, nil
}
with
func ParseGitRef(ref string) (*GitRef, error) {
res := &GitRef{}
if strings.HasPrefix(ref, "github.com/") {
res.IndistinguishableFromLocal = true // Deprecated
} else {
_, proto := ParseProtocol(ref)
switch proto {
case UnknownProtocol:
return nil, errdefs.ErrInvalidArgument
}
switch proto {
case HTTPProtocol, GitProtocol:
res.UnencryptedTCP = true // Discouraged, but not deprecated
}
switch proto {
// An HTTP(S) URL is considered to be a valid git ref only when it has the ".git[...]" suffix.
case HTTPProtocol, HTTPSProtocol:
var gitURLPathWithFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`)
if !gitURLPathWithFragmentSuffix.MatchString(ref) {
return nil, errdefs.ErrInvalidArgument
}
}
}
refSplitBySharp := strings.SplitN(ref, "#", 2)
res.Remote = refSplitBySharp[0]
if len(res.Remote) == 0 {
return res, errdefs.ErrInvalidArgument
}
if len(refSplitBySharp) > 1 {
refSplitBySharpSplitByColon := strings.SplitN(refSplitBySharp[1], ":", 2)
res.Commit = refSplitBySharpSplitByColon[0]
if len(res.Commit) == 0 {
return res, errdefs.ErrInvalidArgument
}
if len(refSplitBySharpSplitByColon) > 1 {
res.SubDir = refSplitBySharpSplitByColon[1]
}
}
repoSplitBySlash := strings.Split(res.Remote, "/")
res.ShortName = strings.TrimSuffix(repoSplitBySlash[len(repoSplitBySlash)-1], ".git")
return res, nil
}

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Buildx Git is not working as intended
2 participants