Maintainer-side re-port of realies' #4288 (substance-approved by maphew 2026-07-27), redone against current main per bd-oibnz. The consolidation half of the original PR (issues.go hand-rolled DOLT_ADD/DOLT_COMMIT sites onto doltAddAndCommitInTx) already landed independently via #5341, so this carries the remaining half: the empty-commit guards. An idempotent no-op write (INSERT IGNORE duplicate, ON DUPLICATE KEY re-writing the same value, UPDATE matching nothing) leaves its table clean, so the selective DOLT_ADD stages nothing and DOLT_COMMIT('-m') fails server-side with a 'nothing to commit' warning — flooding dolt.log at reconcile cadence. - issueops.HasStagedChanges: reports whether dolt_status has staged rows — exactly what a '-m' commit would include. - versioncontrolops.StageAndCommit: global HasPendingChanges fast-path before staging, plus the precise HasStagedChanges guard before commit (an unrelated dirty table must not trigger an empty '-m' commit). - DoltStore.doltAddAndCommit / doltAddAndCommitInTx: staged-set guard before commit (these stage a fixed table list, so only the staged check applies). A guard-read failure in doltAddAndCommit is a plain error, not a publication failure: nothing committed, nothing indeterminate. - Tests: sqlmock discriminators asserting DOLT_COMMIT is NOT issued on an empty staged set (they fail if a guard is reverted), integration regression tests for no-op dependency writes and claim/close over-suppression, and existing commit-boundary/draincall/phase harnesses taught to serve the two new dolt_status guard reads. Co-authored-by: realies <5107843+realies@users.noreply.github.com>
144 lines
4.4 KiB
Go
144 lines
4.4 KiB
Go
package issueops
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// SQLQuerier is the subset of *sql.Tx / *sql.DB needed by the commit helpers.
|
|
type SQLQuerier interface {
|
|
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
|
|
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
|
|
}
|
|
|
|
// HasPendingChanges checks whether there are any committable changes in the
|
|
// Dolt working set, excluding tables matched by dolt_ignore.
|
|
func HasPendingChanges(ctx context.Context, db SQLQuerier) (bool, error) {
|
|
var count int
|
|
err := db.QueryRowContext(ctx, `
|
|
SELECT COUNT(*) FROM dolt_status s
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM dolt_ignore di
|
|
WHERE di.ignored = 1
|
|
AND s.table_name LIKE di.pattern
|
|
)`).Scan(&count)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to check status: %w", err)
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// HasStagedChanges reports whether the Dolt working set has any STAGED changes,
|
|
// i.e. rows that a subsequent DOLT_COMMIT('-m', …) would actually commit.
|
|
//
|
|
// This is the correct pre-commit check for selective-staging commit helpers
|
|
// (StageAndCommit, doltAddAndCommit, doltAddAndCommitInTx) that DOLT_ADD only a
|
|
// fixed/dirty-tracked set of tables. A global HasPendingChanges check is NOT
|
|
// sufficient for them: a table can be marked dirty by a write statement yet have
|
|
// no real row change (idempotent INSERT IGNORE / ON DUPLICATE KEY no-op, or an
|
|
// UPDATE matching nothing). When some OTHER table is concurrently dirty in the
|
|
// working set, HasPendingChanges is true, but staging only the (clean) target
|
|
// tables stages nothing — and DOLT_COMMIT('-m') then fails server-side with a
|
|
// "nothing to commit" warning that floods the Dolt log at reconcile cadence.
|
|
// Checking the staged set AFTER DOLT_ADD captures exactly what '-m' will commit.
|
|
func HasStagedChanges(ctx context.Context, db SQLQuerier) (bool, error) {
|
|
var count int
|
|
err := db.QueryRowContext(ctx,
|
|
"SELECT COUNT(*) FROM dolt_status WHERE staged = 1").Scan(&count)
|
|
if err != nil {
|
|
return false, fmt.Errorf("failed to check staged status: %w", err)
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// BuildBatchCommitMessage generates a descriptive commit message summarizing
|
|
// what changed since the last commit by querying dolt_diff against HEAD.
|
|
// It reports issue-level create/update/delete counts and lists any other
|
|
// tables (labels, comments, events, etc.) that have uncommitted changes.
|
|
func BuildBatchCommitMessage(ctx context.Context, db SQLQuerier, actor string) string {
|
|
if actor == "" {
|
|
actor = "bd"
|
|
}
|
|
|
|
var added, modified, removed int
|
|
rows, err := db.QueryContext(ctx, `
|
|
SELECT diff_type, COUNT(*) as cnt
|
|
FROM dolt_diff('HEAD', 'WORKING', 'issues')
|
|
GROUP BY diff_type
|
|
`)
|
|
if err == nil {
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var diffType string
|
|
var count int
|
|
if scanErr := rows.Scan(&diffType, &count); scanErr == nil {
|
|
switch diffType {
|
|
case "added":
|
|
added = count
|
|
case "modified":
|
|
modified = count
|
|
case "removed":
|
|
removed = count
|
|
}
|
|
}
|
|
}
|
|
_ = rows.Err()
|
|
}
|
|
|
|
var otherTables []string
|
|
statusRows, statusErr := db.QueryContext(ctx, `
|
|
SELECT table_name FROM dolt_status s
|
|
WHERE table_name != 'issues'
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM dolt_ignore di
|
|
WHERE di.ignored = 1
|
|
AND s.table_name LIKE di.pattern
|
|
)`)
|
|
if statusErr == nil {
|
|
defer statusRows.Close()
|
|
for statusRows.Next() {
|
|
var table string
|
|
if scanErr := statusRows.Scan(&table); scanErr == nil {
|
|
otherTables = append(otherTables, table)
|
|
}
|
|
}
|
|
_ = statusRows.Err()
|
|
}
|
|
|
|
msg := fmt.Sprintf("bd: batch commit by %s", actor)
|
|
var parts []string
|
|
if added > 0 {
|
|
parts = append(parts, fmt.Sprintf("%d created", added))
|
|
}
|
|
if modified > 0 {
|
|
parts = append(parts, fmt.Sprintf("%d updated", modified))
|
|
}
|
|
if removed > 0 {
|
|
parts = append(parts, fmt.Sprintf("%d deleted", removed))
|
|
}
|
|
if len(parts) > 0 {
|
|
msg += " — " + strings.Join(parts, ", ")
|
|
}
|
|
if len(otherTables) > 0 {
|
|
msg += fmt.Sprintf(" (+ %s)", strings.Join(otherTables, ", "))
|
|
}
|
|
return msg
|
|
}
|
|
|
|
// IsNothingToCommitError returns true if the error indicates there was nothing
|
|
// to commit (Dolt may report this even when dolt_status showed changes).
|
|
func IsNothingToCommitError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
s := strings.ToLower(err.Error())
|
|
if strings.Contains(s, "nothing to commit") {
|
|
return true
|
|
}
|
|
if strings.Contains(s, "no changes") && strings.Contains(s, "commit") {
|
|
return true
|
|
}
|
|
return false
|
|
}
|