@@ -85,6 +85,10 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
8585 const args = ( cmd : string [ ] ) => [ "--git-dir" , state . gitdir , "--work-tree" , state . worktree , ...cmd ]
8686
8787 const feed = ( list : string [ ] ) => list . join ( "\0" ) + "\0"
88+ const feedSpec = ( list : string [ ] ) => feed ( list . map ( ( item ) => `:(top,literal)${ item } ` ) )
89+
90+ const scope = path . relative ( state . worktree , state . directory ) . replaceAll ( "\\" , "/" )
91+ const spec = scope ? `:(top,literal)${ scope } ` : "."
8892
8993 const git = Effect . fnUntraced (
9094 function * ( cmd : string [ ] , opts ?: { cwd ?: string ; env ?: Record < string , string > ; stdin ?: string } ) {
@@ -122,7 +126,7 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
122126 "-z" ,
123127 ] ,
124128 {
125- cwd : state . directory ,
129+ cwd : state . worktree ,
126130 stdin : feed ( files ) ,
127131 } ,
128132 )
@@ -138,8 +142,8 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
138142 ...args ( [ "rm" , "--cached" , "-f" , "--ignore-unmatch" , "--pathspec-from-file=-" , "--pathspec-file-nul" ] ) ,
139143 ] ,
140144 {
141- cwd : state . directory ,
142- stdin : feed ( files ) ,
145+ cwd : state . worktree ,
146+ stdin : feedSpec ( files ) ,
143147 } ,
144148 )
145149 } )
@@ -149,8 +153,8 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
149153 const result = yield * git (
150154 [ ...cfg , ...args ( [ "add" , "--all" , "--sparse" , "--pathspec-from-file=-" , "--pathspec-file-nul" ] ) ] ,
151155 {
152- cwd : state . directory ,
153- stdin : feed ( files ) ,
156+ cwd : state . worktree ,
157+ stdin : feedSpec ( files ) ,
154158 } ,
155159 )
156160 if ( result . code === 0 ) return
@@ -197,11 +201,11 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
197201 yield * sync ( )
198202 const [ diff , other ] = yield * Effect . all (
199203 [
200- git ( [ ...quote , ...args ( [ "diff-files" , "--name-only" , "-z" , "--" , "." ] ) ] , {
201- cwd : state . directory ,
204+ git ( [ ...quote , ...args ( [ "diff-files" , "--name-only" , "-z" , "--" , spec ] ) ] , {
205+ cwd : state . worktree ,
202206 } ) ,
203- git ( [ ...quote , ...args ( [ "ls-files" , "--others" , "--exclude-standard" , "-z" , "--" , "." ] ) ] , {
204- cwd : state . directory ,
207+ git ( [ ...quote , ...args ( [ "ls-files" , "--others" , "--exclude-standard" , "-z" , "--" , spec ] ) ] , {
208+ cwd : state . worktree ,
205209 } ) ,
206210 ] ,
207211 { concurrency : 2 } ,
@@ -239,7 +243,7 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
239243 ( yield * Effect . all (
240244 allow . map ( ( item ) =>
241245 fs
242- . stat ( path . join ( state . directory , item ) )
246+ . stat ( path . join ( state . worktree , item ) )
243247 . pipe ( Effect . catch ( ( ) => Effect . void ) )
244248 . pipe (
245249 Effect . map ( ( stat ) => {
@@ -306,9 +310,9 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
306310 Effect . gen ( function * ( ) {
307311 yield * add ( )
308312 const result = yield * git (
309- [ ...quote , ...args ( [ "diff" , "--cached" , "--no-ext-diff" , "--name-only" , hash , "--" , "." ] ) ] ,
313+ [ ...quote , ...args ( [ "diff" , "--cached" , "--no-ext-diff" , "--name-only" , hash , "--" , spec ] ) ] ,
310314 {
311- cwd : state . directory ,
315+ cwd : state . worktree ,
312316 } ,
313317 )
314318 if ( result . code !== 0 ) {
@@ -338,24 +342,47 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
338342 return yield * locked (
339343 Effect . gen ( function * ( ) {
340344 log . info ( "restore" , { commit : snapshot } )
341- const result = yield * git ( [ ...core , ...args ( [ "read-tree" , snapshot ] ) ] , { cwd : state . worktree } )
342- if ( result . code === 0 ) {
343- const checkout = yield * git ( [ ...core , ...args ( [ "checkout-index" , "-a" , "-f" ] ) ] , {
345+ const listed = yield * git ( [ ...quote , ...args ( [ "ls-tree" , "-r" , "-z" , "--name-only" , snapshot , "--" , spec ] ) ] , {
346+ cwd : state . worktree ,
347+ } )
348+ if ( listed . code !== 0 ) {
349+ log . error ( "failed to list snapshot files" , {
350+ snapshot,
351+ exitCode : listed . code ,
352+ stderr : listed . stderr ,
353+ } )
354+ return
355+ }
356+ const files = listed . text . split ( "\0" ) . filter ( Boolean )
357+ if ( ! files . length ) return
358+
359+ const index = path . join ( state . gitdir , "restore.index" )
360+ yield * remove ( index )
361+ yield * Effect . gen ( function * ( ) {
362+ const result = yield * git ( [ ...core , ...args ( [ "read-tree" , `--index-output=${ index } ` , snapshot ] ) ] , {
344363 cwd : state . worktree ,
345364 } )
346- if ( checkout . code === 0 ) return
365+ if ( result . code === 0 ) {
366+ const checkout = yield * git ( [ ...core , ...args ( [ "checkout-index" , "-f" , "--stdin" , "-z" ] ) ] , {
367+ cwd : state . worktree ,
368+ env : { GIT_INDEX_FILE : index } ,
369+ stdin : feed ( files ) ,
370+ } )
371+ if ( checkout . code === 0 ) return
372+ log . error ( "failed to restore snapshot" , {
373+ snapshot,
374+ exitCode : checkout . code ,
375+ stderr : checkout . stderr ,
376+ } )
377+ return
378+ }
379+
347380 log . error ( "failed to restore snapshot" , {
348381 snapshot,
349- exitCode : checkout . code ,
350- stderr : checkout . stderr ,
382+ exitCode : result . code ,
383+ stderr : result . stderr ,
351384 } )
352- return
353- }
354- log . error ( "failed to restore snapshot" , {
355- snapshot,
356- exitCode : result . code ,
357- stderr : result . stderr ,
358- } )
385+ } ) . pipe ( Effect . ensuring ( remove ( index ) ) )
359386 } ) ,
360387 )
361388 } )
@@ -479,9 +506,12 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
479506 return yield * locked (
480507 Effect . gen ( function * ( ) {
481508 yield * add ( )
482- const result = yield * git ( [ ...quote , ...args ( [ "diff" , "--cached" , "--no-ext-diff" , hash , "--" , "." ] ) ] , {
483- cwd : state . worktree ,
484- } )
509+ const result = yield * git (
510+ [ ...quote , ...args ( [ "diff" , "--cached" , "--no-ext-diff" , hash , "--" , spec ] ) ] ,
511+ {
512+ cwd : state . worktree ,
513+ } ,
514+ )
485515 if ( result . code !== 0 ) {
486516 log . warn ( "failed to get diff" , {
487517 hash,
@@ -637,8 +667,8 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
637667 const status = new Map < string , "added" | "deleted" | "modified" > ( )
638668
639669 const statuses = yield * git (
640- [ ...quote , ...args ( [ "diff" , "--no-ext-diff" , "--name-status" , "--no-renames" , from , to , "--" , "." ] ) ] ,
641- { cwd : state . directory } ,
670+ [ ...quote , ...args ( [ "diff" , "--no-ext-diff" , "--name-status" , "--no-renames" , from , to , "--" , spec ] ) ] ,
671+ { cwd : state . worktree } ,
642672 )
643673
644674 for ( const line of statuses . text . trim ( ) . split ( "\n" ) ) {
@@ -649,9 +679,9 @@ export const layer: Layer.Layer<Service, never, AppFileSystem.Service | AppProce
649679 }
650680
651681 const numstat = yield * git (
652- [ ...quote , ...args ( [ "diff" , "--no-ext-diff" , "--no-renames" , "--numstat" , from , to , "--" , "." ] ) ] ,
682+ [ ...quote , ...args ( [ "diff" , "--no-ext-diff" , "--no-renames" , "--numstat" , from , to , "--" , spec ] ) ] ,
653683 {
654- cwd : state . directory ,
684+ cwd : state . worktree ,
655685 } ,
656686 )
657687
0 commit comments