Fix migrate sync failing in git worktree environments (#970)

Two issues caused `bd migrate sync` to fail when run from a git worktree:

1. Used GetGitDir() instead of GetGitCommonDir() for worktree path
   - GetGitDir() returns the worktree-specific path (.bare/worktrees/main)
   - GetGitCommonDir() returns the shared git dir (.bare) where new
     worktrees can actually be created

2. Used strings.Index instead of LastIndex in GetRepoRoot()
   - When user paths contain "worktrees" (e.g., ~/Development/worktrees/),
     Index finds the first occurrence and incorrectly strips the path
   - LastIndex finds git's internal /worktrees/ directory

Added GetGitCommonDir() to internal/git/gitdir.go for reuse.

Fixes GH#639 (remaining unfixed callsite in migrate_sync.go)
This commit is contained in:
shendsaliaga
2026-01-09 14:01:56 -05:00
committed by GitHub
parent 2ee0995f51
commit c988c76b08
3 changed files with 26 additions and 8 deletions

View File

@@ -155,12 +155,12 @@ func runMigrateSync(ctx context.Context, branchName string, dryRun, force bool)
fmt.Printf("→ Would create new branch '%s'\n", branchName)
}
// Use worktree-aware git directory detection
gitDir, err := git.GetGitDir()
// Use git-common-dir for worktree path to support bare repos and worktrees (GH#639)
gitCommonDir, err := git.GetGitCommonDir()
if err != nil {
return fmt.Errorf("not a git repository: %w", err)
}
worktreePath := filepath.Join(gitDir, "beads-worktrees", branchName)
worktreePath := filepath.Join(gitCommonDir, "beads-worktrees", branchName)
fmt.Printf("→ Would create worktree at: %s\n", worktreePath)
fmt.Println("\n=== END DRY RUN ===")
@@ -195,12 +195,12 @@ func runMigrateSync(ctx context.Context, branchName string, dryRun, force bool)
}
// Step 2: Create the worktree
// Use worktree-aware git directory detection
gitDir, err := git.GetGitDir()
// Use git-common-dir for worktree path to support bare repos and worktrees (GH#639)
gitCommonDir, err := git.GetGitCommonDir()
if err != nil {
return fmt.Errorf("not a git repository: %w", err)
}
worktreePath := filepath.Join(gitDir, "beads-worktrees", branchName)
worktreePath := filepath.Join(gitCommonDir, "beads-worktrees", branchName)
fmt.Printf("→ Creating worktree at %s...\n", worktreePath)
wtMgr := git.NewWorktreeManager(repoRoot)