@@ -13,6 +13,7 @@ import * as Exit from "effect/Exit";
1313import * as FileSystem from "effect/FileSystem" ;
1414import * as Fiber from "effect/Fiber" ;
1515import * as Layer from "effect/Layer" ;
16+ import * as Option from "effect/Option" ;
1617import * as Path from "effect/Path" ;
1718import * as Queue from "effect/Queue" ;
1819import * as PlatformError from "effect/PlatformError" ;
@@ -4678,6 +4679,79 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
46784679 assert . isTrue ( Result . isSuccess ( result ) , "worktree creation failed in the hook" ) ;
46794680 } ) ,
46804681 ) ;
4682+ it . effect ( "refreshes a new worktree's index once its checkout second is over" , ( ) =>
4683+ Effect . gen ( function * ( ) {
4684+ const cwd = yield * makeTmpDir ( ) ;
4685+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
4686+ const fs = yield * FileSystem . FileSystem ;
4687+ const path = yield * Path . Path ;
4688+ const driver = yield * GitVcsDriver . GitVcsDriver ;
4689+ for ( let file = 0 ; file < 50 ; file ++ ) {
4690+ yield * writeTextFile ( cwd , `src/file-${ file } .txt` , `contents ${ file } \n` ) ;
4691+ }
4692+ yield * git ( cwd , [ "add" , "." ] ) ;
4693+ yield * git ( cwd , [ "commit" , "-m" , "more files" ] ) ;
4694+ const worktreePath = path . join ( yield * makeTmpDir ( "git-worktrees-" ) , "fresh" ) ;
4695+
4696+ // The refresh waits on the clock that stamps the files.
4697+ yield * TestClock . withLive (
4698+ driver . createWorktree ( {
4699+ cwd,
4700+ path : worktreePath ,
4701+ refName : initialBranch ,
4702+ newRefName : "feature/fresh" ,
4703+ } ) ,
4704+ ) ;
4705+
4706+ // Git re-reads every entry stamped in the index's own second or later.
4707+ const indexPath = path . resolve (
4708+ worktreePath ,
4709+ yield * git ( worktreePath , [ "rev-parse" , "--git-path" , "index" ] ) ,
4710+ ) ;
4711+ const indexMtime = Option . getOrThrow ( ( yield * fs . stat ( indexPath ) ) . mtime ) ;
4712+ const indexSecond = Math . floor ( indexMtime . getTime ( ) / 1000 ) ;
4713+ const entrySeconds = Array . from (
4714+ ( yield * git ( worktreePath , [ "ls-files" , "--debug" ] ) ) . matchAll ( / ^ { 2 } m t i m e : ( \d + ) : / gm) ,
4715+ ( [ , seconds ] ) => Number ( seconds ) ,
4716+ ) ;
4717+ assert . equal ( entrySeconds . length , 51 ) ;
4718+ assert . deepEqual (
4719+ entrySeconds . filter ( ( seconds ) => seconds >= indexSecond ) ,
4720+ [ ] ,
4721+ ) ;
4722+ } ) ,
4723+ ) ;
4724+ it . effect ( "returns the worktree when its index refresh fails" , ( ) =>
4725+ Effect . gen ( function * ( ) {
4726+ const cwd = yield * makeTmpDir ( ) ;
4727+ const { initialBranch } = yield * initRepoWithCommit ( cwd ) ;
4728+ const fs = yield * FileSystem . FileSystem ;
4729+ const path = yield * Path . Path ;
4730+ const driver = yield * GitVcsDriver . GitVcsDriver ;
4731+ // Something else takes the new worktree's index lock as soon as its checkout ends.
4732+ yield * writeTextFile (
4733+ cwd ,
4734+ ".git/hooks/post-checkout" ,
4735+ '#!/bin/sh\ntouch "$(git rev-parse --git-path index).lock"\n' ,
4736+ ) ;
4737+ yield * fs . chmod ( path . join ( cwd , ".git/hooks/post-checkout" ) , 0o755 ) ;
4738+ const worktreePath = path . join ( yield * makeTmpDir ( "git-worktrees-" ) , "locked" ) ;
4739+
4740+ const created = yield * driver . createWorktree ( {
4741+ cwd,
4742+ path : worktreePath ,
4743+ refName : initialBranch ,
4744+ newRefName : "feature/locked" ,
4745+ } ) ;
4746+
4747+ assert . equal ( created . worktree . path , worktreePath ) ;
4748+ const indexPath = path . resolve (
4749+ worktreePath ,
4750+ yield * git ( worktreePath , [ "rev-parse" , "--git-path" , "index" ] ) ,
4751+ ) ;
4752+ assert . isTrue ( yield * fs . exists ( `${ indexPath } .lock` ) ) ;
4753+ } ) ,
4754+ ) ;
46814755 it . effect . each ( [ "commit" , "short-commit" , "tag" , "revision" ] as const ) (
46824756 "creates a worktree from a %s without recording it as a PR base" ,
46834757 ( kind ) =>
0 commit comments