From 5d6090e80f827970bbf70c23eddc9e705ade5b36 Mon Sep 17 00:00:00 2001 From: matt wilkie Date: Sat, 1 Aug 2026 09:51:15 -0700 Subject: [PATCH] fix(cli): report commit no-ops by HEAD movement, not by error Cross-vendor review of 412cab2de (which made EmbeddedDoltStore.Commit tolerate nothing-to-commit for gh 3886 parity) found that CLI callers keyed their "did we commit" reporting off Commit's returned error. Server mode has always tolerated this error internally, so it has ALWAYS misreported a no-op commit on a clean working set; the embedded fix just widened the window by making the same tolerance reachable there too. - cmd/bd/vc.go (`bd vc commit`): capture GetCurrentCommit before Commit; after a nil-error Commit, if HEAD is unchanged, report the no-op ("Nothing to commit" / JSON committed:false) same as the existing isDoltNothingToCommit error path. - cmd/bd/dolt.go (`bd dolt commit`): same before/after HEAD comparison, printing "Nothing to commit." instead of "Committed." - cmd/bd/main.go (shutdown pending-batch flush): only print "Flushed pending batch commit on shutdown" when HEAD actually moved, so a clean shutdown stays quiet. The existing isDoltNothingToCommit(err) fallback is kept at all three sites for stores/engines that still surface the error directly. Added embedded-mode regression tests (commit_clean_reports_nothing in both vc_embedded_test.go and dolt_embedded_test.go) asserting the clean-working- set case (immediately after `bd init`, which itself now commits cleanly) reports the no-op rather than a fresh commit. Verified: gofmt clean; go vet -tags gms_pure_go ./cmd/bd/ clean; go build -tags gms_pure_go ./... clean; CGO_ENABLED=1 go test -tags gms_pure_go -run 'TestEmbeddedVC$|TestEmbeddedDolt$' ./cmd/bd/ -count=1 passes (BEADS_TEST_EMBEDDED_DOLT=1), including both new subtests. Resolves the cross-vendor review finding on 412cab2de and absorbs bd mybd-z9h7j. Agent-Signature: claude-fable-5-high on behalf of maphew --- cmd/bd/dolt.go | 11 +++++++++++ cmd/bd/dolt_embedded_test.go | 14 ++++++++++++++ cmd/bd/main.go | 14 ++++++++++++-- cmd/bd/vc.go | 14 ++++++++++++++ cmd/bd/vc_embedded_test.go | 25 +++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 2 deletions(-) diff --git a/cmd/bd/dolt.go b/cmd/bd/dolt.go index 4fc8a0132..d46cec4e4 100644 --- a/cmd/bd/dolt.go +++ b/cmd/bd/dolt.go @@ -732,6 +732,7 @@ For more options (--stdin, custom messages), see: bd vc commit`, if msg == "" { msg = fmt.Sprintf("bd: dolt commit (auto-commit) by %s", getActor()) } + beforeHash, beforeErr := st.GetCurrentCommit(ctx) if err := st.Commit(ctx, msg); err != nil { if isDoltNothingToCommit(err) { fmt.Println("Nothing to commit.") @@ -740,6 +741,16 @@ For more options (--stdin, custom messages), see: bd vc commit`, return HandleError("%v", err) } commandDidExplicitDoltCommit = true + + // A store whose Commit tolerates nothing-to-commit (e.g. the embedded + // store) returns a nil error even when HEAD did not move. Detect that + // case here instead of relying on the error, so both backends report + // the same "nothing to commit" outcome. + if afterHash, afterErr := st.GetCurrentCommit(ctx); beforeErr == nil && afterErr == nil && afterHash == beforeHash { + fmt.Println("Nothing to commit.") + return nil + } + fmt.Println("Committed.") return nil }, diff --git a/cmd/bd/dolt_embedded_test.go b/cmd/bd/dolt_embedded_test.go index a6baedb6a..d04184324 100644 --- a/cmd/bd/dolt_embedded_test.go +++ b/cmd/bd/dolt_embedded_test.go @@ -125,6 +125,20 @@ func TestEmbeddedDolt(t *testing.T) { _ = out }) + t.Run("commit_clean_reports_nothing", func(t *testing.T) { + // bd init leaves the working set clean (bootstrap's own commit + // tolerates nothing-to-commit, GH#3886). A second, truly no-op + // "bd dolt commit" here must print "Nothing to commit.", not + // "Committed." — this is the case EmbeddedDoltStore.Commit's new + // nothing-to-commit tolerance made possible to reach with a nil + // error. + cleanDir, _, _ := bdInit(t, bd, "--prefix", "tdcln") + out := bdDolt(t, bd, cleanDir, "commit") + if !strings.Contains(out, "Nothing to commit.") { + t.Errorf("expected 'Nothing to commit.' on a clean working set, got: %s", out) + } + }) + // ===== Remote management ===== t.Run("remote_list_empty", func(t *testing.T) { diff --git a/cmd/bd/main.go b/cmd/bd/main.go index 368a3bf49..77836098e 100644 --- a/cmd/bd/main.go +++ b/cmd/bd/main.go @@ -1886,13 +1886,23 @@ func flushBatchCommitOnShutdown() { ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() + beforeHash, beforeErr := st.GetCurrentCommit(ctx) + if err := st.Commit(ctx, "bd: flush pending changes on shutdown"); err != nil { if !isDoltNothingToCommit(err) { fmt.Fprintf(os.Stderr, "\nWarning: failed to flush batch commit on shutdown: %v\n", err) } - } else { - fmt.Fprintf(os.Stderr, "\nFlushed pending batch commit on shutdown\n") + return } + + // A store whose Commit tolerates nothing-to-commit (e.g. the embedded + // store) returns a nil error even when HEAD did not move; only report + // the flush when HEAD actually advanced, so a clean shutdown stays quiet. + if afterHash, afterErr := st.GetCurrentCommit(ctx); beforeErr == nil && afterErr == nil && afterHash == beforeHash { + return + } + + fmt.Fprintf(os.Stderr, "\nFlushed pending batch commit on shutdown\n") } // validateWorkspaceIdentity checks that the project identity from metadata.json diff --git a/cmd/bd/vc.go b/cmd/bd/vc.go index b17c15ad3..564f82d76 100644 --- a/cmd/bd/vc.go +++ b/cmd/bd/vc.go @@ -205,6 +205,8 @@ Examples: return HandleErrorRespectJSON("commit message is required (use -m, --message, or --stdin)") } + beforeHash, beforeErr := store.GetCurrentCommit(ctx) + commandDidExplicitDoltCommit = true if err := store.Commit(ctx, vcCommitMessage); err != nil { if isDoltNothingToCommit(err) { @@ -222,6 +224,18 @@ Examples: hash = "(unknown)" } + // A store whose Commit tolerates nothing-to-commit (e.g. the embedded + // store) returns a nil error even when HEAD did not move. Detect that + // case here instead of relying on the error, so both backends report + // the same "nothing to commit" outcome. + if beforeErr == nil && err == nil && hash == beforeHash { + if jsonOutput { + return outputJSON(map[string]interface{}{"committed": false, "message": "nothing to commit"}) + } + fmt.Println("Nothing to commit") + return nil + } + if jsonOutput { return outputJSON(map[string]interface{}{ "committed": true, diff --git a/cmd/bd/vc_embedded_test.go b/cmd/bd/vc_embedded_test.go index a377f9d3f..6fe779803 100644 --- a/cmd/bd/vc_embedded_test.go +++ b/cmd/bd/vc_embedded_test.go @@ -88,6 +88,31 @@ func TestEmbeddedVC(t *testing.T) { } }) + t.Run("commit_clean_reports_nothing", func(t *testing.T) { + // bd init leaves the working set clean (bootstrap's own commit + // tolerates nothing-to-commit, GH#3886). A second, truly no-op + // commit here must be reported as "nothing to commit", not + // silently treated as a fresh commit — this is the case + // EmbeddedDoltStore.Commit's new nothing-to-commit tolerance made + // possible to reach with a nil error. + dir, _, _ := bdInit(t, bd, "--prefix", "vccln") + + cmd := exec.Command(bd, "vc", "commit", "-m", "clean working set", "--json") + cmd.Dir = dir + cmd.Env = bdEnv(dir) + stdout, stderr, err := runCommandBuffers(t, cmd) + if err != nil { + t.Fatalf("bd vc commit --json failed unexpectedly: %v\nstdout:\n%s\nstderr:\n%s", err, stdout.String(), stderr.String()) + } + var result map[string]interface{} + if jsonErr := json.Unmarshal(stdout.Bytes(), &result); jsonErr != nil { + t.Fatalf("failed to parse JSON: %v\n%s", jsonErr, stdout.String()) + } + if committed, _ := result["committed"].(bool); committed { + t.Errorf("expected committed=false on a clean working set, got: %s", stdout.String()) + } + }) + t.Run("commit_json", func(t *testing.T) { dir, _, _ := bdInit(t, bd, "--prefix", "vccj") // bd create auto-commits, so vc commit may see "nothing to commit".