mirror of
https://github.com/moby/buildkit.git
synced 2025-10-28 15:35:15 +03:00
git: fix subdir filter on submodule dir
Because subdir filter happened before the submodule update it resulted in empty directory being filtered and submodule update being skipped because .gitmodules was already missing. Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
@@ -567,6 +567,7 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
|
||||
}
|
||||
}
|
||||
|
||||
cd := checkoutDir
|
||||
if gs.src.KeepGitDir && subdir == "." {
|
||||
checkoutDirGit := filepath.Join(checkoutDir, ".git")
|
||||
if err := os.MkdirAll(checkoutDir, 0711); err != nil {
|
||||
@@ -624,7 +625,6 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
|
||||
}
|
||||
gitDir = checkoutDirGit
|
||||
} else {
|
||||
cd := checkoutDir
|
||||
if subdir != "." {
|
||||
cd, err = os.MkdirTemp(cd, "checkout")
|
||||
if err != nil {
|
||||
@@ -636,6 +636,14 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to checkout remote %s", urlutil.RedactCredentials(gs.src.Remote))
|
||||
}
|
||||
}
|
||||
|
||||
git = git.New(gitutil.WithWorkTree(cd), gitutil.WithGitDir(gitDir))
|
||||
_, err = git.Run(ctx, "submodule", "update", "--init", "--recursive", "--depth=1")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to update submodules for %s", urlutil.RedactCredentials(gs.src.Remote))
|
||||
}
|
||||
|
||||
if subdir != "." {
|
||||
d, err := os.Open(filepath.Join(cd, subdir))
|
||||
if err != nil {
|
||||
@@ -663,13 +671,6 @@ func (gs *gitSourceHandler) Snapshot(ctx context.Context, g session.Group) (out
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
git = git.New(gitutil.WithWorkTree(checkoutDir), gitutil.WithGitDir(gitDir))
|
||||
_, err = git.Run(ctx, "submodule", "update", "--init", "--recursive", "--depth=1")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to update submodules for %s", urlutil.RedactCredentials(gs.src.Remote))
|
||||
}
|
||||
|
||||
if idmap := mount.IdentityMapping(); idmap != nil {
|
||||
uid, gid := idmap.RootPair()
|
||||
|
||||
@@ -733,6 +733,57 @@ func TestCredentialRedaction(t *testing.T) {
|
||||
require.NotContains(t, err.Error(), "keepthissecret")
|
||||
}
|
||||
|
||||
func TestSubmoduleSubdir(t *testing.T) {
|
||||
testSubmoduleSubdir(t, false)
|
||||
}
|
||||
|
||||
func TestSubmoduleSubdirKeepGitDir(t *testing.T) {
|
||||
testSubmoduleSubdir(t, true)
|
||||
}
|
||||
|
||||
func testSubmoduleSubdir(t *testing.T, keepGitDir bool) {
|
||||
if runtime.GOOS == "windows" {
|
||||
t.Skip("Depends on unimplemented containerd bind-mount support on Windows")
|
||||
}
|
||||
t.Parallel()
|
||||
ctx := namespaces.WithNamespace(context.Background(), "buildkit-test")
|
||||
ctx = logProgressStreams(ctx, t)
|
||||
|
||||
gs := setupGitSource(t, t.TempDir())
|
||||
|
||||
repo := setupGitRepo(t)
|
||||
|
||||
id := &GitIdentifier{Remote: repo.mainURL, KeepGitDir: keepGitDir, Ref: "feature", Subdir: "sub"}
|
||||
|
||||
g, err := gs.Resolve(ctx, id, nil, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
key1, pin1, _, done, err := g.CacheKey(ctx, nil, 0)
|
||||
require.NoError(t, err)
|
||||
require.True(t, done)
|
||||
|
||||
expLen := 44
|
||||
require.GreaterOrEqual(t, len(key1), expLen)
|
||||
require.Equal(t, 40, len(pin1))
|
||||
|
||||
ref1, err := g.Snapshot(ctx, nil)
|
||||
require.NoError(t, err)
|
||||
defer ref1.Release(context.TODO())
|
||||
|
||||
mount, err := ref1.Mount(ctx, true, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
lm := snapshot.LocalMounter(mount)
|
||||
dir, err := lm.Mount()
|
||||
require.NoError(t, err)
|
||||
defer lm.Unmount()
|
||||
|
||||
dt, err := os.ReadFile(filepath.Join(dir, "subfile"))
|
||||
require.NoError(t, err)
|
||||
|
||||
require.Equal(t, "subcontents\n", string(dt))
|
||||
}
|
||||
|
||||
func TestSubdir(t *testing.T) {
|
||||
testSubdir(t, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user