Skip to content

Commit 5661310

Browse files
committed
feat(settings): choose the branch new worktrees start from
Projects whose work branches off develop had every new worktree start from main. Add a project-scoped 'Worktree base branch' setting. Empty keeps the repository default branch (origin/HEAD).
1 parent 618761a commit 5661310

11 files changed

Lines changed: 209 additions & 3 deletions

File tree

‎apps/mobile/src/features/settings/SettingsServerControlsRouteScreen.tsx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from "./components/SettingsEnvironmentFilterHeader";
2626
import { BranchNamingSettings } from "./components/BranchNamingSettings";
2727
import { DatabaseStorageNotice } from "./components/DatabaseStorageNotice";
28+
import { WorktreeBaseBranchField } from "./components/WorktreeBaseBranchField";
2829
import { SettingsChoiceRow } from "./components/SettingsChoiceRow";
2930
import { SettingsSection } from "./components/SettingsSection";
3031
import { SettingsSwitchRow } from "./components/SettingsSwitchRow";
@@ -55,6 +56,7 @@ const PAGE_PROJECT_KEYS: Record<SettingsPage, readonly ProjectScopedServerSettin
5556
"defaultAutoPull",
5657
"removeAgentCreditsOnMerge",
5758
"newWorktreesStartFromOrigin",
59+
"newWorktreesBaseBranch",
5860
"branchNamingMode",
5961
"branchNamePrefix",
6062
"branchNameInstructions",
@@ -393,6 +395,11 @@ function ServerSettingsDetail(props: { readonly page: SettingsPage }) {
393395
disabled={disabledFor("newWorktreesStartFromOrigin")}
394396
onValueChange={(value) => write({ newWorktreesStartFromOrigin: value })}
395397
/>
398+
<WorktreeBaseBranchField
399+
value={uniform("newWorktreesBaseBranch")}
400+
disabled={disabledFor("newWorktreesBaseBranch")}
401+
onChange={write}
402+
/>
396403
</SettingsSection>
397404
</>
398405
) : null}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { ServerSettingsPatch } from "@t3tools/contracts";
2+
import { useEffect, useEffectEvent, useRef } from "react";
3+
import { View } from "react-native";
4+
5+
import { AppText as Text, AppTextInput } from "../../../components/ui/AppText";
6+
7+
/** Base branch for new worktrees. `null` means the selected targets differ. */
8+
export function WorktreeBaseBranchField(props: {
9+
value: string | null;
10+
disabled: boolean;
11+
onChange: (patch: ServerSettingsPatch) => void;
12+
}) {
13+
// Typed text waits here until it saves. Android's Back closes the keyboard
14+
// without ending editing, so leaving the screen saves it too.
15+
const draft = useRef<{ text: string; over: string | null } | null>(null);
16+
// A newer saved value remounts the input, so a draft typed over an older
17+
// one is no longer on screen and is dropped.
18+
const save = () => {
19+
const current = draft.current;
20+
draft.current = null;
21+
if (current === null || current.over !== props.value) return;
22+
const value = current.text.trim();
23+
if (props.value === null || value !== props.value) {
24+
props.onChange({ newWorktreesBaseBranch: value });
25+
}
26+
};
27+
const saveOnLeave = useEffectEvent(save);
28+
useEffect(() => () => saveOnLeave(), []);
29+
return (
30+
<View className="gap-2 px-4 py-3">
31+
<Text className="text-sm text-foreground-muted">
32+
New worktrees start from this branch. Leave empty to use the default branch.
33+
</Text>
34+
<AppTextInput
35+
key={props.value}
36+
accessibilityLabel="Worktree base branch"
37+
onChangeText={(text) => {
38+
draft.current = { text, over: props.value };
39+
}}
40+
defaultValue={props.value ?? ""}
41+
placeholder={props.value === null ? "Mixed" : "Default branch"}
42+
editable={!props.disabled}
43+
autoCapitalize="none"
44+
autoCorrect={false}
45+
className="min-h-10 rounded-xl px-3 py-2 text-base text-foreground"
46+
onEndEditing={save}
47+
/>
48+
</View>
49+
);
50+
}

‎apps/mobile/src/features/threads/new-task-flow-provider.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,9 +892,14 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
892892
if (live && (live.mode !== "worktree" || live.branch !== null)) {
893893
return;
894894
}
895-
// The default may only exist as origin/<default> (isRemote), which
896-
// availableBranches filters out — search the unfiltered refs for it.
895+
// The project's configured base branch wins. The default may only exist
896+
// as origin/<default> (isRemote), which availableBranches filters out —
897+
// search the unfiltered refs for it.
898+
const configuredBaseBranch = projectSettings.settings.newWorktreesBaseBranch;
897899
const preferredBranch =
900+
(configuredBaseBranch.length > 0
901+
? allBranchRefs.find((branch) => branch.name === configuredBaseBranch)
902+
: undefined) ??
898903
allBranchRefs.find((branch) => branch.isDefault) ??
899904
availableBranches.find((branch) => branch.current) ??
900905
null;
@@ -905,6 +910,7 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) {
905910
allBranchRefs,
906911
availableBranches,
907912
defaultWorkspaceModeSettled,
913+
projectSettings.settings.newWorktreesBaseBranch,
908914
selectBranch,
909915
selectedBranchName,
910916
selectedProjectDraftKey,

‎apps/web/src/components/BranchToolbar.logic.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
resolveDraftEnvModeAfterBranchChange,
88
resolveEffectiveEnvMode,
99
resolveBranchToolbarPrBranch,
10+
resolveWorktreeBaseBranch,
1011
resolveLocalCheckoutBranchMismatch,
1112
sanitizeNewRefName,
1213
shouldIncludeBranchPickerItem,
@@ -254,3 +255,32 @@ describe("sanitizeNewRefName", () => {
254255
expect(sanitizeNewRefName("foo--bar")).toBe("foo--bar");
255256
});
256257
});
258+
259+
describe("resolveWorktreeBaseBranch", () => {
260+
it("prefers the project's configured base branch over the repo default", () => {
261+
expect(
262+
resolveWorktreeBaseBranch({
263+
configuredBaseBranch: "develop",
264+
defaultBranch: "main",
265+
currentGitBranch: "feature/x",
266+
}),
267+
).toBe("develop");
268+
});
269+
270+
it("falls back to the repo default, then the checked-out branch", () => {
271+
expect(
272+
resolveWorktreeBaseBranch({
273+
configuredBaseBranch: "",
274+
defaultBranch: "main",
275+
currentGitBranch: "feature/x",
276+
}),
277+
).toBe("main");
278+
expect(
279+
resolveWorktreeBaseBranch({
280+
configuredBaseBranch: "",
281+
defaultBranch: null,
282+
currentGitBranch: "feature/x",
283+
}),
284+
).toBe("feature/x");
285+
});
286+
});

‎apps/web/src/components/BranchToolbar.logic.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ export function resolveBranchToolbarValue(input: {
201201
return currentGitBranch ?? activeThreadBranch;
202202
}
203203

204+
/**
205+
* Base branch a new worktree draft starts from: the project's configured base
206+
* branch, then the repo default branch (origin/HEAD), then the checked-out one.
207+
*/
208+
export function resolveWorktreeBaseBranch(input: {
209+
configuredBaseBranch: string;
210+
defaultBranch: string | null;
211+
currentGitBranch: string | null;
212+
}): string | null {
213+
if (input.configuredBaseBranch.length > 0) {
214+
return input.configuredBaseBranch;
215+
}
216+
return input.defaultBranch ?? input.currentGitBranch;
217+
}
218+
204219
export function resolveBranchTriggerLabel(input: {
205220
activeWorktreePath: string | null;
206221
effectiveEnvMode: EnvMode;

‎apps/web/src/components/BranchToolbarBranchSelector.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ import { readLocalApi } from "../localApi";
3434
import { useOpenPrLink } from "../lib/openPullRequestLink";
3535
import { usePaginatedBranches } from "../state/queries";
3636
import { useProject, useQuietThreadShell } from "../state/entities";
37+
import { useEnvironmentSettings } from "../hooks/useSettings";
38+
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
3739
import { useEnvironmentQuery } from "../state/query";
3840
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
3941
import { threadEnvironment } from "../state/threads";
@@ -53,6 +55,7 @@ import {
5355
resolveBranchToolbarValue,
5456
resolveDraftEnvModeAfterBranchChange,
5557
resolveEffectiveEnvMode,
58+
resolveWorktreeBaseBranch,
5659
sanitizeNewRefName,
5760
shouldIncludeBranchPickerItem,
5861
} from "./BranchToolbar.logic";
@@ -143,6 +146,7 @@ export function BranchToolbarBranchSelector({
143146
? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
144147
: null;
145148
const activeProject = useProject(activeProjectRef);
149+
const settings = useEnvironmentSettings(environmentId);
146150

147151
const activeThreadId = serverThread?.id ?? (draftThread ? threadId : undefined);
148152
const activeThreadBranch =
@@ -477,9 +481,18 @@ export function BranchToolbarBranchSelector({
477481
});
478482
};
479483

484+
const configuredBaseBranch = resolveProjectSettings(
485+
settings,
486+
activeProject?.id ?? null,
487+
activeProject ?? undefined,
488+
).settings.newWorktreesBaseBranch;
480489
const worktreeBaseBranchCandidate = isInitialBranchesLoadPending
481490
? null
482-
: (defaultBranchName ?? currentGitBranch);
491+
: resolveWorktreeBaseBranch({
492+
configuredBaseBranch,
493+
defaultBranch: defaultBranchName,
494+
currentGitBranch,
495+
});
483496

484497
// A started thread's server is creating its worktree and records the branch
485498
// itself; defaulting it here would reset the thread to the base checkout and

‎apps/web/src/components/settings/SettingsPanels.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ import {
171171
useSettingsSearchTargetId,
172172
} from "./settingsLayout";
173173
import { searchableSetting } from "./settingsSearch";
174+
import { WorktreeBaseBranchSetting } from "./WorktreeBaseBranchSetting";
174175
import { ProjectFavicon } from "../ProjectFavicon";
175176
import { PanelAnimationsPreview } from "./PanelAnimationsPreview";
176177

@@ -3024,6 +3025,7 @@ export function GeneralSettingsPanel() {
30243025
/>
30253026
}
30263027
/>
3028+
<WorktreeBaseBranchSetting />
30273029
<SettingsRow
30283030
serverScoped
30293031
settingKeys={["addProjectBaseDirectory"]}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useRef } from "react";
2+
import { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
3+
4+
import { Input } from "../ui/input";
5+
import { SettingsRow, SettingResetButton } from "./settingsLayout";
6+
import { useSettingsScope } from "./SettingsScopeContext";
7+
import { searchableSetting } from "./settingsSearch";
8+
import {
9+
useScopedSettings,
10+
useScopedSettingsMixed,
11+
useUpdateScopedSettings,
12+
} from "./useScopedSettings";
13+
14+
export function WorktreeBaseBranchSetting() {
15+
const settings = useScopedSettings();
16+
const { targets } = useSettingsScope();
17+
const scopeKey = targets.map((target) => `${target.environmentId}:${target.projectId}`).join(",");
18+
const edited = useRef(false);
19+
const updateSettings = useUpdateScopedSettings();
20+
const mixed = useScopedSettingsMixed(["newWorktreesBaseBranch"]);
21+
22+
return (
23+
<SettingsRow
24+
serverScoped
25+
settingKeys={["newWorktreesBaseBranch"]}
26+
{...searchableSetting("worktree-base-branch")}
27+
description="New worktrees start from this branch. Leave empty to use the repository default branch."
28+
resetAction={
29+
mixed ||
30+
settings.newWorktreesBaseBranch !== DEFAULT_SERVER_SETTINGS.newWorktreesBaseBranch ? (
31+
<SettingResetButton
32+
label="worktree base branch"
33+
onClick={() =>
34+
updateSettings({
35+
newWorktreesBaseBranch: DEFAULT_SERVER_SETTINGS.newWorktreesBaseBranch,
36+
})
37+
}
38+
/>
39+
) : null
40+
}
41+
control={
42+
<Input
43+
key={`${scopeKey}:${mixed}:${settings.newWorktreesBaseBranch}`}
44+
aria-label="Worktree base branch"
45+
autoCapitalize="none"
46+
spellCheck={false}
47+
onChange={() => {
48+
edited.current = true;
49+
}}
50+
placeholder={mixed ? "Mixed" : "Default branch"}
51+
defaultValue={mixed ? "" : settings.newWorktreesBaseBranch}
52+
onBlur={(event) => {
53+
const value = event.target.value.trim();
54+
if (edited.current && (mixed || value !== settings.newWorktreesBaseBranch))
55+
updateSettings({ newWorktreesBaseBranch: value });
56+
edited.current = false;
57+
}}
58+
/>
59+
}
60+
/>
61+
);
62+
}

‎apps/web/src/components/settings/settingsSearch.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,13 @@ export const SETTINGS_SEARCH_ITEMS = [
485485
scope: "project-defaults",
486486
searchTerms: ["new worktrees start remote origin latest fetched branch local"],
487487
},
488+
{
489+
id: "worktree-base-branch",
490+
title: "Worktree base branch",
491+
to: "/settings/general",
492+
scope: "project-defaults",
493+
searchTerms: ["new worktrees start from develop main default branch origin head"],
494+
},
488495
{
489496
id: "add-project-starts-in",
490497
title: "Add project starts in",

‎docs/user/project-settings.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ to override its environment defaults. Worktree directories keep their original n
6060
If generation fails, or a custom name is invalid or already taken, the temporary
6161
branch name remains.
6262

63+
## Worktree base branch
64+
65+
New worktrees start from the repository default branch. To start them from another branch,
66+
such as `develop`, set **Settings → General → Worktree base branch**. Select a project to set it
67+
for that project only. On mobile, use **Settings → Source control → Worktrees**.
68+
69+
Leave the field empty to use the default branch again. You can still pick another branch for one
70+
thread in the branch selector before you send the first message.
71+
6372
## Scheduled tasks on mobile
6473

6574
Open **Settings → Scheduled tasks** to create recurring tasks or manage existing

0 commit comments

Comments
 (0)