diff --git a/internal/cmd/done.go b/internal/cmd/done.go index 45f55f77..c841e63c 100644 --- a/internal/cmd/done.go +++ b/internal/cmd/done.go @@ -164,13 +164,16 @@ func runDone(cmd *cobra.Command, args []string) error { return fmt.Errorf("branch has %d unpushed commit(s); run 'git push -u origin %s' first", unpushedCount, branch) } - // Check that branch has commits ahead of main (prevents submitting stale branches) - aheadCount, err := g.CommitsAhead("main", branch) + // Detect the repo's default branch (main vs master) + defaultBranch := g.RemoteDefaultBranch() + + // Check that branch has commits ahead of default branch (prevents submitting stale branches) + aheadCount, err := g.CommitsAhead(defaultBranch, branch) if err != nil { - return fmt.Errorf("checking commits ahead of main: %w", err) + return fmt.Errorf("checking commits ahead of %s: %w", defaultBranch, err) } if aheadCount == 0 { - return fmt.Errorf("branch '%s' has 0 commits ahead of main; nothing to merge", branch) + return fmt.Errorf("branch '%s' has 0 commits ahead of %s; nothing to merge", branch, defaultBranch) } if issueID == "" { @@ -181,7 +184,7 @@ func runDone(cmd *cobra.Command, args []string) error { bd := beads.New(cwd) // Determine target branch (auto-detect integration branch if applicable) - target := "main" + target := defaultBranch autoTarget, err := detectIntegrationBranch(bd, g, issueID) if err == nil && autoTarget != "" { target = autoTarget diff --git a/internal/git/git.go b/internal/git/git.go index e04d9998..28ff3ac5 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -255,6 +255,36 @@ func (g *Git) DefaultBranch() string { return "main" } +// RemoteDefaultBranch returns the default branch from the remote (origin). +// This is useful in worktrees where HEAD may not reflect the repo's actual default. +// Checks origin/HEAD first, then falls back to checking if master/main exists. +// Returns "main" as final fallback. +func (g *Git) RemoteDefaultBranch() string { + // Try to get from origin/HEAD symbolic ref + out, err := g.run("symbolic-ref", "refs/remotes/origin/HEAD") + if err == nil && out != "" { + // Returns refs/remotes/origin/main -> extract branch name + parts := strings.Split(out, "/") + if len(parts) > 0 { + return parts[len(parts)-1] + } + } + + // Fallback: check if origin/master exists + _, err = g.run("rev-parse", "--verify", "origin/master") + if err == nil { + return "master" + } + + // Fallback: check if origin/main exists + _, err = g.run("rev-parse", "--verify", "origin/main") + if err == nil { + return "main" + } + + return "main" // final fallback +} + // HasUncommittedChanges returns true if there are uncommitted changes. func (g *Git) HasUncommittedChanges() (bool, error) { status, err := g.Status()