From 03eeb381295931fac56d4a53e49765d9f4514a36 Mon Sep 17 00:00:00 2001 From: dementus Date: Thu, 1 Jan 2026 18:15:34 -0800 Subject: [PATCH] fix(git): Handle missing remote.origin.fetch in worktrees (gt-0eh3r) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When BranchPushedToRemote fails to use the tracking ref (e.g., due to missing remote.origin.fetch config in worktrees), fall back to using git ls-remote to get the remote SHA directly and compare. This makes gt done more resilient in worktrees where the fetch refspec may be incomplete or missing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- internal/git/git.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/internal/git/git.go b/internal/git/git.go index 6cdbdbc6..6b38ddc7 100644 --- a/internal/git/git.go +++ b/internal/git/git.go @@ -761,7 +761,29 @@ func (g *Git) BranchPushedToRemote(localBranch, remote string) (bool, int, error // Check if local is ahead count, err := g.run("rev-list", "--count", remoteBranch+"..HEAD") if err != nil { - return false, 0, fmt.Errorf("counting unpushed commits: %w", err) + // Fallback: If we can't use the tracking ref (possibly missing remote.origin.fetch), + // get the remote commit SHA directly via ls-remote and compare. + // See: gt-0eh3r (gt done fails in worktree with missing remote.origin.fetch config) + remoteSHA, lsErr := g.run("ls-remote", remote, "refs/heads/"+localBranch) + if lsErr != nil { + return false, 0, fmt.Errorf("counting unpushed commits: %w (fallback also failed: %v)", err, lsErr) + } + // Parse SHA from ls-remote output (format: "\trefs/heads/") + remoteSHA = strings.TrimSpace(remoteSHA) + if remoteSHA == "" { + return false, 0, fmt.Errorf("counting unpushed commits: %w (remote branch not found)", err) + } + parts := strings.Fields(remoteSHA) + if len(parts) == 0 { + return false, 0, fmt.Errorf("counting unpushed commits: %w (invalid ls-remote output)", err) + } + remoteSHA = parts[0] + + // Count commits from remote SHA to HEAD + count, err = g.run("rev-list", "--count", remoteSHA+"..HEAD") + if err != nil { + return false, 0, fmt.Errorf("counting unpushed commits (fallback): %w", err) + } } var n int