mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
gitutils: fix checking out submodules
Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
parent
1056d554c5
commit
a602525524
2 changed files with 79 additions and 20 deletions
|
@ -29,6 +29,10 @@ func Clone(remoteURL string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return cloneGitRepo(repo)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cloneGitRepo(repo gitRepo) (string, error) {
|
||||||
fetch := fetchArgs(repo.remote, repo.ref)
|
fetch := fetchArgs(repo.remote, repo.ref)
|
||||||
|
|
||||||
root, err := ioutil.TempDir("", "docker-build-git")
|
root, err := ioutil.TempDir("", "docker-build-git")
|
||||||
|
@ -50,7 +54,19 @@ func Clone(remoteURL string) (string, error) {
|
||||||
return "", errors.Wrapf(err, "error fetching: %s", output)
|
return "", errors.Wrapf(err, "error fetching: %s", output)
|
||||||
}
|
}
|
||||||
|
|
||||||
return checkoutGit(root, repo.ref, repo.subdir)
|
root, err = checkoutGit(root, repo.ref, repo.subdir)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("git", "submodule", "update", "--init", "--recursive", "--depth=1")
|
||||||
|
cmd.Dir = root
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return "", errors.Wrapf(err, "error initializing submodules: %s", output)
|
||||||
|
}
|
||||||
|
|
||||||
|
return root, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseRemoteURL(remoteURL string) (gitRepo, error) {
|
func parseRemoteURL(remoteURL string) (gitRepo, error) {
|
||||||
|
@ -96,7 +112,7 @@ func getRefAndSubdir(fragment string) (ref string, subdir string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchArgs(remoteURL string, ref string) []string {
|
func fetchArgs(remoteURL string, ref string) []string {
|
||||||
args := []string{"fetch", "--recurse-submodules=yes"}
|
args := []string{"fetch"}
|
||||||
|
|
||||||
if supportsShallowClone(remoteURL) {
|
if supportsShallowClone(remoteURL) {
|
||||||
args = append(args, "--depth", "1")
|
args = append(args, "--depth", "1")
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -61,7 +62,7 @@ func TestCloneArgsSmartHttp(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
args := fetchArgs(serverURL.String(), "master")
|
args := fetchArgs(serverURL.String(), "master")
|
||||||
exp := []string{"fetch", "--recurse-submodules=yes", "--depth", "1", "origin", "master"}
|
exp := []string{"fetch", "--depth", "1", "origin", "master"}
|
||||||
assert.Equal(t, exp, args)
|
assert.Equal(t, exp, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,13 +78,13 @@ func TestCloneArgsDumbHttp(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
args := fetchArgs(serverURL.String(), "master")
|
args := fetchArgs(serverURL.String(), "master")
|
||||||
exp := []string{"fetch", "--recurse-submodules=yes", "origin", "master"}
|
exp := []string{"fetch", "origin", "master"}
|
||||||
assert.Equal(t, exp, args)
|
assert.Equal(t, exp, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCloneArgsGit(t *testing.T) {
|
func TestCloneArgsGit(t *testing.T) {
|
||||||
args := fetchArgs("git://github.com/docker/docker", "master")
|
args := fetchArgs("git://github.com/docker/docker", "master")
|
||||||
exp := []string{"fetch", "--recurse-submodules=yes", "--depth", "1", "origin", "master"}
|
exp := []string{"fetch", "--depth", "1", "origin", "master"}
|
||||||
assert.Equal(t, exp, args)
|
assert.Equal(t, exp, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -165,24 +166,55 @@ func TestCheckoutGit(t *testing.T) {
|
||||||
_, err = gitWithinDir(gitDir, "checkout", "master")
|
_, err = gitWithinDir(gitDir, "checkout", "master")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
// set up submodule
|
||||||
|
subrepoDir := filepath.Join(root, "subrepo")
|
||||||
|
_, err = git("init", subrepoDir)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = gitWithinDir(subrepoDir, "config", "user.email", "test@docker.com")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = gitWithinDir(subrepoDir, "config", "user.name", "Docker test")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
err = ioutil.WriteFile(filepath.Join(subrepoDir, "subfile"), []byte("subcontents"), 0644)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = gitWithinDir(subrepoDir, "add", "-A")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = gitWithinDir(subrepoDir, "commit", "-am", "Subrepo initial")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cmd := exec.Command("git", "submodule", "add", subrepoDir, "sub") // this command doesn't work with --work-tree
|
||||||
|
cmd.Dir = gitDir
|
||||||
|
require.NoError(t, cmd.Run())
|
||||||
|
|
||||||
|
_, err = gitWithinDir(gitDir, "add", "-A")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = gitWithinDir(gitDir, "commit", "-am", "With submodule")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
type singleCase struct {
|
type singleCase struct {
|
||||||
frag string
|
frag string
|
||||||
exp string
|
exp string
|
||||||
fail bool
|
fail bool
|
||||||
|
submodule bool
|
||||||
}
|
}
|
||||||
|
|
||||||
cases := []singleCase{
|
cases := []singleCase{
|
||||||
{"", "FROM scratch", false},
|
{"", "FROM scratch", false, true},
|
||||||
{"master", "FROM scratch", false},
|
{"master", "FROM scratch", false, true},
|
||||||
{":subdir", "FROM scratch" + eol + "EXPOSE 5000", false},
|
{":subdir", "FROM scratch" + eol + "EXPOSE 5000", false, false},
|
||||||
{":nosubdir", "", true}, // missing directory error
|
{":nosubdir", "", true, false}, // missing directory error
|
||||||
{":Dockerfile", "", true}, // not a directory error
|
{":Dockerfile", "", true, false}, // not a directory error
|
||||||
{"master:nosubdir", "", true},
|
{"master:nosubdir", "", true, false},
|
||||||
{"master:subdir", "FROM scratch" + eol + "EXPOSE 5000", false},
|
{"master:subdir", "FROM scratch" + eol + "EXPOSE 5000", false, false},
|
||||||
{"master:../subdir", "", true},
|
{"master:../subdir", "", true, false},
|
||||||
{"test", "FROM scratch" + eol + "EXPOSE 3000", false},
|
{"test", "FROM scratch" + eol + "EXPOSE 3000", false, false},
|
||||||
{"test:", "FROM scratch" + eol + "EXPOSE 3000", false},
|
{"test:", "FROM scratch" + eol + "EXPOSE 3000", false, false},
|
||||||
{"test:subdir", "FROM busybox" + eol + "EXPOSE 5000", false},
|
{"test:subdir", "FROM busybox" + eol + "EXPOSE 5000", false, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
if runtime.GOOS != "windows" {
|
if runtime.GOOS != "windows" {
|
||||||
|
@ -197,11 +229,22 @@ func TestCheckoutGit(t *testing.T) {
|
||||||
|
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
ref, subdir := getRefAndSubdir(c.frag)
|
ref, subdir := getRefAndSubdir(c.frag)
|
||||||
r, err := checkoutGit(gitDir, ref, subdir)
|
r, err := cloneGitRepo(gitRepo{remote: gitDir, ref: ref, subdir: subdir})
|
||||||
|
|
||||||
if c.fail {
|
if c.fail {
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
continue
|
continue
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
if c.submodule {
|
||||||
|
b, err := ioutil.ReadFile(filepath.Join(r, "sub/subfile"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "subcontents", string(b))
|
||||||
|
} else {
|
||||||
|
_, err := os.Stat(filepath.Join(r, "sub/subfile"))
|
||||||
|
require.Error(t, err)
|
||||||
|
require.True(t, os.IsNotExist(err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
|
b, err := ioutil.ReadFile(filepath.Join(r, "Dockerfile"))
|
||||||
|
|
Loading…
Add table
Reference in a new issue