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