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
65f4948
commit c12674f
Showing
6 changed files
with
168 additions
and
32 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,48 @@ | ||
package gitutil | ||
|
||
import ( | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/containerd/containerd/errdefs" | ||
) | ||
|
||
type GitRef struct { | ||
Repo string | ||
Commit string // Optional | ||
Dir string | ||
} | ||
|
||
var httpPrefix = regexp.MustCompile(`^https?://`) | ||
var gitURLPathWithFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`) | ||
|
||
func IsGitRef(ref string) bool { | ||
if httpPrefix.MatchString(ref) && gitURLPathWithFragmentSuffix.MatchString(ref) { | ||
return true | ||
} | ||
for _, prefix := range []string{"git://", "github.com/", "git@"} { | ||
if strings.HasPrefix(ref, prefix) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func ParseGitRef(ref string) (*GitRef, error) { | ||
if !IsGitRef(ref) { | ||
return nil, errdefs.ErrInvalidArgument | ||
} | ||
refSplitBySharp := strings.SplitN(ref, "#", 2) | ||
repo := refSplitBySharp[0] | ||
commit := "" | ||
if len(refSplitBySharp) > 1 { | ||
commit = refSplitBySharp[1] | ||
} | ||
repoSplitBySlash := strings.Split(ref, "/") | ||
dir := strings.TrimSuffix(repoSplitBySlash[len(repoSplitBySlash)-1], ".git") | ||
return &GitRef{ | ||
Repo: repo, | ||
Commit: commit, | ||
Dir: dir, | ||
}, nil | ||
} |
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,26 @@ | ||
package gitutil | ||
|
||
import "testing" | ||
|
||
func TestIsGitRef(t *testing.T) { | ||
cases := map[string]bool{ | ||
"https://example.com/": false, | ||
"https://example.com/foo": false, | ||
"https://example.com/foo.git": true, | ||
"https://example.com/foo.git#deadbeef": true, | ||
"https://example.com/foo.git/": false, | ||
"https://example.com/foo.git.bar": false, | ||
"git://example.com/foo": true, | ||
"github.com/moby/buildkit": true, | ||
"https://github.com/moby/buildkit": false, | ||
"https://github.com/moby/buildkit.git": true, | ||
"git@github.com/moby/buildkit": true, | ||
"git@github.com/moby/buildkit.git": true, | ||
} | ||
for ref, expected := range cases { | ||
got := IsGitRef(ref) | ||
if got != expected { | ||
t.Errorf("expected IsGitRef(%q) to be %v, got %v", ref, expected, got) | ||
} | ||
} | ||
} |