Skip to content

Commit a1bf26d

Browse files
leodidoona-agent
andcommitted
fix: handle empty git commit in getDeterministicMtime
When building packages in test fixtures or environments without git, p.C.Git().Commit can be empty. This caused getDeterministicMtime() to fail when trying to run 'git show -s --format=%ct ""'. Solution: Return Unix epoch (0) when commit is empty, providing a deterministic fallback for test environments while maintaining deterministic behavior. This fixes CI test failures in TestBuildDockerDeps and TestBuildDocker_ExportToCache. Co-authored-by: Ona <no-reply@ona.com>
1 parent 08c2de7 commit a1bf26d

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

pkg/leeway/build.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2401,12 +2401,19 @@ func createDockerExportMetadata(wd, version string, cfg DockerPkgConfig) error {
24012401

24022402
// getDeterministicMtime returns the Unix timestamp to use for tar --mtime flag.
24032403
// It uses the same timestamp source as SBOM normalization for consistency.
2404+
// If no git commit is available (e.g., in test fixtures), returns 0 (Unix epoch).
24042405
func (p *Package) getDeterministicMtime() (int64, error) {
2405-
timestamp, err := getGitCommitTimestamp(context.Background(), p.C.Git().Commit)
2406+
commit := p.C.Git().Commit
2407+
if commit == "" {
2408+
// No git commit available (e.g., test fixtures) - use Unix epoch for determinism
2409+
return 0, nil
2410+
}
2411+
2412+
timestamp, err := getGitCommitTimestamp(context.Background(), commit)
24062413
if err != nil {
24072414
return 0, fmt.Errorf("failed to get deterministic timestamp for tar mtime (commit: %s): %w. "+
24082415
"Ensure git is available and the repository is not a shallow clone, or set SOURCE_DATE_EPOCH environment variable",
2409-
p.C.Git().Commit, err)
2416+
commit, err)
24102417
}
24112418
return timestamp.Unix(), nil
24122419
}

0 commit comments

Comments
 (0)