Show origin ref in branch trigger label (#4680)

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex authored and GitHub committed 2026-07-27 23:13:11 +02:00
1 parent 3957a95818
commit a2ffb122e2
3 files changed
+150 -16

No files matched your search

@@ -9,6 +9,7 @@ import {
resolveDraftEnvModeAfterBranchChange,
resolveEffectiveEnvMode,
resolveEnvModeLabel,
resolveBranchTriggerLabel,
resolveBranchToolbarPrBranch,
resolveBranchToolbarValue,
resolveLockedWorkspaceLabel,
@@ -174,6 +175,101 @@ describe("resolveBranchToolbarValue", () => {
});
});
describe("resolveBranchTriggerLabel", () => {
it("shows the origin ref when a new worktree will start from origin", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: "main",
resolvedActiveBranchIsRemote: false,
startFromOrigin: true,
}),
).toBe("From origin/main");
});
it("shows the origin ref for local branch names that contain slashes", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: "feature/demo",
resolvedActiveBranchIsRemote: false,
startFromOrigin: true,
}),
).toBe("From origin/feature/demo");
});
it("shows the local ref when start from origin is disabled", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: "main",
resolvedActiveBranchIsRemote: false,
startFromOrigin: false,
}),
).toBe("From main");
});
it("does not duplicate the origin prefix for an explicit remote ref", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: "origin/feature/demo",
resolvedActiveBranchIsRemote: true,
startFromOrigin: true,
}),
).toBe("From origin/feature/demo");
});
it("preserves an explicit ref from a non-origin remote", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: "upstream/feature/demo",
resolvedActiveBranchIsRemote: true,
startFromOrigin: true,
}),
).toBe("From upstream/feature/demo");
});
it("keeps current-checkout labels and empty state unchanged", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "local",
resolvedActiveBranch: "main",
resolvedActiveBranchIsRemote: false,
startFromOrigin: true,
}),
).toBe("main");
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: null,
resolvedActiveBranchIsRemote: null,
startFromOrigin: true,
}),
).toBe("Select ref");
});
it("does not fabricate an origin ref while branch metadata is loading", () => {
expect(
resolveBranchTriggerLabel({
activeWorktreePath: null,
effectiveEnvMode: "worktree",
resolvedActiveBranch: "upstream/feature/demo",
resolvedActiveBranchIsRemote: null,
startFromOrigin: true,
}),
).toBe("From upstream/feature/demo");
});
});
describe("resolveBranchToolbarPrBranch", () => {
it("uses the explicit thread branch when it matches the displayed branch", () => {
expect(
@@ -156,6 +156,33 @@ export function resolveBranchToolbarValue(input: {
return currentGitBranch ?? activeThreadBranch;
}
export function resolveBranchTriggerLabel(input: {
activeWorktreePath: string | null;
effectiveEnvMode: EnvMode;
resolvedActiveBranch: string | null;
resolvedActiveBranchIsRemote: boolean | null;
startFromOrigin: boolean;
}): string {
const {
activeWorktreePath,
effectiveEnvMode,
resolvedActiveBranch,
resolvedActiveBranchIsRemote,
startFromOrigin,
} = input;
if (!resolvedActiveBranch) {
return "Select ref";
}
if (effectiveEnvMode === "worktree" && !activeWorktreePath) {
const baseRef =
startFromOrigin && resolvedActiveBranchIsRemote === false
? `origin/${resolvedActiveBranch}`
: resolvedActiveBranch;
return `From ${baseRef}`;
}
return resolvedActiveBranch;
}
export function resolveBranchToolbarPrBranch(input: {
activeThreadBranch: string | null;
resolvedActiveBranch: string | null;
@@ -35,6 +35,7 @@ import { parsePullRequestReference } from "../pullRequestReference";
import { getSourceControlPresentation } from "../sourceControlPresentation";
import {
deriveLocalBranchNameFromRemoteRef,
resolveBranchTriggerLabel,
resolveBranchToolbarPrBranch,
resolveBranchSelectionTarget,
resolveBranchToolbarValue,
@@ -81,21 +82,6 @@ function toBranchActionErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : "An error occurred.";
}
function getBranchTriggerLabel(input: {
activeWorktreePath: string | null;
effectiveEnvMode: "local" | "worktree";
resolvedActiveBranch: string | null;
}): string {
const { activeWorktreePath, effectiveEnvMode, resolvedActiveBranch } = input;
if (!resolvedActiveBranch) {
return "Select ref";
}
if (effectiveEnvMode === "worktree" && !activeWorktreePath) {
return `From ${resolvedActiveBranch}`;
}
return resolvedActiveBranch;
}
export function BranchToolbarBranchSelector({
className,
environmentId,
@@ -308,6 +294,29 @@ export function BranchToolbarBranchSelector({
canonicalActiveBranch,
(_currentBranch: string | null, optimisticBranch: string | null) => optimisticBranch,
);
const listedActiveBranch =
resolvedActiveBranch === null ? null : (branchByName.get(resolvedActiveBranch) ?? null);
const activeBranchRefQuery = useEnvironmentQuery(
branchCwd !== null && resolvedActiveBranch !== null
? vcsEnvironment.listRefs({
environmentId,
input: {
cwd: branchCwd,
query: resolvedActiveBranch,
limit: 10,
},
})
: null,
);
const queriedActiveBranch = activeBranchRefQuery.data?.refs.find(
(refName) => refName.name === resolvedActiveBranch,
);
const resolvedActiveBranchIsRemote =
listedActiveBranch !== null
? listedActiveBranch.isRemote === true
: queriedActiveBranch
? queriedActiveBranch.isRemote === true
: null;
const [isBranchActionPending, startBranchActionTransition] = useTransition();
const totalBranchCount = branchRefState.data?.totalCount ?? 0;
const branchStatusText = isInitialBranchesLoadPending
@@ -584,10 +593,12 @@ export function BranchToolbarBranchSelector({
maybeFetchNextBranchPage();
}, [refs.length, maybeFetchNextBranchPage]);
const triggerLabel = getBranchTriggerLabel({
const triggerLabel = resolveBranchTriggerLabel({
activeWorktreePath,
effectiveEnvMode,
resolvedActiveBranch,
resolvedActiveBranchIsRemote,
startFromOrigin,
});
// PR pill shown next to the branch selector when the active branch has one.