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
+3 -2
View File
@@ -987,8 +987,9 @@ func GetRepoRoot(ctx context.Context) (string, error) {
line := strings.TrimSpace(string(content))
if strings.HasPrefix(line, "gitdir: ") {
gitDir := strings.TrimPrefix(line, "gitdir: ")
// Remove /worktrees/* part
if idx := strings.Index(gitDir, "/worktrees/"); idx > 0 {
// Remove /worktrees/* part - use LastIndex to handle user paths containing "worktrees"
// e.g., /Users/foo/worktrees/project/.bare/worktrees/main should strip at .bare/worktrees/
if idx := strings.LastIndex(gitDir, "/worktrees/"); idx > 0 {
gitDir = gitDir[:idx]
}
repoRoot = filepath.Dir(gitDir)