diff --git a/cmd/bd/daemon.go b/cmd/bd/daemon.go index ae4ed2f5..e80ee1ef 100644 --- a/cmd/bd/daemon.go +++ b/cmd/bd/daemon.go @@ -17,6 +17,7 @@ import ( "github.com/steveyegge/beads/internal/daemon" "github.com/steveyegge/beads/internal/rpc" "github.com/steveyegge/beads/internal/storage/sqlite" + "github.com/steveyegge/beads/internal/syncbranch" ) var daemonCmd = &cobra.Command{ @@ -194,10 +195,22 @@ Run 'bd daemon --help' to see all subcommands.`, } // Check for upstream if auto-push enabled - if autoPush && !gitHasUpstream() { - fmt.Fprintf(os.Stderr, "Error: no upstream configured (required for --auto-push)\n") - fmt.Fprintf(os.Stderr, "Hint: git push -u origin \n") - os.Exit(1) + // When sync-branch is configured, check that branch's upstream instead of current HEAD. + // This fixes compatibility with jj/jujutsu which always operates in detached HEAD mode. + if autoPush { + hasUpstream := false + if syncBranch := syncbranch.GetFromYAML(); syncBranch != "" { + // sync-branch configured: check that branch's upstream + hasUpstream = gitBranchHasUpstream(syncBranch) + } else { + // No sync-branch: check current HEAD's upstream (original behavior) + hasUpstream = gitHasUpstream() + } + if !hasUpstream { + fmt.Fprintf(os.Stderr, "Error: no upstream configured (required for --auto-push)\n") + fmt.Fprintf(os.Stderr, "Hint: git push -u origin \n") + os.Exit(1) + } } // Warn if starting daemon in a git worktree diff --git a/cmd/bd/sync_branch.go b/cmd/bd/sync_branch.go index a82d3afa..ffd376e2 100644 --- a/cmd/bd/sync_branch.go +++ b/cmd/bd/sync_branch.go @@ -22,6 +22,18 @@ func getCurrentBranch(ctx context.Context) (string, error) { return strings.TrimSpace(string(output)), nil } +// getCurrentBranchOrHEAD returns the current branch name, or "HEAD" if in detached HEAD state. +// This is useful for jj/jujutsu compatibility where HEAD is always detached but we still +// need a reference for git operations like log and diff. +func getCurrentBranchOrHEAD(ctx context.Context) (string, error) { + branch, err := getCurrentBranch(ctx) + if err != nil { + // Detached HEAD - return "HEAD" as the reference + return "HEAD", nil + } + return branch, nil +} + // getSyncBranch returns the configured sync branch name func getSyncBranch(ctx context.Context) (string, error) { // Ensure store is initialized @@ -47,7 +59,7 @@ func showSyncStatus(ctx context.Context) error { return fmt.Errorf("not in a git repository") } - currentBranch, err := getCurrentBranch(ctx) + currentBranch, err := getCurrentBranchOrHEAD(ctx) if err != nil { return err } diff --git a/cmd/bd/sync_git.go b/cmd/bd/sync_git.go index 1f534dbe..ee9e7f17 100644 --- a/cmd/bd/sync_git.go +++ b/cmd/bd/sync_git.go @@ -57,9 +57,17 @@ func gitHasUpstream() bool { } branch := strings.TrimSpace(string(branchOutput)) - // Check if remote and merge refs are configured - remoteCmd := exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) //nolint:gosec // G204: branch from git symbolic-ref - mergeCmd := exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.merge", branch)) //nolint:gosec // G204: branch from git symbolic-ref + return gitBranchHasUpstream(branch) +} + +// gitBranchHasUpstream checks if a specific branch has an upstream configured. +// Unlike gitHasUpstream(), this works even when HEAD is detached (e.g., jj/jujutsu). +// This is critical for sync-branch workflows where the sync branch has upstream +// tracking but the main working copy may be in detached HEAD state. +func gitBranchHasUpstream(branch string) bool { + // Check if remote and merge refs are configured for the branch + remoteCmd := exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.remote", branch)) //nolint:gosec // G204: branch from caller + mergeCmd := exec.Command("git", "config", "--get", fmt.Sprintf("branch.%s.merge", branch)) //nolint:gosec // G204: branch from caller remoteErr := remoteCmd.Run() mergeErr := mergeCmd.Run() diff --git a/cmd/bd/sync_git_test.go b/cmd/bd/sync_git_test.go index 412acaf0..2bbecce9 100644 --- a/cmd/bd/sync_git_test.go +++ b/cmd/bd/sync_git_test.go @@ -428,3 +428,209 @@ func TestParseGitStatusForBeadsChanges(t *testing.T) { }) } } + +// TestGitBranchHasUpstream tests the gitBranchHasUpstream function +// which checks if a specific branch (not current HEAD) has upstream configured. +// This is critical for jj/jujutsu compatibility where HEAD is always detached +// but the sync-branch may have proper upstream tracking. +func TestGitBranchHasUpstream(t *testing.T) { + // Create temp directory for test repos + tmpDir, err := os.MkdirTemp("", "beads-upstream-test-*") + if err != nil { + t.Fatalf("Failed to create temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Create a bare "remote" repo + remoteDir := filepath.Join(tmpDir, "remote.git") + if err := exec.Command("git", "init", "--bare", remoteDir).Run(); err != nil { + t.Fatalf("Failed to create bare repo: %v", err) + } + + // Create local repo + localDir := filepath.Join(tmpDir, "local") + if err := os.MkdirAll(localDir, 0755); err != nil { + t.Fatalf("Failed to create local dir: %v", err) + } + + // Initialize and configure local repo + cmds := [][]string{ + {"git", "init", "-b", "main"}, + {"git", "config", "user.email", "test@test.com"}, + {"git", "config", "user.name", "Test"}, + {"git", "remote", "add", "origin", remoteDir}, + } + for _, args := range cmds { + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = localDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to run %v: %v\n%s", args, err, out) + } + } + + // Create initial commit on main + testFile := filepath.Join(localDir, "test.txt") + if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { + t.Fatalf("Failed to create test file: %v", err) + } + cmds = [][]string{ + {"git", "add", "test.txt"}, + {"git", "commit", "-m", "initial"}, + {"git", "push", "-u", "origin", "main"}, + } + for _, args := range cmds { + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = localDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to run %v: %v\n%s", args, err, out) + } + } + + // Create beads-sync branch with upstream + cmds = [][]string{ + {"git", "checkout", "-b", "beads-sync"}, + {"git", "push", "-u", "origin", "beads-sync"}, + } + for _, args := range cmds { + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = localDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to run %v: %v\n%s", args, err, out) + } + } + + // Save current dir and change to local repo + origDir, _ := os.Getwd() + if err := os.Chdir(localDir); err != nil { + t.Fatalf("Failed to chdir: %v", err) + } + defer os.Chdir(origDir) + + // Test 1: beads-sync branch should have upstream + t.Run("branch with upstream returns true", func(t *testing.T) { + if !gitBranchHasUpstream("beads-sync") { + t.Error("gitBranchHasUpstream('beads-sync') = false, want true") + } + }) + + // Test 2: non-existent branch should return false + t.Run("non-existent branch returns false", func(t *testing.T) { + if gitBranchHasUpstream("no-such-branch") { + t.Error("gitBranchHasUpstream('no-such-branch') = true, want false") + } + }) + + // Test 3: Simulate jj detached HEAD - beads-sync should still work + t.Run("works with detached HEAD (jj scenario)", func(t *testing.T) { + // Detach HEAD (simulating jj's behavior) + cmd := exec.Command("git", "checkout", "--detach", "HEAD") + cmd.Dir = localDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to detach HEAD: %v\n%s", err, out) + } + + // gitHasUpstream() should fail (detached HEAD) + if gitHasUpstream() { + t.Error("gitHasUpstream() = true with detached HEAD, want false") + } + + // But gitBranchHasUpstream("beads-sync") should still work + if !gitBranchHasUpstream("beads-sync") { + t.Error("gitBranchHasUpstream('beads-sync') = false with detached HEAD, want true") + } + }) + + // Test 4: branch without upstream should return false + t.Run("branch without upstream returns false", func(t *testing.T) { + // Create a local-only branch (no upstream) + cmd := exec.Command("git", "checkout", "-b", "local-only") + cmd.Dir = localDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to create local branch: %v\n%s", err, out) + } + + if gitBranchHasUpstream("local-only") { + t.Error("gitBranchHasUpstream('local-only') = true, want false (no upstream)") + } + }) +} + +// TestGetCurrentBranchOrHEAD tests getCurrentBranchOrHEAD which returns "HEAD" +// when in detached HEAD state (e.g., jj/jujutsu) instead of failing. +func TestGetCurrentBranchOrHEAD(t *testing.T) { + ctx := context.Background() + + // Create temp directory for test repo + tmpDir, err := os.MkdirTemp("", "beads-branch-test-*") + if err != nil { + t.Fatalf("Failed to create temp dir: %v", err) + } + defer os.RemoveAll(tmpDir) + + // Initialize git repo + cmds := [][]string{ + {"git", "init", "-b", "main"}, + {"git", "config", "user.email", "test@test.com"}, + {"git", "config", "user.name", "Test"}, + } + for _, args := range cmds { + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = tmpDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to run %v: %v\n%s", args, err, out) + } + } + + // Create initial commit + testFile := filepath.Join(tmpDir, "test.txt") + if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil { + t.Fatalf("Failed to create test file: %v", err) + } + cmds = [][]string{ + {"git", "add", "test.txt"}, + {"git", "commit", "-m", "initial"}, + } + for _, args := range cmds { + cmd := exec.Command(args[0], args[1:]...) + cmd.Dir = tmpDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to run %v: %v\n%s", args, err, out) + } + } + + // Save current dir and change to test repo + origDir, _ := os.Getwd() + if err := os.Chdir(tmpDir); err != nil { + t.Fatalf("Failed to chdir: %v", err) + } + defer os.Chdir(origDir) + + // Test 1: Normal branch returns branch name + t.Run("returns branch name when on branch", func(t *testing.T) { + branch, err := getCurrentBranchOrHEAD(ctx) + if err != nil { + t.Errorf("getCurrentBranchOrHEAD() error = %v", err) + } + if branch != "main" { + t.Errorf("getCurrentBranchOrHEAD() = %q, want %q", branch, "main") + } + }) + + // Test 2: Detached HEAD returns "HEAD" + t.Run("returns HEAD when detached", func(t *testing.T) { + // Detach HEAD + cmd := exec.Command("git", "checkout", "--detach", "HEAD") + cmd.Dir = tmpDir + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to detach HEAD: %v\n%s", err, out) + } + + branch, err := getCurrentBranchOrHEAD(ctx) + if err != nil { + t.Errorf("getCurrentBranchOrHEAD() error = %v", err) + } + if branch != "HEAD" { + t.Errorf("getCurrentBranchOrHEAD() = %q, want %q", branch, "HEAD") + } + }) +}