fix(git): Handle missing remote.origin.fetch in worktrees (gt-0eh3r)

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 <noreply@anthropic.com>
This commit is contained in:
dementus
2026-01-01 18:15:34 -08:00
committed by Steve Yegge
parent 7552be25e5
commit 03eeb38129

View File

@@ -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: "<sha>\trefs/heads/<branch>")
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