Skip to content

Commit 4db17d3

Browse files
committed
Adds switch to worktree from branch switch
1 parent cf319ab commit 4db17d3

File tree

2 files changed

+90
-35
lines changed

2 files changed

+90
-35
lines changed

src/commands/git/switch.ts

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Repository } from '../../git/models/repository';
66
import type { QuickPickItemOfT } from '../../quickpicks/items/common';
77
import { isStringArray } from '../../system/array';
88
import type { ViewsWithRepositoryFolders } from '../../views/viewBase';
9+
import { getSteps } from '../gitCommands.utils';
910
import type {
1011
PartialStepState,
1112
QuickPickStep,
@@ -154,30 +155,69 @@ export class SwitchGitCommand extends QuickCommand<State> {
154155
state.reference = result;
155156
}
156157

157-
if (isBranchReference(state.reference) && state.reference.remote) {
158-
context.title = `Create Branch and ${this.title}`;
158+
if (isBranchReference(state.reference)) {
159+
if (state.reference.remote) {
160+
context.title = `Create Branch and ${this.title}`;
159161

160-
const { values: branches } = await this.container.git.getBranches(state.reference.repoPath, {
161-
filter: b => b.upstream?.name === state.reference!.name,
162-
sort: { orderBy: 'date:desc' },
163-
});
164-
165-
if (branches.length === 0) {
166-
const result = yield* inputBranchNameStep(state as SwitchStepState, context, {
167-
placeholder: 'Please provide a name for the new branch',
168-
titleContext: ` based on ${getReferenceLabel(state.reference, {
169-
icon: false,
170-
})}`,
171-
value: state.createBranch ?? getNameWithoutRemote(state.reference),
162+
const { values: branches } = await this.container.git.getBranches(state.reference.repoPath, {
163+
filter: b => b.upstream?.name === state.reference!.name,
164+
sort: { orderBy: 'date:desc' },
172165
});
173-
if (result === StepResultBreak) continue;
174166

175-
state.createBranch = result;
167+
if (branches.length === 0) {
168+
const result = yield* inputBranchNameStep(state as SwitchStepState, context, {
169+
placeholder: 'Please provide a name for the new branch',
170+
titleContext: ` based on ${getReferenceLabel(state.reference, {
171+
icon: false,
172+
})}`,
173+
value: state.createBranch ?? getNameWithoutRemote(state.reference),
174+
});
175+
if (result === StepResultBreak) continue;
176+
177+
state.createBranch = result;
178+
} else {
179+
context.title = `${this.title} to Local Branch`;
180+
context.switchToLocalFrom = state.reference;
181+
state.reference = branches[0];
182+
state.createBranch = undefined;
183+
}
176184
} else {
177-
context.title = `${this.title} to Local Branch`;
178-
context.switchToLocalFrom = state.reference;
179-
state.reference = branches[0];
180185
state.createBranch = undefined;
186+
187+
const worktree = await this.container.git.getWorktree(
188+
state.reference.repoPath,
189+
w => w.branch === state.reference!.name,
190+
);
191+
if (worktree != null) {
192+
yield* getSteps(
193+
this.container,
194+
{
195+
command: 'worktree',
196+
state: {
197+
subcommand: 'open',
198+
uri: worktree.uri,
199+
openOnly: true,
200+
overrides: {
201+
confirmTitle: `Confirm Switch to Worktree \u2022 ${getReferenceLabel(
202+
state.reference,
203+
{
204+
icon: false,
205+
label: false,
206+
},
207+
)}`,
208+
confirmPlaceholder: `${getReferenceLabel(state.reference, {
209+
capitalize: true,
210+
icon: false,
211+
})} is linked to a worktree`,
212+
},
213+
},
214+
},
215+
this.pickedVia,
216+
);
217+
218+
endSteps(state);
219+
return;
220+
}
181221
}
182222
} else {
183223
state.createBranch = undefined;

src/commands/git/worktree.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ interface OpenState {
9595
repo: string | Repository;
9696
uri: Uri;
9797
flags: OpenFlags[];
98+
99+
openOnly?: boolean;
100+
overrides?: {
101+
confirmTitle?: string;
102+
confirmPlaceholder?: string;
103+
};
98104
}
99105

100106
type State = CreateState | DeleteState | OpenState;
@@ -852,21 +858,23 @@ export class WorktreeGitCommand extends QuickCommand<State> {
852858
}
853859

854860
private *openCommandConfirmStep(state: OpenStepState, context: Context): StepResultGenerator<OpenFlags[]> {
855-
const step: QuickPickStep<FlagsQuickPickItem<OpenFlags>> = createConfirmStep(
856-
appendReposToTitle(`Confirm ${context.title}`, state, context),
857-
[
858-
createFlagsQuickPickItem<OpenFlags>(state.flags, [], {
859-
label: context.title,
860-
detail: `Will open in the current window, the worktree in $(folder) ${GitWorktree.getFriendlyPath(
861-
state.uri,
862-
)}`,
863-
}),
864-
createFlagsQuickPickItem<OpenFlags>(state.flags, ['--new-window'], {
865-
label: `${context.title} in a New Window`,
866-
detail: `Will open in a new window, the worktree in $(folder) ${GitWorktree.getFriendlyPath(
867-
state.uri,
868-
)}`,
869-
}),
861+
const confirmations: QuickPickItemOfT<OpenFlags[]>[] = [
862+
createFlagsQuickPickItem<OpenFlags>(state.flags, [], {
863+
label: context.title,
864+
detail: `Will open in the current window, the worktree in $(folder) ${GitWorktree.getFriendlyPath(
865+
state.uri,
866+
)}`,
867+
}),
868+
createFlagsQuickPickItem<OpenFlags>(state.flags, ['--new-window'], {
869+
label: `${context.title} in a New Window`,
870+
detail: `Will open in a new window, the worktree in $(folder) ${GitWorktree.getFriendlyPath(
871+
state.uri,
872+
)}`,
873+
}),
874+
];
875+
876+
if (!state.openOnly) {
877+
confirmations.push(
870878
createFlagsQuickPickItem<OpenFlags>(state.flags, ['--add-to-workspace'], {
871879
label: `Add Worktree to Workspace`,
872880
detail: `Will add into the current workspace, the worktree in $(folder) ${GitWorktree.getFriendlyPath(
@@ -879,8 +887,15 @@ export class WorktreeGitCommand extends QuickCommand<State> {
879887
state.uri,
880888
)}`,
881889
}),
882-
],
890+
);
891+
}
892+
893+
const step: QuickPickStep<FlagsQuickPickItem<OpenFlags>> = createConfirmStep(
894+
appendReposToTitle(state.overrides?.confirmTitle ?? `Confirm ${context.title}`, state, context),
895+
confirmations,
883896
context,
897+
undefined,
898+
state.overrides?.confirmPlaceholder ? { placeholder: state.overrides.confirmPlaceholder } : undefined,
884899
);
885900

886901
const selection: StepSelection<typeof step> = yield step;

0 commit comments

Comments
 (0)