sketch: create empty commit on empty repo, both in innie and outtie
Life's a bit too short to fail when the git repo exists but is empty.
For innie (e.g., using -unsafe on an empty repo), we just create the
empty commit. For outtie, we do the same thing, since initializing an
empty repo would be a weird different code path.
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: se0a9606cc9f43388k
diff --git a/loop/agent.go b/loop/agent.go
index b10e2d6..21e10d7 100644
--- a/loop/agent.go
+++ b/loop/agent.go
@@ -1273,7 +1273,24 @@
}
}
- cmd := exec.CommandContext(ctx, "git", "tag", "-f", a.SketchGitBaseRef(), "HEAD")
+ // Check if we have any commits, and if not, create an empty initial commit
+ cmd := exec.CommandContext(ctx, "git", "rev-list", "--all", "--count")
+ cmd.Dir = repoRoot
+ countOut, err := cmd.CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("git rev-list --all --count: %s: %w", countOut, err)
+ }
+ commitCount := strings.TrimSpace(string(countOut))
+ if commitCount == "0" {
+ slog.Info("No commits found, creating empty initial commit")
+ cmd = exec.CommandContext(ctx, "git", "commit", "--allow-empty", "-m", "Initial empty commit")
+ cmd.Dir = repoRoot
+ if commitOut, err := cmd.CombinedOutput(); err != nil {
+ return fmt.Errorf("git commit --allow-empty: %s: %w", commitOut, err)
+ }
+ }
+
+ cmd = exec.CommandContext(ctx, "git", "tag", "-f", a.SketchGitBaseRef(), "HEAD")
cmd.Dir = repoRoot
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("git tag -f %s %s: %s: %w", a.SketchGitBaseRef(), "HEAD", out, err)