@@ -73,6 +73,14 @@ const RANGE_DIFF_SUMMARY_MAX_OUTPUT_BYTES = 19_000;
7373const RANGE_DIFF_PATCH_MAX_OUTPUT_BYTES = 59_000 ;
7474const REVIEW_DIFF_PATCH_MAX_OUTPUT_BYTES = 120_000 ;
7575const REVIEW_METADATA_MAX_OUTPUT_BYTES = 16 * 1024 * 1024 ;
76+ const GIT_DEFAULT_CONFLICT_MARKER_SIZE = 7 ;
77+
78+ // Git falls back to the default for any value that is not a positive integer.
79+ function parseConflictMarkerSize ( value = "" ) {
80+ const size = / ^ \+ ? \d + $ / . test ( value ) ? Number . parseInt ( value , 10 ) : 0 ;
81+ return size > 0 ? size : GIT_DEFAULT_CONFLICT_MARKER_SIZE ;
82+ }
83+
7684// Temporary-index git add scales poorly (50k files took ~30 s); report incomplete totals above this cap.
7785const REVIEW_UNTRACKED_MAX_FILES = 5_000 ;
7886const REVIEW_DIFF_FILE_MAX_OUTPUT_BYTES = 1024 * 1024 ;
@@ -2432,9 +2440,139 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24322440 filePaths ?: readonly string [ ] ,
24332441 env ?: NodeJS . ProcessEnv ,
24342442 ) {
2435- const config = env
2436- ? [ "-c" , "core.splitIndex=false" , "-c" , "splitIndex.sharedIndexExpire=never" ]
2437- : [ ] ;
2443+ const isolatedIndexConfig = [
2444+ "-c" ,
2445+ "core.splitIndex=false" ,
2446+ "-c" ,
2447+ "splitIndex.sharedIndexExpire=never" ,
2448+ ] ;
2449+ const config = env ? isolatedIndexConfig : [ ] ;
2450+ const envOptions = env ? { env } : { } ;
2451+ // Status paths are repository-relative, while cwd can be a subdirectory.
2452+ const toPathspec = ( file : string ) => `:(top,literal)${ file } ` ;
2453+ const pathspecs = ( filePaths ?? [ ] ) . map ( toPathspec ) ;
2454+ const unmerged = yield * executeGit (
2455+ "GitVcsDriver.commit.unmerged" ,
2456+ cwd ,
2457+ [
2458+ "ls-files" ,
2459+ "--unmerged" ,
2460+ "-z" ,
2461+ "--full-name" ,
2462+ "--" ,
2463+ ...( pathspecs . length > 0 ? pathspecs : [ ":(top)" ] ) ,
2464+ ] ,
2465+ { ...envOptions , maxOutputBytes : REVIEW_METADATA_MAX_OUTPUT_BYTES } ,
2466+ ) ;
2467+ // Each record is "<mode> <object> <stage>\t<path>", once per stage.
2468+ const unmergedPaths = new Set (
2469+ splitNullSeparatedGitStdoutPaths ( unmerged ) . map ( ( record ) =>
2470+ record . slice ( record . indexOf ( "\t" ) + 1 ) ,
2471+ ) ,
2472+ ) ;
2473+ if ( unmergedPaths . size > 0 ) {
2474+ // add -A marks unmerged paths resolved, so check the files it would stage for leftover markers.
2475+ const files = Array . from ( unmergedPaths ) ;
2476+ const operation = "GitVcsDriver.commit.conflictMarkers" ;
2477+ const checkFailed = "Could not check conflicted files for conflict markers." ;
2478+ const root = yield * readWorktreeRoot ( cwd ) ;
2479+ if ( root === null ) {
2480+ return yield * new GitCommandError ( { operation, command : "git" , cwd, detail : checkFailed } ) ;
2481+ }
2482+ // check-attr takes plain repository paths, not pathspecs, and prints one
2483+ // "<path> <attribute> <value>" triple per path in input order.
2484+ const readMarkerSizes = ( source : readonly string [ ] , options : { env ?: NodeJS . ProcessEnv } ) =>
2485+ executeGit (
2486+ "GitVcsDriver.commit.conflictMarkerSize" ,
2487+ root ,
2488+ [ "check-attr" , ...source , "-z" , "conflict-marker-size" , "--" , ...files ] ,
2489+ { ...options , maxOutputBytes : REVIEW_METADATA_MAX_OUTPUT_BYTES } ,
2490+ ) . pipe ( Effect . map ( ( result ) => result . stdout . split ( "\0" ) ) ) ;
2491+ // HEAD's attributes come from a throwaway index, since check-attr --source needs Git 2.40.
2492+ const readHeadMarkerSizes = Effect . gen ( function * ( ) {
2493+ const head = yield * executeGit (
2494+ "GitVcsDriver.commit.head" ,
2495+ cwd ,
2496+ [ "rev-parse" , "--verify" , "--quiet" , "HEAD" ] ,
2497+ { allowNonZeroExit : true } ,
2498+ ) ;
2499+ if ( head . exitCode !== 0 ) return [ ] ;
2500+ const directory = yield * fileSystem . makeTempDirectoryScoped ( {
2501+ prefix : "t3code-attributes-index-" ,
2502+ } ) ;
2503+ const headIndex = { GIT_INDEX_FILE : path . join ( directory , "index" ) } ;
2504+ yield * runGit (
2505+ "GitVcsDriver.commit.headIndex" ,
2506+ cwd ,
2507+ [ ...isolatedIndexConfig , "read-tree" , "HEAD" ] ,
2508+ { env : headIndex } ,
2509+ ) ;
2510+ return yield * readMarkerSizes ( [ "--cached" ] , { env : headIndex } ) ;
2511+ } ) . pipe (
2512+ Effect . scoped ,
2513+ Effect . catchTags ( {
2514+ PlatformError : ( cause ) =>
2515+ new GitCommandError ( { operation, command : "git" , cwd, detail : checkFailed , cause } ) ,
2516+ } ) ,
2517+ ) ;
2518+ // Markers use the size from before the conflict; afterwards it may survive only in HEAD, the
2519+ // index or the worktree. An unborn HEAD leaves the default.
2520+ const lookups = yield * Effect . all (
2521+ [
2522+ readMarkerSizes ( [ ] , envOptions ) ,
2523+ readMarkerSizes ( [ "--cached" ] , envOptions ) ,
2524+ readHeadMarkerSizes ,
2525+ ] ,
2526+ { concurrency : 3 } ,
2527+ ) ;
2528+ const filesBySize = new Map < number , Set < string > > ( ) ;
2529+ files . forEach ( ( file , index ) => {
2530+ for ( const values of lookups ) {
2531+ const size = parseConflictMarkerSize ( values [ index * 3 + 2 ] ) ;
2532+ filesBySize . set ( size , ( filesBySize . get ( size ) ?? new Set ( ) ) . add ( file ) ) ;
2533+ }
2534+ } ) ;
2535+ const markedPaths = new Set < string > ( ) ;
2536+ for ( const [ size , group ] of filesBySize ) {
2537+ // Git writes exactly `size` marker characters, then whitespace and a label.
2538+ const args = [
2539+ "grep" ,
2540+ "-l" ,
2541+ "-z" ,
2542+ "--full-name" ,
2543+ "-E" ,
2544+ "-e" ,
2545+ `^(<{${ size } }|>{${ size } })([[:space:]]|$)` ,
2546+ "--" ,
2547+ ...Array . from ( group , toPathspec ) ,
2548+ ] ;
2549+ const markers = yield * executeGit ( operation , cwd , args , {
2550+ ...envOptions ,
2551+ allowNonZeroExit : true ,
2552+ maxOutputBytes : REVIEW_METADATA_MAX_OUTPUT_BYTES ,
2553+ } ) ;
2554+ if ( markers . exitCode !== 0 && markers . exitCode !== 1 ) {
2555+ return yield * new GitCommandError ( {
2556+ ...gitCommandContext ( { operation, cwd, args } ) ,
2557+ detail : checkFailed ,
2558+ ...( markers . exitCode === null ? { } : { exitCode : markers . exitCode } ) ,
2559+ stdoutLength : markers . stdout . length ,
2560+ stderrLength : markers . stderr . length ,
2561+ } ) ;
2562+ }
2563+ for ( const file of splitNullSeparatedGitStdoutPaths ( markers ) ) markedPaths . add ( file ) ;
2564+ }
2565+ if ( markedPaths . size > 0 ) {
2566+ const named = Array . from ( markedPaths ) . slice ( 0 , 5 ) . join ( ", " ) ;
2567+ const more = markedPaths . size > 5 ? ` and ${ markedPaths . size - 5 } more` : "" ;
2568+ return yield * new GitCommandError ( {
2569+ operation : "GitVcsDriver.commit" ,
2570+ command : "git commit" ,
2571+ cwd,
2572+ detail : `Cannot commit files that still contain conflict markers: ${ named } ${ more } . Resolve the conflicts, then commit again.` ,
2573+ } ) ;
2574+ }
2575+ }
24382576 if ( filePaths && filePaths . length > 0 ) {
24392577 if ( env ) {
24402578 const head = yield * executeGit (
@@ -2483,17 +2621,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
24832621 yield * runGit (
24842622 "GitVcsDriver.commit.addSelected" ,
24852623 cwd ,
2486- // Status paths are repository-relative, while cwd can be a subdirectory.
2487- [ ...config , "add" , "-A" , "--" , ...filePaths . map ( ( file ) => `:(top,literal)${ file } ` ) ] ,
2488- env ? { env } : { } ,
2624+ [ ...config , "add" , "-A" , "--" , ...pathspecs ] ,
2625+ envOptions ,
24892626 ) ;
24902627 } else {
2491- yield * runGit (
2492- "GitVcsDriver.commit.addAll" ,
2493- cwd ,
2494- [ ...config , "add" , "-A" ] ,
2495- env ? { env } : { } ,
2496- ) ;
2628+ yield * runGit ( "GitVcsDriver.commit.addAll" , cwd , [ ...config , "add" , "-A" ] , envOptions ) ;
24972629 }
24982630 } ) ;
24992631
0 commit comments