@@ -128,6 +128,10 @@ function failedRemoves(...chunks: string[]) {
128128 )
129129}
130130
131+ function branchMissing ( ...chunks : string [ ] ) {
132+ return chunks . some ( ( chunk ) => / b r a n c h [ ' " ] .+ [ ' " ] n o t f o u n d / i. test ( chunk ) )
133+ }
134+
131135// ---------------------------------------------------------------------------
132136// Effect service
133137// ---------------------------------------------------------------------------
@@ -229,9 +233,11 @@ export const layer: Layer.Layer<
229233
230234 const setup = Effect . fnUntraced ( function * ( info : Info ) {
231235 const ctx = yield * InstanceState . context
236+ const head = yield * git ( [ "rev-parse" , "--verify" , "HEAD" ] , { cwd : ctx . worktree } )
237+ const hasHead = head . code === 0
232238 const created = yield * git (
233239 info . branch
234- ? [ "worktree" , "add" , "--no-checkout" , "-b" , info . branch , info . directory ]
240+ ? [ "worktree" , "add" , ... ( hasHead ? [ "--no-checkout" ] : [ ] ) , "-b" , info . branch , info . directory ]
235241 : [ "worktree" , "add" , "--no-checkout" , "--detach" , info . directory , "HEAD" ] ,
236242 { cwd : ctx . worktree } ,
237243 )
@@ -242,6 +248,7 @@ export const layer: Layer.Layer<
242248 }
243249
244250 yield * project . addSandbox ( ctx . project . id , info . directory ) . pipe ( Effect . catch ( ( ) => Effect . void ) )
251+ return undefined
245252 } )
246253
247254 const boot = Effect . fnUntraced ( function * ( info : Info , startCommand ?: string ) {
@@ -454,7 +461,7 @@ export const layer: Layer.Layer<
454461 const branch = entry . branch ?. replace ( / ^ r e f s \/ h e a d s \/ / , "" )
455462 if ( branch ) {
456463 const deleted = yield * git ( [ "branch" , "-D" , branch ] , { cwd : ctx . worktree } )
457- if ( deleted . code !== 0 ) {
464+ if ( deleted . code !== 0 && ! branchMissing ( deleted . stderr , deleted . text ) ) {
458465 return yield * new RemoveFailedError ( {
459466 message : deleted . stderr || deleted . text || "Failed to delete worktree branch" ,
460467 } )
@@ -478,7 +485,7 @@ export const layer: Layer.Layer<
478485 function * ( directory : string , cmd : string ) {
479486 const [ shell , args ] = process . platform === "win32" ? [ "cmd" , [ "/c" , cmd ] ] : [ "bash" , [ "-lc" , cmd ] ]
480487 const result = yield * appProcess . run (
481- ChildProcess . make ( shell , args as string [ ] , { cwd : directory , extendEnv : true , stdin : "ignore" } ) ,
488+ ChildProcess . make ( shell , args , { cwd : directory , extendEnv : true , stdin : "ignore" } ) ,
482489 )
483490 return { code : result . exitCode , stderr : result . stderr . toString ( "utf8" ) }
484491 } ,
0 commit comments