Fix bd-191: bd sync --dry-run should not modify database

Skip auto-import when sync command is run with --dry-run flag to prevent
database modifications during dry-run mode. Previously, autoImportIfNewer()
would run in PersistentPreRun hook and modify the database even in dry-run,
causing the JSONL file to become dirty.

Amp-Thread-ID: https://ampcode.com/threads/T-1bc29344-0c59-4127-855d-860d1579ba0b
Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
Steve Yegge
2025-10-27 18:40:58 -07:00
parent 897edc6ce7
commit 6b5d26d7d1
2 changed files with 26 additions and 1 deletions

View File

@@ -428,8 +428,21 @@ var rootCmd = &cobra.Command{
// Auto-import if JSONL is newer than DB (e.g., after git pull)
// Skip for import command itself to avoid recursion
// Skip if sync --dry-run to avoid modifying DB in dry-run mode (bd-191)
if cmd.Name() != "import" && autoImportEnabled {
autoImportIfNewer()
// Check if this is sync command with --dry-run flag
if cmd.Name() == "sync" {
if dryRun, _ := cmd.Flags().GetBool("dry-run"); dryRun {
// Skip auto-import in dry-run mode
if os.Getenv("BD_DEBUG") != "" {
fmt.Fprintf(os.Stderr, "Debug: auto-import skipped for sync --dry-run\n")
}
} else {
autoImportIfNewer()
}
} else {
autoImportIfNewer()
}
}
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {