|
Expand Up
|
@@ -20,6 +20,7 @@ import { |
|
|
useEffectEvent, |
|
|
useImperativeHandle, |
|
|
useOptimistic, |
|
|
useRef, |
|
|
useState, |
|
|
useTransition, |
|
|
type MouseEvent as ReactMouseEvent, |
|
Expand Down
Expand Up
|
@@ -53,6 +54,7 @@ import { |
|
|
resolveBranchToolbarPrBranch, |
|
|
resolveBranchSelectionTarget, |
|
|
resolveBranchToolbarValue, |
|
|
resolveConfirmedSelectedRef, |
|
|
resolveDefaultWorktreeBaseBranch, |
|
|
resolveDraftEnvModeAfterBranchChange, |
|
|
resolveEffectiveEnvMode, |
|
Expand Down
Expand Up
|
@@ -130,6 +132,9 @@ export function BranchToolbarBranchSelector({ |
|
|
const switchRef = useAtomCommand(vcsEnvironment.switchRef, { |
|
|
reportFailure: false, |
|
|
}); |
|
|
const readRefs = useAtomCommand(vcsEnvironment.readRefs, { |
|
|
reportFailure: false, |
|
|
}); |
|
|
const refreshLocalStatusForPicker = useAtomCommand(vcsEnvironment.refreshLocalStatusForPicker, { |
|
|
reportFailure: false, |
|
|
}); |
|
Expand Down
Expand Up
|
@@ -423,17 +428,40 @@ export function BranchToolbarBranchSelector({ |
|
|
}); |
|
|
}; |
|
|
|
|
|
const selectBranch = (refName: VcsRef) => { |
|
|
// The picker's list, and the server's coalesced ref listing, can predate a checkout an agent |
|
|
// ran itself. Only the claimed worktree's live HEAD confirms the claim; an unconfirmed one |
|
|
// falls back to a real checkout, which Git refuses if the branch is checked out elsewhere. |
|
|
const latestSelectionIdRef = useRef(0); |
|
|
const readSelectedRef = async (worktreePath: string, listedRef: VcsRef): Promise<VcsRef> => { |
|
|
const result = await readRefs({ |
|
|
environmentId, |
|
|
input: { cwd: worktreePath, query: listedRef.name, refKind: "local", limit: 1 }, |
|
|
}); |
|
|
return resolveConfirmedSelectedRef(listedRef, result._tag === "Success" ? result.value : null); |
|
|
}; |
|
|
|
|
|
const selectBranch = async (listedRef: VcsRef) => { |
|
|
if (!canUpdateThreadBranch || !branchCwd || !activeProjectCwd || isBranchActionPending) return; |
|
|
const selectionId = ++latestSelectionIdRef.current; |
|
|
|
|
|
const latest = readLatestThreadState(); |
|
|
let latest = readLatestThreadState(); |
|
|
if (isSelectingWorktreeBase) { |
|
|
if (latest.worktreePath) return; |
|
|
setThreadBranch(refName.name, null); |
|
|
setThreadBranch(listedRef.name, null); |
|
|
setIsBranchMenuOpen(false); |
|
|
onComposerFocusRequest?.(); |
|
|
return; |
|
|
} |
|
|
|
|
|
setIsBranchMenuOpen(false); |
|
|
onComposerFocusRequest?.(); |
|
|
|
|
|
const refName = listedRef.worktreePath |
|
|
? await readSelectedRef(listedRef.worktreePath, listedRef) |
|
|
: listedRef; |
|
|
// A later pick made while this one was confirming its worktree wins. |
|
|
if (selectionId !== latestSelectionIdRef.current) return; |
|
|
latest = readLatestThreadState(); |
|
|
const selectionTarget = resolveBranchSelectionTarget({ |
|
|
activeProjectCwd, |
|
|
activeProjectCheckoutRoot, |
|
Expand All
|
@@ -455,18 +483,13 @@ export function BranchToolbarBranchSelector({ |
|
|
!(selectionTarget.staysInActiveCheckout && canWriteSourceControl) |
|
|
) { |
|
|
setThreadBranch(refName.name, selectionTarget.nextWorktreePath); |
|
|
setIsBranchMenuOpen(false); |
|
|
onComposerFocusRequest?.(); |
|
|
return; |
|
|
} |
|
|
|
|
|
const selectedBranchName = refName.isRemote |
|
|
? deriveLocalBranchNameFromRemoteRef(refName.name) |
|
|
: refName.name; |
|
|
|
|
|
setIsBranchMenuOpen(false); |
|
|
onComposerFocusRequest?.(); |
|
|
|
|
|
runBranchAction(async () => { |
|
|
const previousBranch = resolvedActiveBranch; |
|
|
setOptimisticBranch(selectedBranchName); |
|
Expand Down
Expand Up
|
@@ -531,6 +554,7 @@ export function BranchToolbarBranchSelector({ |
|
|
if (!canChangeThreadBranch) return; |
|
|
const name = sanitizeNewRefName(rawName); |
|
|
if (!branchCwd || !name || isBranchActionPending) return; |
|
|
latestSelectionIdRef.current++; |
|
|
|
|
|
setIsBranchMenuOpen(false); |
|
|
onComposerFocusRequest?.(); |
|
Expand Down
Expand Up
|
@@ -667,14 +691,15 @@ export function BranchToolbarBranchSelector({ |
|
|
|
|
|
function selectPickerItem(itemValue: string) { |
|
|
if (itemValue === checkoutPullRequestItemValue && prReference && onCheckoutPullRequestRequest) { |
|
|
latestSelectionIdRef.current++; |
|
|
handleOpenChange(false); |
|
|
onComposerFocusRequest?.(); |
|
|
onCheckoutPullRequestRequest(prReference); |
|
|
} else if (itemValue === createBranchItemValue) { |
|
|
createRef(trimmedBranchQuery); |
|
|
} else { |
|
|
const refName = branchByName.get(itemValue); |
|
|
if (refName) selectBranch(refName); |
|
|
if (refName) void selectBranch(refName); |
|
|
} |
|
|
} |
|
|
|
|
Expand Down
|
|