fix(init): ensure sync branch persistence on init

Problem:
- Sync branch setup occurred before the config file was initialized
- Persistence only targeted the database, leading to loss on re-init

Solution:
- Reorder initialization to create the config file before sync setup
- Synchronize sync branch state to both config file and database

Impact:
- Settings are preserved across re-initialization and DB clears
- Better consistency between file and database state

Fixes: #927 (Bug 3)
This commit is contained in:
Peter Chanthamynavong
2026-01-06 15:22:15 -08:00
parent c1d3644b9a
commit 362b07d74f
4 changed files with 168 additions and 19 deletions

View File

@@ -193,13 +193,29 @@ func getConfigFromDB(dbPath string, key string) string {
return value
}
// Set stores the sync branch configuration in the database
// Set stores the sync branch configuration in both config.yaml AND the database.
// GH#909: Writing to both ensures bd doctor and migrate detection work correctly.
//
// Config precedence on read (from Get function):
// 1. BEADS_SYNC_BRANCH env var
// 2. sync-branch in config.yaml (recommended, version controlled)
// 3. sync.branch in database (legacy, for backward compatibility)
func Set(ctx context.Context, store storage.Storage, branch string) error {
// GH#807: Use sync-specific validation that rejects main/master
if err := ValidateSyncBranchName(branch); err != nil {
return err
}
// GH#909: Write to config.yaml first (primary source for doctor/migration checks)
// This also handles uncommenting if the key was commented out
if err := config.SetYamlConfig(ConfigYAMLKey, branch); err != nil {
// Log warning but don't fail - database write is still valuable
// This can fail if config.yaml doesn't exist yet (pre-init state)
// In that case, the database config still works for backward compatibility
fmt.Fprintf(os.Stderr, "Warning: could not update config.yaml: %v\n", err)
}
// Write to database for backward compatibility
return store.SetConfig(ctx, ConfigKey, branch)
}