fix(worktree): push to the branch's configured upstream remote, not hard-coded origin

PushEnvironmentBranch was running `git push origin <branch>`, which
breaks for repos whose primary remote isn't called "origin" (fork
workflows that use "upstream", "github", etc.). Read the branch's
configured upstream remote via `git config branch.<branch>.remote`
and fall back to "origin" only when no upstream is set.
This commit is contained in:
José Almeida
2026-05-14 00:05:50 +01:00
parent e65339b068
commit a07b8f1f18
+23 -1
View File
@@ -164,7 +164,12 @@ func (a *environmentDestroyerAdapter) PushEnvironmentBranch(ctx context.Context,
if branch == "" { if branch == "" {
cmd = exec.CommandContext(pushCtx, "git", "push") cmd = exec.CommandContext(pushCtx, "git", "push")
} else { } else {
cmd = exec.CommandContext(pushCtx, "git", "push", "origin", branch) // Prefer the branch's configured upstream remote so this works for
// repos whose primary remote isn't called "origin" (e.g. fork
// workflows with "upstream"/"github"). Fall back to "origin" only
// when no upstream is set, matching the historical behaviour.
remote := detectBranchRemote(pushCtx, env.WorktreePath, branch)
cmd = exec.CommandContext(pushCtx, "git", "push", remote, branch)
} }
cmd.Dir = env.WorktreePath cmd.Dir = env.WorktreePath
// Disable interactive credential prompts — without this, a missing // Disable interactive credential prompts — without this, a missing
@@ -180,3 +185,20 @@ func (a *environmentDestroyerAdapter) PushEnvironmentBranch(ctx context.Context,
} }
return nil return nil
} }
// detectBranchRemote reads the configured upstream remote for `branch` from
// the worktree's git config. Falls back to "origin" when no upstream is set
// (matching the historical hard-coded behaviour).
func detectBranchRemote(ctx context.Context, dir, branch string) string {
cmd := exec.CommandContext(ctx, "git", "config", "--get", "branch."+branch+".remote")
cmd.Dir = dir
out, err := cmd.Output()
if err != nil {
return "origin"
}
remote := strings.TrimSpace(string(out))
if remote == "" {
return "origin"
}
return remote
}