Fix bd-73: Add git worktree detection and warnings
- Implement robust worktree detection using git-dir vs git-common-dir comparison - Add prominent warning when daemon mode is active in a worktree - Warn in 3 places: initial connection, auto-start, and daemon start command - Show shared database path and clarify BEADS_AUTO_START_DAEMON behavior - Document limitations and solutions in README.md and AGENTS.md - Add comprehensive tests for detection and path truncation Fixes #55 Amp-Thread-ID: https://ampcode.com/threads/T-254eb9e3-1a42-42d7-afdf-b7ca2d2dcb8b Co-authored-by: Amp <amp@ampcode.com>
This commit is contained in:
@@ -117,6 +117,19 @@ Use --health to check daemon health and metrics.`,
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Warn if starting daemon in a git worktree
|
||||
if !global {
|
||||
// Ensure dbPath is set for warning
|
||||
if dbPath == "" {
|
||||
if foundDB := beads.FindDatabasePath(); foundDB != "" {
|
||||
dbPath = foundDB
|
||||
}
|
||||
}
|
||||
if dbPath != "" {
|
||||
warnWorktreeDaemon(dbPath)
|
||||
}
|
||||
}
|
||||
|
||||
// Start daemon
|
||||
scope := "local"
|
||||
if global {
|
||||
|
||||
@@ -188,6 +188,8 @@ var rootCmd = &cobra.Command{
|
||||
if os.Getenv("BD_DEBUG") != "" {
|
||||
fmt.Fprintf(os.Stderr, "Debug: connected to daemon at %s (health: %s)\n", socketPath, health.Status)
|
||||
}
|
||||
// Warn if using daemon with git worktrees
|
||||
warnWorktreeDaemon(dbPath)
|
||||
return // Skip direct storage initialization
|
||||
} else {
|
||||
// Health check failed or daemon unhealthy
|
||||
@@ -248,6 +250,8 @@ var rootCmd = &cobra.Command{
|
||||
elapsed := time.Since(startTime).Milliseconds()
|
||||
fmt.Fprintf(os.Stderr, "Debug: auto-start succeeded; connected at %s in %dms\n", socketPath, elapsed)
|
||||
}
|
||||
// Warn if using daemon with git worktrees
|
||||
warnWorktreeDaemon(dbPath)
|
||||
return // Skip direct storage initialization
|
||||
} else {
|
||||
// Auto-started daemon is unhealthy
|
||||
|
||||
93
cmd/bd/worktree.go
Normal file
93
cmd/bd/worktree.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// isGitWorktree detects if the current directory is in a git worktree
|
||||
// by comparing --git-dir and --git-common-dir (canonical detection method)
|
||||
func isGitWorktree() bool {
|
||||
gitDir := gitRevParse("--git-dir")
|
||||
if gitDir == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
commonDir := gitRevParse("--git-common-dir")
|
||||
if commonDir == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
absGit, err1 := filepath.Abs(gitDir)
|
||||
absCommon, err2 := filepath.Abs(commonDir)
|
||||
if err1 != nil || err2 != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return absGit != absCommon
|
||||
}
|
||||
|
||||
// gitRevParse runs git rev-parse with the given flag and returns the trimmed output
|
||||
func gitRevParse(flag string) string {
|
||||
out, err := exec.Command("git", "rev-parse", flag).Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
// getWorktreeGitDir returns the .git directory path for a worktree
|
||||
// Returns empty string if not in a git repo or not a worktree
|
||||
func getWorktreeGitDir() string {
|
||||
cmd := exec.Command("git", "rev-parse", "--git-dir")
|
||||
out, err := cmd.Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(string(out))
|
||||
}
|
||||
|
||||
// warnWorktreeDaemon prints a warning if using daemon with worktrees
|
||||
// Call this only when daemon mode is actually active (connected)
|
||||
func warnWorktreeDaemon(dbPathForWarning string) {
|
||||
if !isGitWorktree() {
|
||||
return
|
||||
}
|
||||
|
||||
gitDir := getWorktreeGitDir()
|
||||
beadsDir := filepath.Dir(dbPathForWarning)
|
||||
if beadsDir == "." || beadsDir == "" {
|
||||
beadsDir = dbPathForWarning
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr)
|
||||
fmt.Fprintln(os.Stderr, "╔══════════════════════════════════════════════════════════════════════════╗")
|
||||
fmt.Fprintln(os.Stderr, "║ WARNING: Git worktree detected with daemon mode ║")
|
||||
fmt.Fprintln(os.Stderr, "╠══════════════════════════════════════════════════════════════════════════╣")
|
||||
fmt.Fprintln(os.Stderr, "║ Git worktrees share the same .beads directory, which can cause the ║")
|
||||
fmt.Fprintln(os.Stderr, "║ daemon to commit/push to the wrong branch. ║")
|
||||
fmt.Fprintln(os.Stderr, "║ ║")
|
||||
fmt.Fprintf(os.Stderr, "║ Shared database: %-55s ║\n", truncateForBox(beadsDir, 55))
|
||||
fmt.Fprintf(os.Stderr, "║ Worktree git dir: %-54s ║\n", truncateForBox(gitDir, 54))
|
||||
fmt.Fprintln(os.Stderr, "║ ║")
|
||||
fmt.Fprintln(os.Stderr, "║ RECOMMENDED SOLUTIONS: ║")
|
||||
fmt.Fprintln(os.Stderr, "║ 1. Use --no-daemon flag: bd --no-daemon <command> ║")
|
||||
fmt.Fprintln(os.Stderr, "║ 2. Disable daemon mode: export BEADS_NO_DAEMON=1 ║")
|
||||
fmt.Fprintln(os.Stderr, "║ ║")
|
||||
fmt.Fprintln(os.Stderr, "║ Note: BEADS_AUTO_START_DAEMON=false only prevents auto-start; ║")
|
||||
fmt.Fprintln(os.Stderr, "║ you can still connect to a running daemon. ║")
|
||||
fmt.Fprintln(os.Stderr, "╚══════════════════════════════════════════════════════════════════════════╝")
|
||||
fmt.Fprintln(os.Stderr)
|
||||
}
|
||||
|
||||
// truncateForBox truncates a path to fit in the warning box
|
||||
func truncateForBox(path string, maxLen int) string {
|
||||
if len(path) <= maxLen {
|
||||
return path
|
||||
}
|
||||
// Truncate with ellipsis
|
||||
return "..." + path[len(path)-(maxLen-3):]
|
||||
}
|
||||
118
cmd/bd/worktree_test.go
Normal file
118
cmd/bd/worktree_test.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsGitWorktree(t *testing.T) {
|
||||
// Save current directory
|
||||
origDir, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.Chdir(origDir)
|
||||
|
||||
// Create a temp directory for our test repo
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Initialize a git repo
|
||||
mainRepo := filepath.Join(tmpDir, "main")
|
||||
if err := os.Mkdir(mainRepo, 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Initialize main git repo
|
||||
if err := os.Chdir(mainRepo); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := exec.Command("git", "init").Run(); err != nil {
|
||||
t.Skip("git not available")
|
||||
}
|
||||
|
||||
if err := exec.Command("git", "config", "user.email", "test@example.com").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := exec.Command("git", "config", "user.name", "Test User").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create a commit
|
||||
readmeFile := filepath.Join(mainRepo, "README.md")
|
||||
if err := os.WriteFile(readmeFile, []byte("# Test\n"), 0644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := exec.Command("git", "add", "README.md").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := exec.Command("git", "commit", "-m", "Initial commit").Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Test 1: Main repo should NOT be a worktree
|
||||
if isGitWorktree() {
|
||||
t.Error("Main repository should not be detected as a worktree")
|
||||
}
|
||||
|
||||
// Create a worktree
|
||||
worktreeDir := filepath.Join(tmpDir, "worktree")
|
||||
if err := exec.Command("git", "worktree", "add", worktreeDir, "-b", "feature").Run(); err != nil {
|
||||
t.Skip("git worktree not available")
|
||||
}
|
||||
|
||||
// Change to worktree directory
|
||||
if err := os.Chdir(worktreeDir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Test 2: Worktree should be detected
|
||||
if !isGitWorktree() {
|
||||
t.Error("Worktree should be detected as a worktree")
|
||||
}
|
||||
|
||||
// Test 3: Verify git-dir != git-common-dir in worktree
|
||||
wtGitDir := gitRevParse("--git-dir")
|
||||
wtCommonDir := gitRevParse("--git-common-dir")
|
||||
if wtGitDir == "" || wtCommonDir == "" {
|
||||
t.Error("git rev-parse should return valid paths in worktree")
|
||||
}
|
||||
if wtGitDir == wtCommonDir {
|
||||
t.Errorf("In worktree, git-dir (%s) should differ from git-common-dir (%s)", wtGitDir, wtCommonDir)
|
||||
}
|
||||
|
||||
// Clean up worktree
|
||||
if err := os.Chdir(mainRepo); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := exec.Command("git", "worktree", "remove", worktreeDir).Run(); err != nil {
|
||||
t.Logf("Warning: failed to clean up worktree: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTruncateForBox(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
path string
|
||||
maxLen int
|
||||
want string
|
||||
}{
|
||||
{"short path", "/home/user", 20, "/home/user"},
|
||||
{"exact length", "/home/user/test", 15, "/home/user/test"},
|
||||
{"long path", "/very/long/path/to/database/file.db", 20, ".../database/file.db"},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := truncateForBox(tt.path, tt.maxLen)
|
||||
if len(got) > tt.maxLen {
|
||||
t.Errorf("truncateForBox() result too long: got %d chars, want <= %d", len(got), tt.maxLen)
|
||||
}
|
||||
if len(tt.path) <= tt.maxLen && got != tt.path {
|
||||
t.Errorf("truncateForBox() shouldn't truncate short paths: got %q, want %q", got, tt.path)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user