fix: hide issues.jsonl from git status when sync.branch configured (GH#870)

When sync.branch is configured, issues.jsonl appears modified in git status
even though changes go to the sync branch. This is confusing for users and
risks accidental commits to the wrong branch.

Implementation:
- Added SyncBranchGitignore() to set git index flags (assume-unchanged,
  skip-worktree) on issues.jsonl when sync.branch is configured
- For untracked files, adds to .git/info/exclude instead
- Called automatically from bd sync after successful sync-branch sync
- Added bd doctor check and fix for this issue
- Added HasSyncBranchGitignoreFlags() to check current flag state
- Added ClearSyncBranchGitignore() to remove flags when sync.branch disabled

Fixes: GH#870 (duplicate of GH#797, GH#801)

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

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>

Executed-By: beads/crew/dave
Rig: beads
Role: crew
This commit is contained in:
dave
2026-01-03 21:07:32 -08:00
committed by Steve Yegge
parent 62e4eaf7c1
commit 6a5c289af3
5 changed files with 269 additions and 0 deletions
+161
View File
@@ -2,7 +2,9 @@ package fix
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
@@ -147,3 +149,162 @@ func SyncBranchHealth(path, syncBranch string) error {
fmt.Printf(" ✓ Recreated %s from %s and pushed\n", syncBranch, mainBranch)
return nil
}
// SyncBranchGitignore sets git index flags to hide .beads/issues.jsonl from git status
// when sync.branch is configured. This prevents the file from showing as modified on
// the main branch while actual data lives on the sync branch. (GH#797, GH#801, GH#870)
//
// Sets both flags for comprehensive hiding:
// - assume-unchanged: Performance optimization, skips file stat check
// - skip-worktree: Clear error message if user tries explicit `git add`
func SyncBranchGitignore(path string) error {
if err := validateBeadsWorkspace(path); err != nil {
return err
}
// Find the .beads directory
beadsDir := filepath.Join(path, ".beads")
if _, err := os.Stat(beadsDir); os.IsNotExist(err) {
return fmt.Errorf(".beads directory not found at %s", beadsDir)
}
// Check if issues.jsonl exists and is tracked by git
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
// File doesn't exist, nothing to hide
return nil
}
// Check if file is tracked by git
cmd := exec.Command("git", "ls-files", "--error-unmatch", jsonlPath)
cmd.Dir = path
if err := cmd.Run(); err != nil {
// File is not tracked - add to .git/info/exclude instead
return addToGitExclude(path, ".beads/issues.jsonl")
}
// File is tracked - set both git index flags
// These must be separate commands (git quirk)
cmd = exec.Command("git", "update-index", "--assume-unchanged", jsonlPath)
cmd.Dir = path
if out, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to set assume-unchanged: %w\n%s", err, out)
}
cmd = exec.Command("git", "update-index", "--skip-worktree", jsonlPath)
cmd.Dir = path
if out, err := cmd.CombinedOutput(); err != nil {
// Revert assume-unchanged if skip-worktree fails
revertCmd := exec.Command("git", "update-index", "--no-assume-unchanged", jsonlPath)
revertCmd.Dir = path
_ = revertCmd.Run()
return fmt.Errorf("failed to set skip-worktree: %w\n%s", err, out)
}
fmt.Println(" ✓ Set git index flags to hide .beads/issues.jsonl")
return nil
}
// ClearSyncBranchGitignore removes git index flags from .beads/issues.jsonl.
// Called when sync.branch is disabled to restore normal git tracking.
func ClearSyncBranchGitignore(path string) error {
beadsDir := filepath.Join(path, ".beads")
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
return nil // File doesn't exist, nothing to do
}
// Check if file is tracked
cmd := exec.Command("git", "ls-files", "--error-unmatch", jsonlPath)
cmd.Dir = path
if err := cmd.Run(); err != nil {
return nil // Not tracked, nothing to clear
}
// Clear both flags
cmd = exec.Command("git", "update-index", "--no-assume-unchanged", jsonlPath)
cmd.Dir = path
_ = cmd.Run() // Ignore errors - flag might not be set
cmd = exec.Command("git", "update-index", "--no-skip-worktree", jsonlPath)
cmd.Dir = path
_ = cmd.Run() // Ignore errors - flag might not be set
return nil
}
// HasSyncBranchGitignoreFlags checks if git index flags are set on .beads/issues.jsonl
func HasSyncBranchGitignoreFlags(path string) (bool, bool, error) {
beadsDir := filepath.Join(path, ".beads")
jsonlPath := filepath.Join(beadsDir, "issues.jsonl")
if _, err := os.Stat(jsonlPath); os.IsNotExist(err) {
return false, false, nil
}
// Get file status from git ls-files -v
// 'h' = assume-unchanged, 'S' = skip-worktree
cmd := exec.Command("git", "ls-files", "-v", jsonlPath)
cmd.Dir = path
output, err := cmd.Output()
if err != nil {
return false, false, nil // File not tracked
}
line := strings.TrimSpace(string(output))
if len(line) == 0 {
return false, false, nil
}
// First character indicates status:
// 'H' = tracked, 'h' = assume-unchanged, 'S' = skip-worktree
firstChar := line[0]
hasAssumeUnchanged := firstChar == 'h'
hasSkipWorktree := firstChar == 'S'
return hasAssumeUnchanged, hasSkipWorktree, nil
}
// addToGitExclude adds a pattern to .git/info/exclude
func addToGitExclude(path, pattern string) error {
// Get git directory
cmd := exec.Command("git", "rev-parse", "--git-dir")
cmd.Dir = path
output, err := cmd.Output()
if err != nil {
return fmt.Errorf("failed to get git directory: %w", err)
}
gitDir := strings.TrimSpace(string(output))
if !filepath.IsAbs(gitDir) {
gitDir = filepath.Join(path, gitDir)
}
excludePath := filepath.Join(gitDir, "info", "exclude")
// Create info directory if needed
if err := os.MkdirAll(filepath.Dir(excludePath), 0755); err != nil {
return fmt.Errorf("failed to create info directory: %w", err)
}
// Check if pattern already exists
content, _ := os.ReadFile(excludePath)
if strings.Contains(string(content), pattern) {
return nil // Already excluded
}
// Append pattern
f, err := os.OpenFile(excludePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("failed to open exclude file: %w", err)
}
defer f.Close()
if _, err := f.WriteString(pattern + "\n"); err != nil {
return fmt.Errorf("failed to write exclude pattern: %w", err)
}
fmt.Printf(" ✓ Added %s to .git/info/exclude\n", pattern)
return nil
}