refactor: consolidate check-health DB access and expand hook checks
- bd-b8h: Extract getCheckHealthDBPath() to DRY up path resolution - bd-xyc: Open DB once in runCheckHealth, pass connection to check functions - bd-2em: checkHooksQuick now verifies all 4 hooks (pre-commit, post-merge, pre-push, post-checkout) instead of just post-merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
153
cmd/bd/doctor.go
153
cmd/bd/doctor.go
@@ -247,8 +247,31 @@ func runCheckHealth(path string) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get database path once (bd-b8h: centralized path resolution)
|
||||||
|
dbPath := getCheckHealthDBPath(beadsDir)
|
||||||
|
|
||||||
|
// Check if database exists
|
||||||
|
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||||
|
// No database - only check hooks
|
||||||
|
if issue := checkHooksQuick(path); issue != "" {
|
||||||
|
printCheckHealthHint([]string{issue})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open database once for all checks (bd-xyc: single DB connection)
|
||||||
|
db, err := sql.Open("sqlite3", "file:"+dbPath+"?mode=ro")
|
||||||
|
if err != nil {
|
||||||
|
// Can't open DB - only check hooks
|
||||||
|
if issue := checkHooksQuick(path); issue != "" {
|
||||||
|
printCheckHealthHint([]string{issue})
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
// Check if hints.doctor is disabled in config
|
// Check if hints.doctor is disabled in config
|
||||||
if hintsDisabled(beadsDir) {
|
if hintsDisabledDB(db) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -256,12 +279,12 @@ func runCheckHealth(path string) {
|
|||||||
var issues []string
|
var issues []string
|
||||||
|
|
||||||
// Check 1: Database version mismatch (CLI vs database bd_version)
|
// Check 1: Database version mismatch (CLI vs database bd_version)
|
||||||
if issue := checkVersionMismatch(beadsDir); issue != "" {
|
if issue := checkVersionMismatchDB(db); issue != "" {
|
||||||
issues = append(issues, issue)
|
issues = append(issues, issue)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check 2: Sync branch not configured
|
// Check 2: Sync branch not configured
|
||||||
if issue := checkSyncBranchQuick(beadsDir); issue != "" {
|
if issue := checkSyncBranchQuickDB(db); issue != "" {
|
||||||
issues = append(issues, issue)
|
issues = append(issues, issue)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -272,6 +295,13 @@ func runCheckHealth(path string) {
|
|||||||
|
|
||||||
// If any issues found, print hint
|
// If any issues found, print hint
|
||||||
if len(issues) > 0 {
|
if len(issues) > 0 {
|
||||||
|
printCheckHealthHint(issues)
|
||||||
|
}
|
||||||
|
// Silent exit on success
|
||||||
|
}
|
||||||
|
|
||||||
|
// printCheckHealthHint prints the health check hint and exits with error.
|
||||||
|
func printCheckHealthHint(issues []string) {
|
||||||
fmt.Fprintf(os.Stderr, "💡 bd doctor recommends a health check:\n")
|
fmt.Fprintf(os.Stderr, "💡 bd doctor recommends a health check:\n")
|
||||||
for _, issue := range issues {
|
for _, issue := range issues {
|
||||||
fmt.Fprintf(os.Stderr, " • %s\n", issue)
|
fmt.Fprintf(os.Stderr, " • %s\n", issue)
|
||||||
@@ -280,64 +310,32 @@ func runCheckHealth(path string) {
|
|||||||
fmt.Fprintf(os.Stderr, " (Suppress with: bd config set %s false)\n", ConfigKeyHintsDoctor)
|
fmt.Fprintf(os.Stderr, " (Suppress with: bd config set %s false)\n", ConfigKeyHintsDoctor)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
// Silent exit on success
|
|
||||||
}
|
|
||||||
|
|
||||||
// hintsDisabled checks if hints.doctor is set to "false" in the database config.
|
// getCheckHealthDBPath returns the database path for check-health operations.
|
||||||
func hintsDisabled(beadsDir string) bool {
|
// This centralizes the path resolution logic (bd-b8h).
|
||||||
// Get database path
|
func getCheckHealthDBPath(beadsDir string) string {
|
||||||
var dbPath string
|
|
||||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" {
|
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" {
|
||||||
dbPath = cfg.DatabasePath(beadsDir)
|
return cfg.DatabasePath(beadsDir)
|
||||||
} else {
|
}
|
||||||
dbPath = filepath.Join(beadsDir, beads.CanonicalDatabaseName)
|
return filepath.Join(beadsDir, beads.CanonicalDatabaseName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if database exists
|
// hintsDisabledDB checks if hints.doctor is set to "false" using an existing DB connection.
|
||||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
// Used by runCheckHealth to avoid multiple DB opens (bd-xyc).
|
||||||
return false // Can't check config, assume hints enabled
|
func hintsDisabledDB(db *sql.DB) bool {
|
||||||
}
|
|
||||||
|
|
||||||
// Open database read-only to check config
|
|
||||||
db, err := sql.Open("sqlite3", "file:"+dbPath+"?mode=ro")
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
var value string
|
var value string
|
||||||
err = db.QueryRow("SELECT value FROM config WHERE key = ?", ConfigKeyHintsDoctor).Scan(&value)
|
err := db.QueryRow("SELECT value FROM config WHERE key = ?", ConfigKeyHintsDoctor).Scan(&value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false // Key not set, assume hints enabled
|
return false // Key not set, assume hints enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
return strings.ToLower(value) == "false"
|
return strings.ToLower(value) == "false"
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkVersionMismatch checks if CLI version differs from database bd_version.
|
// checkVersionMismatchDB checks if CLI version differs from database bd_version.
|
||||||
func checkVersionMismatch(beadsDir string) string {
|
// Uses an existing DB connection (bd-xyc).
|
||||||
// Get database path
|
func checkVersionMismatchDB(db *sql.DB) string {
|
||||||
var dbPath string
|
|
||||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" {
|
|
||||||
dbPath = cfg.DatabasePath(beadsDir)
|
|
||||||
} else {
|
|
||||||
dbPath = filepath.Join(beadsDir, beads.CanonicalDatabaseName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if database exists
|
|
||||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
|
||||||
return "" // No database, skip check
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open database read-only
|
|
||||||
db, err := sql.Open("sqlite3", "file:"+dbPath+"?mode=ro")
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
var dbVersion string
|
var dbVersion string
|
||||||
err = db.QueryRow("SELECT value FROM metadata WHERE key = 'bd_version'").Scan(&dbVersion)
|
err := db.QueryRow("SELECT value FROM metadata WHERE key = 'bd_version'").Scan(&dbVersion)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "" // Can't read version, skip
|
return "" // Can't read version, skip
|
||||||
}
|
}
|
||||||
@@ -349,30 +347,11 @@ func checkVersionMismatch(beadsDir string) string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkSyncBranchQuick checks if sync.branch is configured.
|
// checkSyncBranchQuickDB checks if sync.branch is configured.
|
||||||
func checkSyncBranchQuick(beadsDir string) string {
|
// Uses an existing DB connection (bd-xyc).
|
||||||
// Get database path
|
func checkSyncBranchQuickDB(db *sql.DB) string {
|
||||||
var dbPath string
|
|
||||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" {
|
|
||||||
dbPath = cfg.DatabasePath(beadsDir)
|
|
||||||
} else {
|
|
||||||
dbPath = filepath.Join(beadsDir, beads.CanonicalDatabaseName)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if database exists
|
|
||||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
|
||||||
return "" // No database, skip check
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open database read-only
|
|
||||||
db, err := sql.Open("sqlite3", "file:"+dbPath+"?mode=ro")
|
|
||||||
if err != nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
defer db.Close()
|
|
||||||
|
|
||||||
var value string
|
var value string
|
||||||
err = db.QueryRow("SELECT value FROM config WHERE key = 'sync.branch'").Scan(&value)
|
err := db.QueryRow("SELECT value FROM config WHERE key = 'sync.branch'").Scan(&value)
|
||||||
if err != nil || value == "" {
|
if err != nil || value == "" {
|
||||||
return "sync.branch not configured"
|
return "sync.branch not configured"
|
||||||
}
|
}
|
||||||
@@ -381,6 +360,7 @@ func checkSyncBranchQuick(beadsDir string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// checkHooksQuick does a fast check for outdated git hooks.
|
// checkHooksQuick does a fast check for outdated git hooks.
|
||||||
|
// Checks all beads hooks: pre-commit, post-merge, pre-push, post-checkout (bd-2em).
|
||||||
func checkHooksQuick(path string) string {
|
func checkHooksQuick(path string) string {
|
||||||
hooksDir := filepath.Join(path, ".git", "hooks")
|
hooksDir := filepath.Join(path, ".git", "hooks")
|
||||||
|
|
||||||
@@ -389,17 +369,23 @@ func checkHooksQuick(path string) string {
|
|||||||
return "" // No git hooks directory, skip
|
return "" // No git hooks directory, skip
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check post-merge hook version (most likely to be outdated after merge)
|
// Check all beads-managed hooks (bd-2em: expanded from just post-merge)
|
||||||
hookPath := filepath.Join(hooksDir, "post-merge")
|
hookNames := []string{"pre-commit", "post-merge", "pre-push", "post-checkout"}
|
||||||
|
|
||||||
|
var outdatedHooks []string
|
||||||
|
var oldestVersion string
|
||||||
|
|
||||||
|
for _, hookName := range hookNames {
|
||||||
|
hookPath := filepath.Join(hooksDir, hookName)
|
||||||
content, err := os.ReadFile(hookPath) // #nosec G304 - path is controlled
|
content, err := os.ReadFile(hookPath) // #nosec G304 - path is controlled
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "" // Hook doesn't exist, skip (will be caught by full doctor)
|
continue // Hook doesn't exist, skip (will be caught by full doctor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Look for version marker
|
// Look for version marker
|
||||||
hookContent := string(content)
|
hookContent := string(content)
|
||||||
if !strings.Contains(hookContent, "bd-hooks-version:") {
|
if !strings.Contains(hookContent, "bd-hooks-version:") {
|
||||||
return "" // Not a bd hook or old format, skip
|
continue // Not a bd hook or old format, skip
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract version
|
// Extract version
|
||||||
@@ -409,16 +395,29 @@ func checkHooksQuick(path string) string {
|
|||||||
if len(parts) == 2 {
|
if len(parts) == 2 {
|
||||||
hookVersion := strings.TrimSpace(parts[1])
|
hookVersion := strings.TrimSpace(parts[1])
|
||||||
if hookVersion != Version {
|
if hookVersion != Version {
|
||||||
return fmt.Sprintf("Git hooks outdated (%s → %s)", hookVersion, Version)
|
outdatedHooks = append(outdatedHooks, hookName)
|
||||||
|
// Track the oldest version for display
|
||||||
|
if oldestVersion == "" || compareVersions(hookVersion, oldestVersion) < 0 {
|
||||||
|
oldestVersion = hookVersion
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(outdatedHooks) == 0 {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return summary of outdated hooks
|
||||||
|
if len(outdatedHooks) == 1 {
|
||||||
|
return fmt.Sprintf("Git hook %s outdated (%s → %s)", outdatedHooks[0], oldestVersion, Version)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("Git hooks outdated: %s (%s → %s)", strings.Join(outdatedHooks, ", "), oldestVersion, Version)
|
||||||
|
}
|
||||||
|
|
||||||
func runDiagnostics(path string) doctorResult {
|
func runDiagnostics(path string) doctorResult {
|
||||||
result := doctorResult{
|
result := doctorResult{
|
||||||
Path: path,
|
Path: path,
|
||||||
|
|||||||
Reference in New Issue
Block a user