@@ -7,6 +7,7 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
77import * as HostProcess from "@t3tools/shared/HostProcess" ;
88import { assert , it , describe } from "@effect/vitest" ;
99import * as Cause from "effect/Cause" ;
10+ import * as Clock from "effect/Clock" ;
1011import * as Deferred from "effect/Deferred" ;
1112import * as Effect from "effect/Effect" ;
1213import * as Exit from "effect/Exit" ;
@@ -3960,6 +3961,98 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
39603961 } ) ,
39613962 ) ;
39623963
3964+ it . effect (
3965+ "reports a new repository without waiting on a huge untracked file's line count" ,
3966+ ( ) =>
3967+ Effect . gen ( function * ( ) {
3968+ const cwd = yield * makeTmpDir ( ) ;
3969+ yield * git ( cwd , [ "init" , "-b" , "main" ] ) ;
3970+ yield * writeTextFile ( cwd , "small.txt" , "line\n" ) ;
3971+ yield * writeTextFile ( cwd , "large.data" , "" ) ;
3972+ const fileSystem = yield * FileSystem . FileSystem ;
3973+ const pathService = yield * Path . Path ;
3974+ yield * fileSystem . truncate ( pathService . join ( cwd , "large.data" ) , 8 * 1024 * 1024 * 1024 ) ;
3975+ const delegate = yield * ChildProcessSpawner . ChildProcessSpawner ;
3976+ const totalsDiffSettled = yield * Deferred . make < void > ( ) ;
3977+ const largeFileDiffAbandonedAt = yield * Deferred . make < number > ( ) ;
3978+ // Stands in for `git diff --numstat` reading gigabytes of untracked file content.
3979+ const readsLargeFile = ( command : ChildProcess . Command ) => {
3980+ const indexFile =
3981+ ChildProcess . isStandardCommand ( command ) &&
3982+ command . args . includes ( "--numstat" ) &&
3983+ command . options . env ?. GIT_INDEX_FILE ;
3984+ return (
3985+ typeof indexFile === "string" &&
3986+ NodeFS . readFileSync ( indexFile ) . includes ( Buffer . from ( "large.data" ) )
3987+ ) ;
3988+ } ;
3989+ const spawner = ChildProcessSpawner . make ( ( command ) =>
3990+ readsLargeFile ( command )
3991+ ? Deferred . succeed ( totalsDiffSettled , undefined ) . pipe (
3992+ Effect . as (
3993+ ChildProcessSpawner . makeHandle ( {
3994+ ...makeSuccessfulHandle ( "" ) ,
3995+ isRunning : Effect . succeed ( true ) ,
3996+ exitCode : Effect . never . pipe (
3997+ Effect . onInterrupt ( ( ) =>
3998+ Clock . currentTimeMillis . pipe (
3999+ Effect . flatMap ( ( now ) =>
4000+ Deferred . succeed ( largeFileDiffAbandonedAt , now ) ,
4001+ ) ,
4002+ ) ,
4003+ ) ,
4004+ ) ,
4005+ } ) ,
4006+ ) ,
4007+ )
4008+ : delegate . spawn ( command ) ,
4009+ ) ;
4010+ const driver = yield * makeGitVcsDriverCore ( ) . pipe (
4011+ Effect . provideService ( ChildProcessSpawner . ChildProcessSpawner , spawner ) ,
4012+ ) ;
4013+ const startedAt = yield * Clock . currentTimeMillis ;
4014+
4015+ const status = yield * driver
4016+ . statusDetailsLocal ( cwd , { includeDivergence : false , includeBranchChanges : true } )
4017+ . pipe ( Effect . forkChild ) ;
4018+ yield * Effect . raceFirst ( Deferred . await ( totalsDiffSettled ) , Fiber . await ( status ) ) ;
4019+ // Git's default command timeout, which is what releases the status on main.
4020+ yield * TestClock . adjust ( "30 seconds" ) ;
4021+ const reported = yield * Fiber . join ( status ) ;
4022+
4023+ const statusHeldForMs = ( yield * Deferred . isDone ( largeFileDiffAbandonedAt ) )
4024+ ? ( yield * Deferred . await ( largeFileDiffAbandonedAt ) ) - startedAt
4025+ : 0 ;
4026+ assert . isAtMost ( statusHeldForMs , 5_000 , "status waited on the untracked line count" ) ;
4027+ assert . isTrue ( reported . isRepo ) ;
4028+ assert . strictEqual ( reported . branch , "main" ) ;
4029+ } ) . pipe ( Effect . provide ( layerServerConfig ) ) ,
4030+ ) ;
4031+
4032+ it . effect ( "counts totals when a large unstaged copy of a removed file stays on disk" , ( ) =>
4033+ Effect . gen ( function * ( ) {
4034+ const cwd = yield * makeTmpDir ( ) ;
4035+ yield * initRepoWithCommit ( cwd ) ;
4036+ const driver = yield * GitVcsDriver . GitVcsDriver ;
4037+ const fileSystem = yield * FileSystem . FileSystem ;
4038+ const pathService = yield * Path . Path ;
4039+ yield * writeTextFile ( cwd , "asset.data" , "old\n" ) ;
4040+ yield * git ( cwd , [ "add" , "asset.data" ] ) ;
4041+ yield * git ( cwd , [ "commit" , "-m" , "add asset" ] ) ;
4042+ yield * git ( cwd , [ "rm" , "--cached" , "asset.data" ] ) ;
4043+ yield * fileSystem . truncate ( pathService . join ( cwd , "asset.data" ) , 257 * 1024 * 1024 ) ;
4044+ yield * writeTextFile ( cwd , "ordinary.txt" , "new\n" ) ;
4045+
4046+ const status = yield * driver . statusDetailsLocal ( cwd , { includeBranchChanges : true } ) ;
4047+
4048+ assert . deepStrictEqual ( status . branchChanges , {
4049+ baseRef : null ,
4050+ insertions : 1 ,
4051+ deletions : 1 ,
4052+ } ) ;
4053+ } ) ,
4054+ ) ;
4055+
39634056 it . effect ( "reports changes to a file named HEAD" , ( ) =>
39644057 Effect . gen ( function * ( ) {
39654058 const cwd = yield * makeTmpDir ( ) ;
0 commit comments