@@ -76,6 +76,7 @@ function createReviewFixture(): ReviewFixture {
7676 writeFileSync ( join ( root , "transition-a.txt" ) , "pr-a\n" ) ;
7777 writeFileSync ( join ( root , "transition-b.txt" ) , "pr-b\n" ) ;
7878 writeFileSync ( join ( root , "overlap.txt" ) , "pr-a-overlap\n" ) ;
79+ writeFileSync ( join ( root , "pr-only.txt" ) , "removed on main\n" ) ;
7980 git ( root , "add" , "." ) ;
8081 git ( root , "commit" , "-m" , "PR head A" ) ;
8182 const prASha = git ( root , "rev-parse" , "HEAD" ) ;
@@ -256,6 +257,53 @@ describePosix("scripts/pr worktree containment", () => {
256257 expectCanonicalCheckoutUnchanged ( fixture ) ;
257258 } ) ;
258259
260+ for ( const mode of [ "branch" , "detached" ] as const ) {
261+ it . each ( [
262+ {
263+ name : "partially restored index" ,
264+ setup : [ 'git restore --source="$target_sha" --staged --worktree -- pr-only.txt' ] ,
265+ } ,
266+ {
267+ name : "fully restored index" ,
268+ setup : [ 'git restore --source="$target_sha" --staged --worktree -- .' ] ,
269+ } ,
270+ {
271+ name : "interrupted recovery checkout" ,
272+ setup : [
273+ 'git() { if [ "${1:-}" = checkout ]; then return 73; fi; command git "$@"; }' ,
274+ "if recover_review_transition 42; then exit 1; fi" ,
275+ "unset -f git" ,
276+ 'git diff --cached --quiet "$target_sha"' ,
277+ ] ,
278+ } ,
279+ ] ) ( `recovers completed deletions after $name (${ mode } )` , ( { setup } ) => {
280+ const fixture = createReviewFixture ( ) ;
281+ const worktree = join ( fixture . root , ".worktrees" , "pr-42" ) ;
282+ const result = runShell ( fixture , [
283+ "review_init 42" ,
284+ "review_checkout_pr 42" ,
285+ 'printf "preserve me\\n" > .local/review-note' ,
286+ "source_sha=$(git rev-parse HEAD)" ,
287+ "target_sha=$(git rev-parse origin/main)" ,
288+ `write_review_transition_journal 42 "$source_sha" "$target_sha" ${ mode } temp/pr-42` ,
289+ ...setup ,
290+ "test ! -e pr-only.txt" ,
291+ 'test "$(git rev-parse HEAD)" = "$source_sha"' ,
292+ "test -f .local/review-transition.json" ,
293+ "recover_review_transition 42" ,
294+ ] ) ;
295+
296+ expect ( result . status , `${ result . stdout } \n${ result . stderr } ` ) . toBe ( 0 ) ;
297+ expect ( git ( worktree , "rev-parse" , "HEAD" ) ) . toBe ( fixture . mainSha ) ;
298+ expect ( git ( worktree , "branch" , "--show-current" ) ) . toBe ( mode === "branch" ? "temp/pr-42" : "" ) ;
299+ expect ( git ( worktree , "status" , "--porcelain=v1" , "--untracked-files=no" ) ) . toBe ( "" ) ;
300+ expect ( existsSync ( join ( worktree , "pr-only.txt" ) ) ) . toBe ( false ) ;
301+ expect ( readFileSync ( join ( worktree , ".local" , "review-note" ) , "utf8" ) ) . toBe ( "preserve me\n" ) ;
302+ expect ( existsSync ( join ( worktree , ".local" , "review-transition.json" ) ) ) . toBe ( false ) ;
303+ expectCanonicalCheckoutUnchanged ( fixture ) ;
304+ } ) ;
305+ }
306+
259307 for ( const testCase of [
260308 {
261309 name : "staged" ,
0 commit comments