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
This commit is contained in:
matt wilkie 2026-08-01 09:51:15 -07:00
commit 5d6090e80f
5 changed files with 76 additions and 2 deletions

View file

@ -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
},

View file

@ -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) {

View file

@ -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

View file

@ -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,

View file

@ -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".