Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions apps/web/src/components/BranchToolbar.logic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,26 @@ describe("resolveDraftEnvModeAfterBranchChange", () => {
});

describe("resolveBranchToolbarValue", () => {
it("defaults new-worktree mode to current git ref when no explicit base ref is set", () => {
it("shows the setting's base for a new worktree, not the checkout's branch", () => {
expect(
resolveBranchToolbarValue({
envMode: "worktree",
activeWorktreePath: null,
activeThreadBranch: null,
currentGitBranch: "main",
currentGitBranch: "feature/current",
newWorktreeBase: "main",
}),
).toBe("main");
// Not known yet: nothing, rather than the checkout's branch as a guess.
expect(
resolveBranchToolbarValue({
envMode: "worktree",
activeWorktreePath: null,
activeThreadBranch: null,
currentGitBranch: "feature/current",
newWorktreeBase: null,
}),
).toBeNull();
});

it("keeps an explicitly selected worktree base ref", () => {
Expand All @@ -91,6 +102,7 @@ describe("resolveBranchToolbarValue", () => {
activeWorktreePath: null,
activeThreadBranch: "feature/base",
currentGitBranch: "main",
newWorktreeBase: "develop",
}),
).toBe("feature/base");
});
Expand All @@ -102,6 +114,7 @@ describe("resolveBranchToolbarValue", () => {
activeWorktreePath: null,
activeThreadBranch: "feature/base",
currentGitBranch: "main",
newWorktreeBase: null,
}),
).toBe("main");
});
Expand Down
9 changes: 8 additions & 1 deletion apps/web/src/components/BranchToolbar.logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,22 @@ export function resolveDraftEnvModeAfterBranchChange(input: {
return "local";
}

/**
* The branch shown for a thread. A new worktree shows its base: the one
* picked, else the one the "New worktrees from" setting gives
* (`newWorktreeBase`), never the checkout's branch in its place, which the
* worktree won't start from unless the setting says so.
*/
export function resolveBranchToolbarValue(input: {
envMode: EnvMode;
activeWorktreePath: string | null;
activeThreadBranch: string | null;
currentGitBranch: string | null;
newWorktreeBase: string | null;
}): string | null {
const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input;
if (envMode === "worktree" && !activeWorktreePath) {
return activeThreadBranch ?? currentGitBranch;
return activeThreadBranch ?? input.newWorktreeBase;
}
return currentGitBranch ?? activeThreadBranch;
}
Expand Down
53 changes: 36 additions & 17 deletions apps/web/src/components/BranchToolbarBranchSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,41 @@ export function BranchToolbarBranchSelector({
[branchStatusQuery.data?.sourceControlProvider],
);
const SourceControlIcon = sourceControlPresentation.Icon;
// A new worktree starts from the computer's "New worktrees from" branch
// until the user picks another. A thread that already exists holds it
// beside its pending mode rather than recording it as the thread's branch:
// only a pick the user makes is recorded.
const needsWorktreeBase =
effectiveEnvMode === "worktree" &&
!activeWorktreePath &&
(!activeThreadBranch || activeThreadBranchFromSetting);
const newWorktreeBase = useNewWorktreeBaseBranch({
environmentId,
cwd: refsCwd,
currentGitBranch,
currentIsDefault: branchStatusQuery.data?.isDefaultRef === true,
canStartWorktree: !envLocked && !activeWorktreePath,
needed: needsWorktreeBase,
});
const newWorktreeBaseBranch = newWorktreeBase.branch;
const canonicalActiveBranch = resolveBranchToolbarValue({
envMode: effectiveEnvMode,
activeWorktreePath,
activeThreadBranch,
currentGitBranch,
newWorktreeBase: needsWorktreeBase ? newWorktreeBase.expected : null,
});
// The setting's branch is still being looked up and can't be named yet:
// the row says which kind it is rather than guessing a name, or "…" while
// even the setting hasn't arrived.
const pendingWorktreeBaseLabel =
needsWorktreeBase && newWorktreeBase.branch === undefined && newWorktreeBase.expected === null
? newWorktreeBase.from === undefined
? "…"
: newWorktreeBase.from === "current"
? "current branch"
: "default branch"
: null;
const branchNames = useMemo(() => refs.map((refName) => refName.name), [refs]);
const branchByName = useMemo(
() => new Map(refs.map((refName) => [refName.name, refName] as const)),
Expand Down Expand Up @@ -551,21 +580,6 @@ export function BranchToolbarBranchSelector({
runSwitchRef(pending.refName, selectionTarget.checkoutCwd, selectionTarget.nextWorktreePath);
};

// A new worktree starts from the computer's "New worktrees from" branch
// until the user picks another. A thread that already exists holds it
// beside its pending mode rather than recording it as the thread's branch:
// only a pick the user makes is recorded.
const needsWorktreeBase =
effectiveEnvMode === "worktree" &&
!activeWorktreePath &&
(!activeThreadBranch || activeThreadBranchFromSetting);
const newWorktreeBaseBranch = useNewWorktreeBaseBranch({
environmentId,
cwd: refsCwd,
currentGitBranch,
canStartWorktree: !envLocked && !activeWorktreePath,
needed: needsWorktreeBase,
});
useEffect(() => {
if (!needsWorktreeBase || !newWorktreeBaseBranch) {
return;
Expand Down Expand Up @@ -665,11 +679,16 @@ export function BranchToolbarBranchSelector({

const triggerLabel = annotateMissingCheckoutLabel(
presentation === "sentence"
? (resolvedActiveBranch ?? "pick a branch")
? (resolvedActiveBranch ??
(pendingWorktreeBaseLabel === null
? "pick a branch"
: pendingWorktreeBaseLabel === "…"
? "…"
: `the ${pendingWorktreeBaseLabel}`))
: getBranchTriggerLabel({
activeWorktreePath,
effectiveEnvMode,
resolvedActiveBranch,
resolvedActiveBranch: resolvedActiveBranch ?? pendingWorktreeBaseLabel,
}),
selectedCheckoutMissing,
);
Expand Down
9 changes: 7 additions & 2 deletions apps/web/src/components/ChatView.sending.browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,10 @@ describe("ChatView (full app): first sends and worktree setup", () => {
try {
(await waitForButtonByText("Current checkout")).click();
await clickSelectItemContainingText("New worktree");
// Until the list says which branch is the default, the row says that
// much, not the branch the checkout happens to be on.
await waitForButtonByText("From default branch");
expect(findButtonByText("From feature/current")).toBeFalsy();
useComposerDraftStore.getState().setPrompt(THREAD_REF, "Ship it");
await waitForLayout();
(await waitForSendButton()).click();
Expand Down Expand Up @@ -1575,8 +1579,9 @@ describe("ChatView (full app): first sends and worktree setup", () => {
(await waitForSendButton()).click();
await waitForLayout();

// The send has its place while it waits for the setting's branch.
expect((await waitForButtonByText("From main")).disabled).toBe(true);
// The send has its place while it waits for the setting's branch, which
// the row already names (not the checkout's branch, main).
expect((await waitForButtonByText("From develop")).disabled).toBe(true);
releaseSearch();

await vi.waitFor(
Expand Down
57 changes: 50 additions & 7 deletions apps/web/src/hooks/useNewWorktreeBaseBranch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { EnvironmentId } from "@threadlines/contracts";
import { findRefForBranchName, resolveNewWorktreeBaseBranch } from "@threadlines/shared/git";
import type { EnvironmentId, NewWorktreeBaseSettings } from "@threadlines/contracts";
import {
findRefForBranchName,
predictNewWorktreeBaseBranch,
resolveNewWorktreeBaseBranch,
} from "@threadlines/shared/git";
import { type QueryClient, useInfiniteQuery } from "@tanstack/react-query";

import { gitBranchSearchInfiniteQueryOptions } from "../lib/gitReactQuery";
Expand Down Expand Up @@ -50,21 +54,53 @@ export async function fetchNewWorktreeBaseBranch(input: {
/**
* The branch a new worktree starts from until the user picks one: the
* computer's "New worktrees from" setting (Settings › Threads) applied to
* `cwd`'s refs. Undefined until it is `needed` and what it rests on is known
* `cwd`'s refs.
*
* `branch` is undefined until it is `needed` and what it rests on is known
* (the computer's settings, and those refs unless the setting is the current
* branch). A lookup that fails leaves it undefined, so the user picks a base
* branch). A lookup that fails makes it null, so the user picks a base
* rather than getting a fallback the setting didn't ask for. The lookups load
* while the thread only `canStartWorktree`, so switching to a new worktree
* finds the base ready instead of showing another branch first.
*
* `expected` is what to show meanwhile (predictNewWorktreeBaseBranch), and
* `from` the setting's kind, for naming the branch while neither is known.
*/
export function useNewWorktreeBaseBranch(input: {
readonly environmentId: EnvironmentId;
readonly cwd: string | null;
readonly currentGitBranch: string | null;
/** The checkout is on the repository's default branch. */
readonly currentIsDefault: boolean;
readonly canStartWorktree: boolean;
readonly needed: boolean;
}): string | null | undefined {
}): {
readonly branch: string | null | undefined;
readonly expected: string | null;
readonly from: NewWorktreeBaseSettings["from"] | undefined;
} {
const base = useComputerConfig(input.environmentId)?.settings.newWorktreeBase;
const branch = useResolvedNewWorktreeBaseBranch(input, base);
return {
branch,
// A prediction only while the lookup runs: once it has answered, even
// with no branch, its answer stands.
expected:
branch !== undefined
? branch
: predictNewWorktreeBaseBranch({
base,
currentGitBranch: input.currentGitBranch,
currentIsDefault: input.currentIsDefault,
}),
from: base?.from,
};
}

function useResolvedNewWorktreeBaseBranch(
input: Parameters<typeof useNewWorktreeBaseBranch>[0],
base: NewWorktreeBaseSettings | undefined,
): string | null | undefined {
const namedBranch = base?.from === "named" ? base.branch : "";
const load = input.canStartWorktree || input.needed;
// The unfiltered list leads with the current and default branches; a
Expand All @@ -91,8 +127,15 @@ export function useNewWorktreeBaseBranch(input: {
if (base.from === "current" && input.currentGitBranch !== null) {
return input.currentGitBranch;
}
// A lookup still loading, or one that failed without a past answer, says
// nothing about where the branch is: no base yet rather than a fallback.
// A lookup that failed without a past answer finds no base: the user picks
// one rather than getting a fallback the setting didn't ask for.
if (
(listed.data === undefined && listed.isError) ||
(namedBranch.length > 0 && named.data === undefined && named.isError)
) {
return null;
}
// One still loading says nothing about where the branch is yet.
if (listed.data === undefined || (namedBranch.length > 0 && named.data === undefined)) {
return undefined;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/shared/src/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { VcsStatusRemoteResult, VcsStatusResult } from "@threadlines/contra
import { describe, expect, it } from "vite-plus/test";

import {
predictNewWorktreeBaseBranch,
applyGitStatusStreamEvent,
buildSshRemoteUrl,
buildTemporaryWorktreeBranchName,
Expand Down Expand Up @@ -373,6 +374,25 @@ describe("worktreeFolderNameForBranch", () => {
});
});

describe("predictNewWorktreeBaseBranch", () => {
const predict = (
base: Parameters<typeof predictNewWorktreeBaseBranch>[0]["base"],
currentIsDefault: boolean,
) => predictNewWorktreeBaseBranch({ base, currentGitBranch: "feature/x", currentIsDefault });

it("names the branch the setting will give when it is known without the branch list", () => {
expect(predict({ from: "current", branch: "" }, false)).toBe("feature/x");
expect(predict({ from: "named", branch: "develop" }, false)).toBe("develop");
expect(predict({ from: "default", branch: "" }, true)).toBe("feature/x");
});

it("names nothing when only the branch list can say", () => {
// The default branch, while the checkout is on another one.
expect(predict({ from: "default", branch: "" }, false)).toBeNull();
expect(predict(undefined, true)).toBeNull();
});
});

describe("resolveNewWorktreeBaseBranch", () => {
const refs = [
{ name: "threadlines/2d841c42", isDefault: false },
Expand Down
19 changes: 19 additions & 0 deletions packages/shared/src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,25 @@ export function resolveNewWorktreeBaseBranch(input: {
return preferred ?? defaultBranch ?? input.currentBranch;
}

/**
* The branch the "New worktrees from" setting will give, from what is known
* before the branch list loads (which takes seconds on a busy repository):
* the checkout's branch when the setting is the current branch, or when it is
* the default branch and the checkout is on it; the named branch. Null when
* only the list can say.
*/
export function predictNewWorktreeBaseBranch(input: {
readonly base: NewWorktreeBaseSettings | undefined;
readonly currentGitBranch: string | null;
readonly currentIsDefault: boolean;
}): string | null {
const { base, currentGitBranch, currentIsDefault } = input;
if (base === undefined) return null;
if (base.from === "current") return currentGitBranch;
if (base.from === "named" && base.branch.length > 0) return base.branch;
return currentIsDefault ? currentGitBranch : null;
}

export function detectSourceControlProviderFromGitRemoteUrl(
remoteUrl: string,
): SourceControlProviderInfo | null {
Expand Down
Loading