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 }