Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
e.g., ```dockerfile FROM alpine ADD https://github.com/moby/buildkit.git#v0.10.1 /buildkit ``` Close issue 775 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
- Loading branch information
1 parent
9b45d45
commit 9fe2edb
Showing
6 changed files
with
331 additions
and
31 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package gitutil | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/containerd/containerd/errdefs" | ||
) | ||
|
||
// GitRef represents a git ref. | ||
// | ||
// Examples: | ||
// - "https://github.com/foo/bar.git#baz/qux:quux/quuz" is parsed into: | ||
// {Remote: "https://github.com/foo/bar.git", ShortName: "bar", Commit:"baz/qux", SubDir: "quux/quuz"}. | ||
type GitRef struct { | ||
// Remote is the remote repository path. | ||
Remote string | ||
|
||
// ShortName is the directory name of the repo. | ||
// e.g., "bar" for "https://github.com/foo/bar.git" | ||
ShortName string | ||
|
||
// Commit is a commit hash, a tag, or branch name. | ||
// Commit is optional. | ||
Commit string | ||
|
||
// SubDir is a directory path inside the repo. | ||
// SubDir is optional. | ||
SubDir string | ||
|
||
// IndistinguishableFromLocal is true for a ref that is indistinguishable from a local file path, | ||
// e.g., "github.com/foo/bar". | ||
// | ||
// Deprecated. Use a distinguishable form such as "https://github.com/foo/bar.git". | ||
// | ||
// The dockerfile frontend still accepts this form only for build contexts. | ||
IndistinguishableFromLocal bool | ||
|
||
// Unencrypted TCP is true for a ref that needs an unencrypted TCP connection, | ||
// e.g., "git://..." and "http://..." . | ||
// | ||
// Discouraged, although not deprecated. | ||
// Use an encrypted TCP connection such as "git@github.com/foo/bar.git" or "https://github.com/foo/bar.git". | ||
UnencryptedTCP bool | ||
} | ||
|
||
var httpPrefix = regexp.MustCompile(`^https?://`) | ||
var gitURLPathWithFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`) | ||
|
||
func ParseGitRef(ref string) (*GitRef, error) { | ||
res := &GitRef{} | ||
var valid bool | ||
|
||
if httpPrefix.MatchString(ref) && gitURLPathWithFragmentSuffix.MatchString(ref) { | ||
valid = true | ||
if strings.HasPrefix(ref, "http://") { | ||
res.UnencryptedTCP = true | ||
} | ||
} else { | ||
if strings.HasPrefix(ref, "git://") { | ||
valid = true | ||
res.UnencryptedTCP = true | ||
} | ||
if strings.HasPrefix(ref, "github.com/") { | ||
valid = true | ||
res.IndistinguishableFromLocal = true | ||
} | ||
if strings.HasPrefix(ref, "git@") { | ||
valid = true | ||
} | ||
} | ||
if !valid { | ||
return res, 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 | ||
} |
Oops, something went wrong.