fix(daemon): check sync-branch upstream for jj/jujutsu compatibility
When sync-branch is configured, check that branch's upstream instead of current HEAD's upstream. This fixes --auto-push with jj/jujutsu which always operates in detached HEAD mode. Adds gitBranchHasUpstream(branch) to check specific branch's upstream tracking, independent of current HEAD state. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,7 @@ import (
|
|||||||
"github.com/steveyegge/beads/internal/daemon"
|
"github.com/steveyegge/beads/internal/daemon"
|
||||||
"github.com/steveyegge/beads/internal/rpc"
|
"github.com/steveyegge/beads/internal/rpc"
|
||||||
"github.com/steveyegge/beads/internal/storage/sqlite"
|
"github.com/steveyegge/beads/internal/storage/sqlite"
|
||||||
|
"github.com/steveyegge/beads/internal/syncbranch"
|
||||||
)
|
)
|
||||||
|
|
||||||
var daemonCmd = &cobra.Command{
|
var daemonCmd = &cobra.Command{
|
||||||
@@ -173,10 +174,22 @@ Run 'bd daemon' with no flags to see available options.`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check for upstream if auto-push enabled
|
// Check for upstream if auto-push enabled
|
||||||
if autoPush && !gitHasUpstream() {
|
// When sync-branch is configured, check that branch's upstream instead of current HEAD.
|
||||||
fmt.Fprintf(os.Stderr, "Error: no upstream configured (required for --auto-push)\n")
|
// This fixes compatibility with jj/jujutsu which always operates in detached HEAD mode.
|
||||||
fmt.Fprintf(os.Stderr, "Hint: git push -u origin <branch-name>\n")
|
if autoPush {
|
||||||
os.Exit(1)
|
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 <branch-name>\n")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn if starting daemon in a git worktree
|
// Warn if starting daemon in a git worktree
|
||||||
|
|||||||
@@ -57,9 +57,17 @@ func gitHasUpstream() bool {
|
|||||||
}
|
}
|
||||||
branch := strings.TrimSpace(string(branchOutput))
|
branch := strings.TrimSpace(string(branchOutput))
|
||||||
|
|
||||||
// Check if remote and merge refs are configured
|
return gitBranchHasUpstream(branch)
|
||||||
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
|
|
||||||
|
// 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()
|
remoteErr := remoteCmd.Run()
|
||||||
mergeErr := mergeCmd.Run()
|
mergeErr := mergeCmd.Run()
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -98,3 +101,129 @@ 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)")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user