From df0f8b66cec4f2dcc93c52712c8527c1a99397fb Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 15 Nov 2026 09:18:13 +0000 Subject: [PATCH 1/6] acceptance: tgz git artifact in a bundle nested inside a git repo Records current behavior: the tarball is silently empty, or git archive fails, because : resolves from the repository root and git archive filters by the working directory. Co-authored-by: Isaac --- .../artifacts/tgz_git_subdir/out.test.toml | 2 ++ .../artifacts/tgz_git_subdir/output.txt | 17 +++++++++ .../tgz_git_subdir/proj/databricks.yml | 35 +++++++++++++++++++ .../artifacts/tgz_git_subdir/proj/src/main.py | 1 + .../artifacts/tgz_git_subdir/proj/src/util.py | 1 + .../bundle/artifacts/tgz_git_subdir/script | 23 ++++++++++++ .../artifacts/tgz_git_subdir/src/unrelated.py | 1 + .../bundle/artifacts/tgz_git_subdir/test.toml | 5 +++ 8 files changed, 85 insertions(+) create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/out.test.toml create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/output.txt create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/proj/databricks.yml create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/proj/src/main.py create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/proj/src/util.py create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/script create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/src/unrelated.py create mode 100644 acceptance/bundle/artifacts/tgz_git_subdir/test.toml diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/out.test.toml b/acceptance/bundle/artifacts/tgz_git_subdir/out.test.toml new file mode 100644 index 00000000000..a927a5fbc06 --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/out.test.toml @@ -0,0 +1,2 @@ +Cloud = false +EnvMatrix.DMS = ["", "true"] diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/output.txt b/acceptance/bundle/artifacts/tgz_git_subdir/output.txt new file mode 100644 index 00000000000..a05223f038c --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/output.txt @@ -0,0 +1,17 @@ + +>>> errcode [CLI] bundle deploy +Building from_git... +Building from_git_include... +Error: git archive main:src: exit status 128: fatal: pathspec 'main.py' did not match any files + + + +Exit code: 1 + +=== Contents of from_git.tgz + +=== Contents of from_git_include.tgz +(not built) + +=== Contents of from_worktree.tgz +(not built) diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/proj/databricks.yml b/acceptance/bundle/artifacts/tgz_git_subdir/proj/databricks.yml new file mode 100644 index 00000000000..2cb2be94604 --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/proj/databricks.yml @@ -0,0 +1,35 @@ +bundle: + name: test-bundle + +workspace: + artifact_path: /Workspace/foo/bar/artifacts + +sync: + exclude: + - dist/** + +# The bundle lives in a subdirectory of the git repository (as in a monorepo). +artifacts: + from_git: + type: tgz + path: src + git: + branch: main + files: + - source: dist/from_git.tgz + from_git_include: + type: tgz + path: src + include: + - main.py + git: + branch: main + files: + - source: dist/from_git_include.tgz + from_worktree: + type: tgz + path: src + include: + - . + files: + - source: dist/from_worktree.tgz diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/proj/src/main.py b/acceptance/bundle/artifacts/tgz_git_subdir/proj/src/main.py new file mode 100644 index 00000000000..f6c95a92ad7 --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/proj/src/main.py @@ -0,0 +1 @@ +print("main") diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/proj/src/util.py b/acceptance/bundle/artifacts/tgz_git_subdir/proj/src/util.py new file mode 100644 index 00000000000..78c81cd2d2f --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/proj/src/util.py @@ -0,0 +1 @@ +print("util") diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/script b/acceptance/bundle/artifacts/tgz_git_subdir/script new file mode 100644 index 00000000000..29868988636 --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/script @@ -0,0 +1,23 @@ +git init -qb main +git config core.autocrlf false +git config user.name "Tester" +git config user.email "tester@databricks.com" +git config core.hooksPath no-hooks +# A repo-root src/ unrelated to the bundle's proj/src/, as in a monorepo. +git add proj src +git commit -qm 'Add bundle' + +cd proj +trace errcode $CLI bundle deploy + +for name in from_git from_git_include from_worktree; do + title "Contents of $name.tgz" + if [ -f dist/$name.tgz ]; then + tar -tzf dist/$name.tgz | sort + else + echo "(not built)" + fi +done + +rm -rf ../.git +rm "$OUT_REQUESTS" diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/src/unrelated.py b/acceptance/bundle/artifacts/tgz_git_subdir/src/unrelated.py new file mode 100644 index 00000000000..1ff3133a150 --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/src/unrelated.py @@ -0,0 +1 @@ +print("unrelated") diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/test.toml b/acceptance/bundle/artifacts/tgz_git_subdir/test.toml new file mode 100644 index 00000000000..9f75182ac59 --- /dev/null +++ b/acceptance/bundle/artifacts/tgz_git_subdir/test.toml @@ -0,0 +1,5 @@ +RecordRequests = true +Ignore = [ + '.databricks', + 'proj/dist', +] From 4838571fa5a29331e2e0351ad5210c195bd9a4d8 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 15 Nov 2026 09:22:41 +0000 Subject: [PATCH 2/6] Fix tgz git artifacts for bundles nested inside a git repo : resolves from the repository root and git archive drops entries outside the working directory, so running git archive from the bundle sync root produced an empty tarball or failed. Run it from the worktree root with path relative to it. Co-authored-by: Isaac --- .nextchanges/bundles/tgz-git-subdir.md | 1 + .../artifacts/tgz_git_subdir/output.txt | 19 ++++++++++++------- bundle/artifacts/tarball.go | 12 ++++++++---- bundle/artifacts/tarball_test.go | 5 +++-- 4 files changed, 24 insertions(+), 13 deletions(-) create mode 100644 .nextchanges/bundles/tgz-git-subdir.md diff --git a/.nextchanges/bundles/tgz-git-subdir.md b/.nextchanges/bundles/tgz-git-subdir.md new file mode 100644 index 00000000000..e39531d9d71 --- /dev/null +++ b/.nextchanges/bundles/tgz-git-subdir.md @@ -0,0 +1 @@ +* Fix `tgz` artifacts with `git` set producing an empty tarball or failing when the bundle is in a subdirectory of the git repository. diff --git a/acceptance/bundle/artifacts/tgz_git_subdir/output.txt b/acceptance/bundle/artifacts/tgz_git_subdir/output.txt index a05223f038c..f001aaa11ec 100644 --- a/acceptance/bundle/artifacts/tgz_git_subdir/output.txt +++ b/acceptance/bundle/artifacts/tgz_git_subdir/output.txt @@ -2,16 +2,21 @@ >>> errcode [CLI] bundle deploy Building from_git... Building from_git_include... -Error: git archive main:src: exit status 128: fatal: pathspec 'main.py' did not match any files - - - -Exit code: 1 +Building from_worktree... +Uploading dist/from_git.tgz... +Uploading dist/from_git_include.tgz... +Uploading dist/from_worktree.tgz... +Uploading bundle files to /Workspace/Users/[USERNAME]/.bundle/test-bundle/default/files... +Files: 3 uploaded, 0 deleted +Resources: 0 created, 0 changed, 0 deleted, 0 unchanged === Contents of from_git.tgz +main.py +util.py === Contents of from_git_include.tgz -(not built) +main.py === Contents of from_worktree.tgz -(not built) +main.py +util.py diff --git a/bundle/artifacts/tarball.go b/bundle/artifacts/tarball.go index 85c0124220b..004f8b67554 100644 --- a/bundle/artifacts/tarball.go +++ b/bundle/artifacts/tarball.go @@ -171,18 +171,22 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w if ref == "" { return errors.New("git artifact: specify git.commit or git.branch") } - relBase, err := codeRel(b, a) + // Run git from the worktree root with `path` relative to it: : resolves + // from the repository root, and git archive drops entries outside the working + // directory, so running from a bundle nested in the repo yields a wrong or empty tree. + gitRoot := b.WorktreeRoot.Native() + relBase, err := filepath.Rel(gitRoot, a.Path) if err != nil { - return err + return fmt.Errorf("artifact path %q: %w", a.Path, err) } treeish := ref if relBase != "." { // The tree at :, so entries come out relative to `path`. - treeish = ref + ":" + relBase + treeish = ref + ":" + filepath.ToSlash(relBase) } // Ask git for an uncompressed tar and gzip it ourselves with pgzip: git's // --format=tar.gz gzip is single-threaded, whereas pgzip spreads it across cores. - args := []string{"-C", b.SyncRootPath, "archive", "--format=tar", treeish} + args := []string{"-C", gitRoot, "archive", "--format=tar", treeish} if len(a.Include) > 0 { args = append(args, "--") args = append(args, a.Include...) diff --git a/bundle/artifacts/tarball_test.go b/bundle/artifacts/tarball_test.go index 49764877ae6..a56de36e574 100644 --- a/bundle/artifacts/tarball_test.go +++ b/bundle/artifacts/tarball_test.go @@ -14,6 +14,7 @@ import ( "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" + "github.com/databricks/cli/libs/vfs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -60,7 +61,7 @@ func TestTarballFromGitArchivesRef(t *testing.T) { runGit(t, repo, "add", "-A") runGit(t, repo, "commit", "-qm", "init") - b := &bundle.Bundle{SyncRootPath: repo} + b := &bundle.Bundle{SyncRootPath: repo, WorktreeRoot: vfs.MustNew(repo)} a := &config.Artifact{Path: repo, Git: &config.ArtifactGit{Commit: "HEAD"}} var buf bytes.Buffer @@ -94,7 +95,7 @@ func TestTarballFromGitIsReproducible(t *testing.T) { runGit(t, repo, "add", "-A") runGit(t, repo, "commit", "-qm", "init") - b := &bundle.Bundle{SyncRootPath: repo} + b := &bundle.Bundle{SyncRootPath: repo, WorktreeRoot: vfs.MustNew(repo)} a := &config.Artifact{Path: repo, Git: &config.ArtifactGit{Commit: "HEAD"}} var buf1, buf2 bytes.Buffer From d7c5520b828333962a811501ea67b347c7f32df3 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 10:29:58 +0000 Subject: [PATCH 3/6] Add PR link to changelog fragment Co-authored-by: Isaac --- .nextchanges/bundles/tgz-git-subdir.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.nextchanges/bundles/tgz-git-subdir.md b/.nextchanges/bundles/tgz-git-subdir.md index e39531d9d71..c71f460c648 100644 --- a/.nextchanges/bundles/tgz-git-subdir.md +++ b/.nextchanges/bundles/tgz-git-subdir.md @@ -1 +1 @@ -* Fix `tgz` artifacts with `git` set producing an empty tarball or failing when the bundle is in a subdirectory of the git repository. +* Fix `tgz` artifacts with `git` set producing an empty tarball or failing when the bundle is in a subdirectory of the git repository. ([#7674](https://github.com/databricks/cli/pull/7674)) From 45b553be9a7a66fda286000e5b2b8381350986d6 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 13:50:00 +0000 Subject: [PATCH 4/6] Run git archive from the artifact path instead of archiving : Archiving the : tree stamped entries with the current time, so a nested bundle's tarball changed on every build. git archive run from the artifact path with the commit ref already keeps only that directory, names entries relative to it and uses the commit time, so the path arithmetic is no longer needed. Co-authored-by: Isaac --- bundle/artifacts/tarball.go | 26 +++++----------- bundle/artifacts/tarball_test.go | 52 ++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 21 deletions(-) diff --git a/bundle/artifacts/tarball.go b/bundle/artifacts/tarball.go index 004f8b67554..6cdcc7d6742 100644 --- a/bundle/artifacts/tarball.go +++ b/bundle/artifacts/tarball.go @@ -160,9 +160,8 @@ func tarballFromInclude(ctx context.Context, b *bundle.Bundle, a *config.Artifac } // tarballFromGit snapshots a git ref via `git archive`, so the archive reflects the -// committed tree at that ref. Entries are named relative to `path` (the tree at -// `:`); `include`, when set, scopes the archived pathspecs. Commit wins over -// Branch. +// committed tree at that ref. Entries are named relative to `path`; `include`, when +// set, scopes the archived pathspecs. Commit wins over Branch. func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w io.Writer) error { ref := a.Git.Commit if ref == "" { @@ -171,22 +170,13 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w if ref == "" { return errors.New("git artifact: specify git.commit or git.branch") } - // Run git from the worktree root with `path` relative to it: : resolves - // from the repository root, and git archive drops entries outside the working - // directory, so running from a bundle nested in the repo yields a wrong or empty tree. - gitRoot := b.WorktreeRoot.Native() - relBase, err := filepath.Rel(gitRoot, a.Path) - if err != nil { - return fmt.Errorf("artifact path %q: %w", a.Path, err) - } - treeish := ref - if relBase != "." { - // The tree at :, so entries come out relative to `path`. - treeish = ref + ":" + filepath.ToSlash(relBase) - } + // Run from `path`: git archive then keeps only that directory, with entries and + // include pathspecs relative to it, wherever it sits in the repository. Archiving a + // commit (not a : tree) also stamps entries with the commit time, which + // keeps the tarball reproducible. // Ask git for an uncompressed tar and gzip it ourselves with pgzip: git's // --format=tar.gz gzip is single-threaded, whereas pgzip spreads it across cores. - args := []string{"-C", gitRoot, "archive", "--format=tar", treeish} + args := []string{"-C", a.Path, "archive", "--format=tar", ref} if len(a.Include) > 0 { args = append(args, "--") args = append(args, a.Include...) @@ -201,7 +191,7 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w cmd.Stderr = &stderr if err := cmd.Run(); err != nil { gzw.Close() - return fmt.Errorf("git archive %s: %w: %s", treeish, err, stderr.String()) + return fmt.Errorf("git archive %s: %w: %s", ref, err, stderr.String()) } return gzw.Close() } diff --git a/bundle/artifacts/tarball_test.go b/bundle/artifacts/tarball_test.go index a56de36e574..474203ec507 100644 --- a/bundle/artifacts/tarball_test.go +++ b/bundle/artifacts/tarball_test.go @@ -11,10 +11,10 @@ import ( "path/filepath" "strings" "testing" + "time" "github.com/databricks/cli/bundle" "github.com/databricks/cli/bundle/config" - "github.com/databricks/cli/libs/vfs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -61,7 +61,7 @@ func TestTarballFromGitArchivesRef(t *testing.T) { runGit(t, repo, "add", "-A") runGit(t, repo, "commit", "-qm", "init") - b := &bundle.Bundle{SyncRootPath: repo, WorktreeRoot: vfs.MustNew(repo)} + b := &bundle.Bundle{SyncRootPath: repo} a := &config.Artifact{Path: repo, Git: &config.ArtifactGit{Commit: "HEAD"}} var buf bytes.Buffer @@ -95,7 +95,7 @@ func TestTarballFromGitIsReproducible(t *testing.T) { runGit(t, repo, "add", "-A") runGit(t, repo, "commit", "-qm", "init") - b := &bundle.Bundle{SyncRootPath: repo, WorktreeRoot: vfs.MustNew(repo)} + b := &bundle.Bundle{SyncRootPath: repo} a := &config.Artifact{Path: repo, Git: &config.ArtifactGit{Commit: "HEAD"}} var buf1, buf2 bytes.Buffer @@ -106,3 +106,49 @@ func TestTarballFromGitIsReproducible(t *testing.T) { // Round-trips through gzip and carries the packed file. assert.Equal(t, "print('x')", strings.TrimSpace(tarEntries(t, buf1.Bytes())["train.py"])) } + +// TestTarballFromGitSubdirIsReproducible covers a `path` below the repository root: +// entries are relative to `path` and carry the commit time, not the current time, +// so rebuilding the same commit gives the same bytes. +func TestTarballFromGitSubdirIsReproducible(t *testing.T) { + repo := t.TempDir() + runGit(t, repo, "init", "-q") + runGit(t, repo, "config", "user.email", "t@example.com") + runGit(t, repo, "config", "user.name", "t") + runGit(t, repo, "config", "core.autocrlf", "false") + src := filepath.Join(repo, "proj", "src") + require.NoError(t, os.MkdirAll(src, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(src, "main.py"), []byte("print('x')\n"), 0o644)) + require.NoError(t, os.WriteFile(filepath.Join(repo, "unrelated.py"), []byte("print('y')\n"), 0o644)) + runGit(t, repo, "add", "-A") + commitTime := time.Unix(1700000000, 0) + cmd := exec.Command("git", "commit", "-qm", "init") + cmd.Dir = repo + cmd.Env = append(os.Environ(), "GIT_COMMITTER_DATE=@1700000000 +0000", "GIT_AUTHOR_DATE=@1700000000 +0000") + out, err := cmd.CombinedOutput() + require.NoError(t, err, "git commit: %s", out) + + b := &bundle.Bundle{SyncRootPath: filepath.Join(repo, "proj")} + a := &config.Artifact{Path: src, Git: &config.ArtifactGit{Commit: "HEAD"}} + + var buf bytes.Buffer + require.NoError(t, tarballFromGit(t.Context(), b, a, &buf)) + + gzr, err := gzip.NewReader(bytes.NewReader(buf.Bytes())) + require.NoError(t, err) + tr := tar.NewReader(gzr) + var names []string + for { + hdr, err := tr.Next() + if errors.Is(err, io.EOF) { + break + } + require.NoError(t, err) + if hdr.Typeflag == tar.TypeXGlobalHeader { + continue + } + names = append(names, hdr.Name) + assert.True(t, commitTime.Equal(hdr.ModTime), "%s: mtime %v, want commit time %v", hdr.Name, hdr.ModTime, commitTime) + } + assert.Equal(t, []string{"main.py"}, names) +} From afe3b28736103dd95129f2ea2d8250fb54ea49bc Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 13:57:15 +0000 Subject: [PATCH 5/6] Fail when a tgz git artifact's path is not in the ref git archive run from a directory the ref doesn't contain (uncommitted, or added after an older ref) exits 0 with an empty archive, which was then uploaded. Check that :./ resolves from the artifact path first. Co-authored-by: Isaac --- bundle/artifacts/tarball.go | 8 +++++++- bundle/artifacts/tarball_test.go | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/bundle/artifacts/tarball.go b/bundle/artifacts/tarball.go index 6cdcc7d6742..12a9752bac2 100644 --- a/bundle/artifacts/tarball.go +++ b/bundle/artifacts/tarball.go @@ -174,6 +174,12 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w // include pathspecs relative to it, wherever it sits in the repository. Archiving a // commit (not a : tree) also stamps entries with the commit time, which // keeps the tarball reproducible. + // git archive run from a directory the ref doesn't have (uncommitted, or added + // after an older ref) succeeds with an empty archive, so check that it exists. + verify := exec.CommandContext(ctx, "git", "-C", a.Path, "rev-parse", "--verify", "--quiet", ref+":./") + if err := verify.Run(); err != nil { + return fmt.Errorf("git artifact: %s is not in the tree of %s; commit it or pick a ref that contains it", a.Path, ref) + } // Ask git for an uncompressed tar and gzip it ourselves with pgzip: git's // --format=tar.gz gzip is single-threaded, whereas pgzip spreads it across cores. args := []string{"-C", a.Path, "archive", "--format=tar", ref} @@ -191,7 +197,7 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w cmd.Stderr = &stderr if err := cmd.Run(); err != nil { gzw.Close() - return fmt.Errorf("git archive %s: %w: %s", ref, err, stderr.String()) + return fmt.Errorf("git archive %s in %s: %w: %s", ref, a.Path, err, stderr.String()) } return gzw.Close() } diff --git a/bundle/artifacts/tarball_test.go b/bundle/artifacts/tarball_test.go index 474203ec507..b079bd44bd5 100644 --- a/bundle/artifacts/tarball_test.go +++ b/bundle/artifacts/tarball_test.go @@ -152,3 +152,25 @@ func TestTarballFromGitSubdirIsReproducible(t *testing.T) { } assert.Equal(t, []string{"main.py"}, names) } + +// TestTarballFromGitRejectsPathMissingFromRef: git archive run from a directory the +// ref doesn't contain succeeds with an empty archive, which must not be uploaded. +func TestTarballFromGitRejectsPathMissingFromRef(t *testing.T) { + repo := t.TempDir() + runGit(t, repo, "init", "-q") + runGit(t, repo, "config", "user.email", "t@example.com") + runGit(t, repo, "config", "user.name", "t") + require.NoError(t, os.WriteFile(filepath.Join(repo, "README"), []byte("x\n"), 0o644)) + runGit(t, repo, "add", "-A") + runGit(t, repo, "commit", "-qm", "init") + src := filepath.Join(repo, "src") + require.NoError(t, os.MkdirAll(src, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(src, "main.py"), []byte("print('x')\n"), 0o644)) + + b := &bundle.Bundle{SyncRootPath: repo} + a := &config.Artifact{Path: src, Git: &config.ArtifactGit{Commit: "HEAD"}} + + var buf bytes.Buffer + err := tarballFromGit(t.Context(), b, a, &buf) + assert.ErrorContains(t, err, "is not in the tree of HEAD") +} From db6d4409faea860df420090fcd3c76ccbb8df4d3 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 14:01:30 +0000 Subject: [PATCH 6/6] Keep git's own error when a tgz git artifact's ref does not resolve The path check reported every failure, including an unknown ref or a bundle outside a git repository, as "not in the tree". Check the path only once the ref resolves and let git archive report the rest. Co-authored-by: Isaac --- bundle/artifacts/tarball.go | 10 ++++++++-- bundle/artifacts/tarball_test.go | 21 +++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/bundle/artifacts/tarball.go b/bundle/artifacts/tarball.go index 12a9752bac2..4fc9a6381b9 100644 --- a/bundle/artifacts/tarball.go +++ b/bundle/artifacts/tarball.go @@ -176,8 +176,9 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w // keeps the tarball reproducible. // git archive run from a directory the ref doesn't have (uncommitted, or added // after an older ref) succeeds with an empty archive, so check that it exists. - verify := exec.CommandContext(ctx, "git", "-C", a.Path, "rev-parse", "--verify", "--quiet", ref+":./") - if err := verify.Run(); err != nil { + // Only once the ref resolves: otherwise git archive below reports git's own error + // (unknown ref, not a git repository). + if gitVerify(ctx, a.Path, ref+"^{commit}") && !gitVerify(ctx, a.Path, ref+":./") { return fmt.Errorf("git artifact: %s is not in the tree of %s; commit it or pick a ref that contains it", a.Path, ref) } // Ask git for an uncompressed tar and gzip it ourselves with pgzip: git's @@ -202,6 +203,11 @@ func tarballFromGit(ctx context.Context, b *bundle.Bundle, a *config.Artifact, w return gzw.Close() } +// gitVerify reports whether rev resolves when git runs in dir. +func gitVerify(ctx context.Context, dir, rev string) bool { + return exec.CommandContext(ctx, "git", "-C", dir, "rev-parse", "--verify", "--quiet", rev).Run() == nil +} + // addFileToTarball writes f (a sync-root-relative file) to the archive under a name // relative to relBase, so entries are relative to the artifact's `path`. func addFileToTarball(tw *tar.Writer, root vfs.Path, relBase string, f fileset.File) error { diff --git a/bundle/artifacts/tarball_test.go b/bundle/artifacts/tarball_test.go index b079bd44bd5..b381d58e2ed 100644 --- a/bundle/artifacts/tarball_test.go +++ b/bundle/artifacts/tarball_test.go @@ -174,3 +174,24 @@ func TestTarballFromGitRejectsPathMissingFromRef(t *testing.T) { err := tarballFromGit(t.Context(), b, a, &buf) assert.ErrorContains(t, err, "is not in the tree of HEAD") } + +// TestTarballFromGitReportsUnknownRef: a ref that doesn't resolve keeps git's own +// error rather than the "not in the tree" one meant for a missing path. +func TestTarballFromGitReportsUnknownRef(t *testing.T) { + repo := t.TempDir() + runGit(t, repo, "init", "-q") + runGit(t, repo, "config", "user.email", "t@example.com") + runGit(t, repo, "config", "user.name", "t") + require.NoError(t, os.WriteFile(filepath.Join(repo, "README"), []byte("x\n"), 0o644)) + runGit(t, repo, "add", "-A") + runGit(t, repo, "commit", "-qm", "init") + + b := &bundle.Bundle{SyncRootPath: repo} + a := &config.Artifact{Path: repo, Git: &config.ArtifactGit{Branch: "no-such-branch"}} + + var buf bytes.Buffer + err := tarballFromGit(t.Context(), b, a, &buf) + require.Error(t, err) + assert.Contains(t, err.Error(), "git archive no-such-branch") + assert.NotContains(t, err.Error(), "is not in the tree") +}