@@ -6,6 +6,7 @@ import type { Repository } from '../../git/models/repository';
66import type { QuickPickItemOfT } from '../../quickpicks/items/common' ;
77import { isStringArray } from '../../system/array' ;
88import type { ViewsWithRepositoryFolders } from '../../views/viewBase' ;
9+ import { getSteps } from '../gitCommands.utils' ;
910import 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 ;
0 commit comments