@@ -26,8 +26,12 @@ export namespace Snapshot {
2626 . nothrow ( )
2727 log . info ( "initialized" )
2828 }
29- await $ `git --git-dir ${ git } add .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
30- const hash = await $ `git --git-dir ${ git } write-tree` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( ) . text ( )
29+ await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } add .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
30+ const hash = await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } write-tree`
31+ . quiet ( )
32+ . cwd ( Instance . directory )
33+ . nothrow ( )
34+ . text ( )
3135 log . info ( "tracking" , { hash, cwd : Instance . directory , git } )
3236 return hash . trim ( )
3337 }
@@ -40,8 +44,11 @@ export namespace Snapshot {
4044
4145 export async function patch ( hash : string ) : Promise < Patch > {
4246 const git = gitdir ( )
43- await $ `git --git-dir ${ git } add .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
44- const result = await $ `git --git-dir ${ git } diff --name-only ${ hash } -- .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
47+ await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } add .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
48+ const result = await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } diff --name-only ${ hash } -- .`
49+ . quiet ( )
50+ . cwd ( Instance . directory )
51+ . nothrow ( )
4552
4653 // If git diff fails, return empty patch
4754 if ( result . exitCode !== 0 ) {
@@ -64,10 +71,11 @@ export namespace Snapshot {
6471 export async function restore ( snapshot : string ) {
6572 log . info ( "restore" , { commit : snapshot } )
6673 const git = gitdir ( )
67- const result = await $ `git --git-dir=${ git } read-tree ${ snapshot } && git --git-dir=${ git } checkout-index -a -f`
68- . quiet ( )
69- . cwd ( Instance . worktree )
70- . nothrow ( )
74+ const result =
75+ await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } read-tree ${ snapshot } && git --git-dir ${ git } --work-tree ${ Instance . worktree } checkout-index -a -f`
76+ . quiet ( )
77+ . cwd ( Instance . worktree )
78+ . nothrow ( )
7179
7280 if ( result . exitCode !== 0 ) {
7381 log . error ( "failed to restore snapshot" , {
@@ -86,16 +94,17 @@ export namespace Snapshot {
8694 for ( const file of item . files ) {
8795 if ( files . has ( file ) ) continue
8896 log . info ( "reverting" , { file, hash : item . hash } )
89- const result = await $ `git --git-dir= ${ git } checkout ${ item . hash } -- ${ file } `
97+ const result = await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } checkout ${ item . hash } -- ${ file } `
9098 . quiet ( )
9199 . cwd ( Instance . worktree )
92100 . nothrow ( )
93101 if ( result . exitCode !== 0 ) {
94102 const relativePath = path . relative ( Instance . worktree , file )
95- const checkTree = await $ `git --git-dir=${ git } ls-tree ${ item . hash } -- ${ relativePath } `
96- . quiet ( )
97- . cwd ( Instance . worktree )
98- . nothrow ( )
103+ const checkTree =
104+ await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } ls-tree ${ item . hash } -- ${ relativePath } `
105+ . quiet ( )
106+ . cwd ( Instance . worktree )
107+ . nothrow ( )
99108 if ( checkTree . exitCode === 0 && checkTree . text ( ) . trim ( ) ) {
100109 log . info ( "file existed in snapshot but checkout failed, keeping" , {
101110 file,
@@ -112,8 +121,11 @@ export namespace Snapshot {
112121
113122 export async function diff ( hash : string ) {
114123 const git = gitdir ( )
115- await $ `git --git-dir ${ git } add .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
116- const result = await $ `git --git-dir=${ git } diff ${ hash } -- .` . quiet ( ) . cwd ( Instance . worktree ) . nothrow ( )
124+ await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } add .` . quiet ( ) . cwd ( Instance . directory ) . nothrow ( )
125+ const result = await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } diff ${ hash } -- .`
126+ . quiet ( )
127+ . cwd ( Instance . worktree )
128+ . nothrow ( )
117129
118130 if ( result . exitCode !== 0 ) {
119131 log . warn ( "failed to get diff" , {
@@ -143,16 +155,20 @@ export namespace Snapshot {
143155 export async function diffFull ( from : string , to : string ) : Promise < FileDiff [ ] > {
144156 const git = gitdir ( )
145157 const result : FileDiff [ ] = [ ]
146- for await ( const line of $ `git --git-dir= ${ git } diff --no-renames --numstat ${ from } ${ to } -- .`
158+ for await ( const line of $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } diff --no-renames --numstat ${ from } ${ to } -- .`
147159 . quiet ( )
148160 . cwd ( Instance . directory )
149161 . nothrow ( )
150162 . lines ( ) ) {
151163 if ( ! line ) continue
152164 const [ additions , deletions , file ] = line . split ( "\t" )
153165 const isBinaryFile = additions === "-" && deletions === "-"
154- const before = isBinaryFile ? "" : await $ `git --git-dir=${ git } show ${ from } :${ file } ` . quiet ( ) . nothrow ( ) . text ( )
155- const after = isBinaryFile ? "" : await $ `git --git-dir=${ git } show ${ to } :${ file } ` . quiet ( ) . nothrow ( ) . text ( )
166+ const before = isBinaryFile
167+ ? ""
168+ : await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } show ${ from } :${ file } ` . quiet ( ) . nothrow ( ) . text ( )
169+ const after = isBinaryFile
170+ ? ""
171+ : await $ `git --git-dir ${ git } --work-tree ${ Instance . worktree } show ${ to } :${ file } ` . quiet ( ) . nothrow ( ) . text ( )
156172 result . push ( {
157173 file,
158174 before,
0 commit comments