@@ -3590,6 +3590,195 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
35903590 } ) ,
35913591 ) ;
35923592
3593+ it . effect ( "force-removes a worktree without deleting what its junctions point to" , ( ) =>
3594+ Effect . gen ( function * ( ) {
3595+ const cwd = yield * makeTmpDir ( ) ;
3596+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3597+ yield * writeTextFile ( cwd , ".gitignore" , "node_modules\n" ) ;
3598+ yield * git ( cwd , [ "add" , ".gitignore" ] ) ;
3599+ yield * git ( cwd , [ "commit" , "-m" , "ignore node_modules" ] ) ;
3600+ const fileSystem = yield * FileSystem . FileSystem ;
3601+ const pathService = yield * Path . Path ;
3602+ const outside = yield * makeTmpDir ( "git-worktree-link-target-" ) ;
3603+ yield * writeTextFile ( outside , "keep.txt" , "keep\n" ) ;
3604+ const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "linked" ) ;
3605+ const driver = yield * GitVcsDriver . GitVcsDriver ;
3606+ yield * driver . createWorktree ( {
3607+ cwd,
3608+ path : worktreePath ,
3609+ refName : initialBranch ,
3610+ newRefName : "feature/linked" ,
3611+ } ) ;
3612+ // Junctions on Windows, the way pnpm links packages; symlinks elsewhere.
3613+ NodeFS . mkdirSync ( pathService . join ( worktreePath , "node_modules" ) ) ;
3614+ NodeFS . symlinkSync (
3615+ outside ,
3616+ pathService . join ( worktreePath , "node_modules" , "dep" ) ,
3617+ "junction" ,
3618+ ) ;
3619+ NodeFS . symlinkSync ( outside , pathService . join ( worktreePath , "shared" ) , "junction" ) ;
3620+
3621+ yield * driver . removeWorktree ( { cwd, path : worktreePath , force : true } ) ;
3622+
3623+ assert . deepEqual ( NodeFS . readdirSync ( pathService . dirname ( worktreePath ) ) , [ ] ) ;
3624+ assert . equal (
3625+ yield * fileSystem . readFileString ( pathService . join ( outside , "keep.txt" ) ) ,
3626+ "keep\n" ,
3627+ ) ;
3628+ } ) ,
3629+ ) ;
3630+
3631+ it . effect ( "keeps the junctions of a worktree that git refuses to remove" , ( ) =>
3632+ Effect . gen ( function * ( ) {
3633+ const cwd = yield * makeTmpDir ( ) ;
3634+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3635+ yield * writeTextFile ( cwd , ".gitignore" , "node_modules\n" ) ;
3636+ yield * git ( cwd , [ "add" , ".gitignore" ] ) ;
3637+ yield * git ( cwd , [ "commit" , "-m" , "ignore node_modules" ] ) ;
3638+ const pathService = yield * Path . Path ;
3639+ const outside = yield * makeTmpDir ( "git-worktree-link-target-" ) ;
3640+ const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "linked" ) ;
3641+ const driver = yield * GitVcsDriver . GitVcsDriver ;
3642+ yield * driver . createWorktree ( {
3643+ cwd,
3644+ path : worktreePath ,
3645+ refName : initialBranch ,
3646+ newRefName : "feature/linked" ,
3647+ } ) ;
3648+ const link = pathService . join ( worktreePath , "node_modules" ) ;
3649+ NodeFS . symlinkSync ( outside , link , "junction" ) ;
3650+ yield * writeTextFile ( worktreePath , "notes.txt" , "draft\n" ) ;
3651+
3652+ const result = yield * Effect . result ( driver . removeWorktree ( { cwd, path : worktreePath } ) ) ;
3653+
3654+ assert . isTrue ( Result . isFailure ( result ) ) ;
3655+ assert . isTrue ( NodeFS . lstatSync ( link ) . isSymbolicLink ( ) ) ;
3656+ assert . deepEqual ( NodeFS . readdirSync ( pathService . dirname ( worktreePath ) ) , [ "linked" ] ) ;
3657+ } ) ,
3658+ ) ;
3659+
3660+ it . effect ( "keeps a locked worktree whose lock can't be read" , ( ) =>
3661+ Effect . gen ( function * ( ) {
3662+ const cwd = yield * makeTmpDir ( ) ;
3663+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3664+ const pathService = yield * Path . Path ;
3665+ const fileSystem = yield * FileSystem . FileSystem ;
3666+ const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "linked" ) ;
3667+ const driver = yield * GitVcsDriver . GitVcsDriver ;
3668+ yield * driver . createWorktree ( {
3669+ cwd,
3670+ path : worktreePath ,
3671+ refName : initialBranch ,
3672+ newRefName : "feature/linked" ,
3673+ } ) ;
3674+ // Git counts the lock file's existence; one that can't be read still locks.
3675+ const lockFile = pathService . join (
3676+ yield * git ( worktreePath , [ "rev-parse" , "--absolute-git-dir" ] ) ,
3677+ "locked" ,
3678+ ) ;
3679+ NodeFS . mkdirSync ( lockFile ) ;
3680+
3681+ const result = yield * Effect . result (
3682+ driver . removeWorktree ( { cwd, path : worktreePath , force : true } ) ,
3683+ ) ;
3684+
3685+ assert . isTrue ( Result . isFailure ( result ) ) ;
3686+ assert . equal ( yield * fileSystem . exists ( pathService . join ( worktreePath , "README.md" ) ) , true ) ;
3687+ } ) ,
3688+ ) ;
3689+
3690+ it . effect ( "keeps edits in a worktree whose core.worktree points elsewhere" , ( ) =>
3691+ Effect . gen ( function * ( ) {
3692+ const cwd = yield * makeTmpDir ( ) ;
3693+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3694+ const pathService = yield * Path . Path ;
3695+ const fileSystem = yield * FileSystem . FileSystem ;
3696+ const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "linked" ) ;
3697+ const driver = yield * GitVcsDriver . GitVcsDriver ;
3698+ yield * driver . createWorktree ( {
3699+ cwd,
3700+ path : worktreePath ,
3701+ refName : initialBranch ,
3702+ newRefName : "feature/linked" ,
3703+ } ) ;
3704+ // A plain `git status` in the worktree now inspects the clean main checkout.
3705+ yield * git ( cwd , [ "config" , "extensions.worktreeConfig" , "true" ] ) ;
3706+ yield * git ( worktreePath , [ "config" , "--worktree" , "core.worktree" , cwd ] ) ;
3707+ yield * writeTextFile ( worktreePath , "README.md" , "uncommitted\n" ) ;
3708+
3709+ const result = yield * Effect . result ( driver . removeWorktree ( { cwd, path : worktreePath } ) ) ;
3710+
3711+ assert . isTrue ( Result . isFailure ( result ) ) ;
3712+ assert . equal (
3713+ yield * fileSystem . readFileString ( pathService . join ( worktreePath , "README.md" ) ) ,
3714+ "uncommitted\n" ,
3715+ ) ;
3716+ } ) ,
3717+ ) ;
3718+
3719+ it . effect ( "removes a worktree whose tracked folder was replaced by a junction" , ( ) =>
3720+ Effect . gen ( function * ( ) {
3721+ const cwd = yield * makeTmpDir ( ) ;
3722+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3723+ yield * writeTextFile ( cwd , "src/keep.txt" , "keep\n" ) ;
3724+ yield * git ( cwd , [ "add" , "src" ] ) ;
3725+ yield * git ( cwd , [ "commit" , "-m" , "add src" ] ) ;
3726+ const fileSystem = yield * FileSystem . FileSystem ;
3727+ const pathService = yield * Path . Path ;
3728+ const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "linked" ) ;
3729+ const driver = yield * GitVcsDriver . GitVcsDriver ;
3730+ yield * driver . createWorktree ( {
3731+ cwd,
3732+ path : worktreePath ,
3733+ refName : initialBranch ,
3734+ newRefName : "feature/linked" ,
3735+ } ) ;
3736+ // Same bytes as the checkout, so Git sees neither a change nor an untracked file.
3737+ const outside = yield * makeTmpDir ( "git-worktree-link-target-" ) ;
3738+ NodeFS . copyFileSync (
3739+ pathService . join ( worktreePath , "src" , "keep.txt" ) ,
3740+ pathService . join ( outside , "keep.txt" ) ,
3741+ ) ;
3742+ NodeFS . rmSync ( pathService . join ( worktreePath , "src" ) , { recursive : true } ) ;
3743+ NodeFS . symlinkSync ( outside , pathService . join ( worktreePath , "src" ) , "junction" ) ;
3744+
3745+ yield * driver . removeWorktree ( { cwd, path : worktreePath } ) ;
3746+
3747+ assert . deepEqual ( NodeFS . readdirSync ( pathService . dirname ( worktreePath ) ) , [ ] ) ;
3748+ assert . equal (
3749+ yield * fileSystem . readFileString ( pathService . join ( outside , "keep.txt" ) ) ,
3750+ "keep\n" ,
3751+ ) ;
3752+ } ) ,
3753+ ) ;
3754+
3755+ it . effect ( "leaves a folder alone that only shares the worktree's short name" , ( ) =>
3756+ Effect . gen ( function * ( ) {
3757+ const cwd = yield * makeTmpDir ( ) ;
3758+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
3759+ const pathService = yield * Path . Path ;
3760+ const fileSystem = yield * FileSystem . FileSystem ;
3761+ const driver = yield * GitVcsDriver . GitVcsDriver ;
3762+ const worktreePath = pathService . join ( yield * makeTmpDir ( "git-worktrees-" ) , "short" ) ;
3763+ yield * driver . createWorktree ( {
3764+ cwd,
3765+ path : worktreePath ,
3766+ refName : initialBranch ,
3767+ newRefName : "feature/short" ,
3768+ } ) ;
3769+ // Unrelated to the worktree, but resolving "short" against cwd lands here.
3770+ const outside = yield * makeTmpDir ( "git-worktree-link-target-" ) ;
3771+ const unrelated = pathService . join ( cwd , "short" ) ;
3772+ NodeFS . mkdirSync ( unrelated ) ;
3773+ NodeFS . symlinkSync ( outside , pathService . join ( unrelated , "linked" ) , "junction" ) ;
3774+
3775+ yield * Effect . result ( driver . removeWorktree ( { cwd, path : "short" , force : true } ) ) ;
3776+
3777+ assert . isTrue ( NodeFS . lstatSync ( pathService . join ( unrelated , "linked" ) ) . isSymbolicLink ( ) ) ;
3778+ assert . equal ( yield * fileSystem . exists ( worktreePath ) , true ) ;
3779+ } ) ,
3780+ ) ;
3781+
35933782 it . effect ( "allows worktree removal to run longer than the default command timeout" , ( ) =>
35943783 Effect . gen ( function * ( ) {
35953784 const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
0 commit comments