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:
@@ -287,24 +287,6 @@ With --stealth: configures per-repository git settings for invisible beads usage
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Set sync.branch only if explicitly specified via --branch flag
|
||||
// GH#807: Do NOT auto-detect current branch - if sync.branch is set to main/master,
|
||||
// the worktree created by bd sync will check out main, preventing the user from
|
||||
// checking out main in their working directory (git error: "'main' is already checked out")
|
||||
//
|
||||
// When --branch is not specified, bd sync will commit directly to the current branch
|
||||
// (the original behavior before sync branch feature)
|
||||
if branch != "" {
|
||||
if err := syncbranch.Set(ctx, store, branch); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: failed to set sync branch: %v\n", err)
|
||||
_ = store.Close()
|
||||
os.Exit(1)
|
||||
}
|
||||
if !quiet {
|
||||
fmt.Printf(" Sync branch: %s\n", branch)
|
||||
}
|
||||
}
|
||||
|
||||
// === TRACKING METADATA (Pattern B: Warn and Continue) ===
|
||||
// Tracking metadata enhances functionality (diagnostics, version checks, collision detection)
|
||||
// but the system works without it. Failures here degrade gracefully - we warn but continue.
|
||||
@@ -386,6 +368,27 @@ With --stealth: configures per-repository git settings for invisible beads usage
|
||||
}
|
||||
}
|
||||
|
||||
// Set sync.branch only if explicitly specified via --branch flag
|
||||
// GH#807: Do NOT auto-detect current branch - if sync.branch is set to main/master,
|
||||
// the worktree created by bd sync will check out main, preventing the user from
|
||||
// checking out main in their working directory (git error: "'main' is already checked out")
|
||||
//
|
||||
// When --branch is not specified, bd sync will commit directly to the current branch
|
||||
// (the original behavior before sync branch feature)
|
||||
//
|
||||
// GH#927: This must run AFTER createConfigYaml() so that config.yaml exists
|
||||
// and syncbranch.Set() can update it via config.SetYamlConfig() (PR#910 mechanism)
|
||||
if branch != "" {
|
||||
if err := syncbranch.Set(ctx, store, branch); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: failed to set sync branch: %v\n", err)
|
||||
_ = store.Close()
|
||||
os.Exit(1)
|
||||
}
|
||||
if !quiet {
|
||||
fmt.Printf(" Sync branch: %s\n", branch)
|
||||
}
|
||||
}
|
||||
|
||||
// Check if git has existing issues to import (fresh clone scenario)
|
||||
// With --from-jsonl: import from local file instead of git history
|
||||
if fromJSONL {
|
||||
|
||||
@@ -1188,6 +1188,64 @@ func TestInitBranchPersistsToConfigYaml(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestInitReinitWithBranch verifies that --branch flag works on reinit
|
||||
// GH#927: When reinitializing with --branch, config.yaml should be updated even if it exists
|
||||
func TestInitReinitWithBranch(t *testing.T) {
|
||||
// Reset global state
|
||||
origDBPath := dbPath
|
||||
defer func() { dbPath = origDBPath }()
|
||||
dbPath = ""
|
||||
|
||||
// Reset Cobra flags
|
||||
initCmd.Flags().Set("branch", "")
|
||||
initCmd.Flags().Set("force", "false")
|
||||
|
||||
tmpDir := t.TempDir()
|
||||
t.Chdir(tmpDir)
|
||||
|
||||
// Initialize git repo first
|
||||
if err := runCommandInDir(tmpDir, "git", "init", "--initial-branch=dev"); err != nil {
|
||||
t.Fatalf("Failed to init git: %v", err)
|
||||
}
|
||||
|
||||
// First init WITHOUT --branch (creates config.yaml with commented sync-branch)
|
||||
rootCmd.SetArgs([]string{"init", "--prefix", "test", "--quiet"})
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
t.Fatalf("First init failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify config.yaml has commented sync-branch initially
|
||||
configPath := filepath.Join(tmpDir, ".beads", "config.yaml")
|
||||
content, err := os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read config.yaml: %v", err)
|
||||
}
|
||||
if !strings.Contains(string(content), "# sync-branch:") {
|
||||
t.Errorf("Initial config.yaml should have commented sync-branch")
|
||||
}
|
||||
|
||||
// Reset Cobra flags for reinit
|
||||
initCmd.Flags().Set("branch", "")
|
||||
initCmd.Flags().Set("force", "false")
|
||||
|
||||
// Reinit WITH --branch (should update existing config.yaml)
|
||||
rootCmd.SetArgs([]string{"init", "--prefix", "test", "--branch", "beads-sync", "--force", "--quiet"})
|
||||
if err := rootCmd.Execute(); err != nil {
|
||||
t.Fatalf("Reinit with --branch failed: %v", err)
|
||||
}
|
||||
|
||||
// Verify config.yaml now has uncommented sync-branch
|
||||
content, err = os.ReadFile(configPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read config.yaml after reinit: %v", err)
|
||||
}
|
||||
|
||||
configStr := string(content)
|
||||
if !strings.Contains(configStr, "sync-branch: \"beads-sync\"") {
|
||||
t.Errorf("After reinit with --branch, config.yaml should contain uncommented 'sync-branch: \"beads-sync\"', got:\n%s", configStr)
|
||||
}
|
||||
}
|
||||
|
||||
// TestSetupGlobalGitIgnore_ReadOnly verifies graceful handling when the
|
||||
// gitignore file cannot be written (prints manual instructions instead of failing).
|
||||
func TestSetupGlobalGitIgnore_ReadOnly(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user