Skip to content

Commit

Permalink
Merge pull request #416 from tonistiigi/git-transports
Browse files Browse the repository at this point in the history
git: support all git transports
  • Loading branch information
AkihiroSuda authored May 30, 2018
2 parents cca48c0 + 1067c24 commit a3f37e2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
15 changes: 15 additions & 0 deletions client/llb/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,19 @@ type ImageInfo struct {
}

func Git(remote, ref string, opts ...GitOption) State {
url := ""

for _, prefix := range []string{
"http://", "https://", "git://", "git@",
} {
if strings.HasPrefix(remote, prefix) {
url = strings.Split(remote, "#")[0]
remote = strings.TrimPrefix(remote, prefix)
}
}

id := remote

if ref != "" {
id += "#" + ref
}
Expand All @@ -154,6 +166,9 @@ func Git(remote, ref string, opts ...GitOption) State {
if gi.KeepGitDir {
attrs[pb.AttrKeepGitDir] = "true"
}
if url != "" {
attrs[pb.AttrFullRemoteURL] = url
}
source := NewSource("git://"+id, attrs, gi.Metadata())
return NewState(source.Output())
}
Expand Down
31 changes: 22 additions & 9 deletions frontend/dockerfile/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const (
dockerignoreFilename = ".dockerignore"
buildArgPrefix = "build-arg:"
labelPrefix = "label:"
gitPrefix = "git://"
keyNoCache = "no-cache"
)

var httpPrefix = regexp.MustCompile("^https?://")
var gitUrlPathWithFragmentSuffix = regexp.MustCompile(".git(?:#.+)?$")

func Build(ctx context.Context, c client.Client) error {
opts := c.Opts()
Expand All @@ -55,12 +55,10 @@ func Build(ctx context.Context, c client.Client) error {
llb.SharedKeyHint(defaultDockerfileName),
)
var buildContext *llb.State
if strings.HasPrefix(opts[LocalNameContext], gitPrefix) {
src = parseGitSource(opts[LocalNameContext])
if st, ok := detectGitContext(opts[LocalNameContext]); ok {
src = *st
buildContext = &src
}

if httpPrefix.MatchString(opts[LocalNameContext]) {
} else if httpPrefix.MatchString(opts[LocalNameContext]) {
unpack := llb.Image(dockerfile2llb.CopyImage).Run(llb.Shlex("copy --unpack /src/context /out/"), llb.ReadonlyRootFS())
unpack.AddMount("/src", llb.HTTP(opts[LocalNameContext], llb.Filename("context")), llb.Readonly)
src = unpack.AddMount("/out", llb.Scratch())
Expand Down Expand Up @@ -196,12 +194,27 @@ func filter(opt map[string]string, key string) map[string]string {
return m
}

func parseGitSource(ref string) llb.State {
ref = strings.TrimPrefix(ref, gitPrefix)
func detectGitContext(ref string) (*llb.State, bool) {
found := false
if httpPrefix.MatchString(ref) && gitUrlPathWithFragmentSuffix.MatchString(ref) {
found = true
}

for _, prefix := range []string{"git://", "github.com/", "git@"} {
if strings.HasPrefix(ref, prefix) {
found = true
break
}
}
if !found {
return nil, false
}

parts := strings.SplitN(ref, "#", 2)
branch := ""
if len(parts) > 1 {
branch = parts[1]
}
return llb.Git(parts[0], branch)
st := llb.Git(parts[0], branch)
return &st, true
}
6 changes: 3 additions & 3 deletions frontend/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ COPY --from=build foo bar2
)
require.NoError(t, err)

server := httptest.NewServer(http.FileServer(http.Dir(filepath.Join(gitDir, ".git"))))
server := httptest.NewServer(http.FileServer(http.Dir(filepath.Join(gitDir))))
defer server.Close()

destDir, err := ioutil.TempDir("", "buildkit")
Expand All @@ -1401,7 +1401,7 @@ COPY --from=build foo bar2
_, err = c.Solve(context.TODO(), nil, client.SolveOpt{
Frontend: "dockerfile.v0",
FrontendAttrs: map[string]string{
"context": "git://" + server.URL + "/#first",
"context": server.URL + "/.git#first",
},
Exporter: client.ExporterLocal,
ExporterOutputDir: destDir,
Expand All @@ -1424,7 +1424,7 @@ COPY --from=build foo bar2
_, err = c.Solve(context.TODO(), nil, client.SolveOpt{
Frontend: "dockerfile.v0",
FrontendAttrs: map[string]string{
"context": "git://" + server.URL + "/",
"context": server.URL + "/.git",
},
Exporter: client.ExporterLocal,
ExporterOutputDir: destDir,
Expand Down
1 change: 1 addition & 0 deletions solver/pb/attr.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pb

const AttrKeepGitDir = "git.keepgitdir"
const AttrFullRemoteURL = "git.fullurl"
const AttrLocalSessionID = "local.session"
const AttrIncludePatterns = "local.includepattern"
const AttrExcludePatterns = "local.excludepatterns"
Expand Down
2 changes: 2 additions & 0 deletions source/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func FromLLB(op *pb.Op_Source) (Identifier, error) {
if v == "true" {
id.KeepGitDir = true
}
case pb.AttrFullRemoteURL:
id.Remote = v
}
}
}
Expand Down

0 comments on commit a3f37e2

Please sign in to comment.