fix: bd sync fails in bare repo worktrees (#785)

The bug: In a bare repo + worktrees setup, jsonlRelPath was calculated
relative to the project root (which contains all worktrees), resulting in
paths like "main/.beads/issues.jsonl". But the sync branch worktree uses
sparse checkout for .beads/*, so files are at ".beads/issues.jsonl".

This caused copyJSONLToMainRepo to look in the wrong location, silently
returning when the file was not found.

Fix: Add normalizeBeadsRelPath() to strip leading path components before
".beads", ensuring correct path resolution in both directions:
- copyJSONLToMainRepo (worktree -> local)
- SyncJSONLToWorktreeWithOptions (local -> worktree)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Steve Yegge
2025-12-29 15:39:07 -08:00
parent 1256352d49
commit 0fa97a4c30
3 changed files with 93 additions and 3 deletions

View File

@@ -656,7 +656,12 @@ func extractJSONLFromCommit(ctx context.Context, worktreePath, commit, filePath
// copyJSONLToMainRepo copies JSONL and related files from worktree to main repo.
func copyJSONLToMainRepo(worktreePath, jsonlRelPath, jsonlPath string) error {
worktreeJSONLPath := filepath.Join(worktreePath, jsonlRelPath)
// GH#785: Handle bare repo worktrees where jsonlRelPath might include the
// worktree name (e.g., "main/.beads/issues.jsonl" instead of ".beads/issues.jsonl").
// The sync branch uses sparse checkout for .beads/* so we normalize the path
// to strip any leading components before .beads.
normalizedRelPath := normalizeBeadsRelPath(jsonlRelPath)
worktreeJSONLPath := filepath.Join(worktreePath, normalizedRelPath)
// Check if worktree JSONL exists
if _, err := os.Stat(worktreeJSONLPath); os.IsNotExist(err) {
@@ -1077,6 +1082,20 @@ func formatVanishedIssues(localIssues, mergedIssues map[string]issueSummary, loc
return lines
}
// normalizeBeadsRelPath strips any leading path components before .beads.
// This handles bare repo worktrees where the relative path includes the worktree
// name (e.g., "main/.beads/issues.jsonl" -> ".beads/issues.jsonl").
// GH#785: Fix for sync failing across worktrees in bare repo setup.
func normalizeBeadsRelPath(relPath string) string {
// Use filepath.ToSlash for consistent handling across platforms
normalized := filepath.ToSlash(relPath)
if idx := strings.Index(normalized, ".beads"); idx > 0 {
// Strip leading path components before .beads
return filepath.FromSlash(normalized[idx:])
}
return relPath
}
// HasGitRemote checks if any git remote exists
func HasGitRemote(ctx context.Context) bool {
cmd := exec.CommandContext(ctx, "git", "remote")