@@ -8,6 +8,7 @@ import { execGit } from './execGit.js'
88import * as figures from './figures.js'
99import { readFile , unlink , writeFile } from './file.js'
1010import { getDiffCommand } from './getDiffCommand.js'
11+ import { chunkFilesForCommand } from './getSpawnedTasks.js'
1112import { normalizePath } from './normalizePath.js'
1213import { parseGitZOutput } from './parseGitZOutput.js'
1314import { runParallelTasks } from './runParallelTasks.js'
@@ -87,6 +88,7 @@ export class GitWorkflow {
8788 gitConfigDir,
8889 logger,
8990 matchedFiles,
91+ maxArgLength,
9092 topLevelDir,
9193 } ) {
9294 this . execGit = ( args , options = { } ) => execGit ( args , { ...options , cwd : topLevelDir } )
@@ -98,6 +100,7 @@ export class GitWorkflow {
98100 this . logger = logger
99101 /** @type {Set<import('./getStagedFiles.js').StagedFile> } */
100102 this . matchedFiles = matchedFiles
103+ this . maxArgLength = maxArgLength
101104 this . topLevelDir = topLevelDir
102105
103106 /**
@@ -384,28 +387,33 @@ export class GitWorkflow {
384387 r . status === 'fulfilled' ? [ r . value ] : [ ]
385388 )
386389
387- debugLog ( 'Updating active Git index: %s' , activeIndexFile )
388- await this . execGit ( [ 'add' , '--' , ...addableFiles ] )
389- debugLog ( 'Done updating Git index: %s' , activeIndexFile )
390+ const addCommands = await chunkFilesForCommand ( 'git add --' , addableFiles , this . maxArgLength )
390391
391- if ( activeIndexFile ?. endsWith ( '.lock' ) ) {
392- const defaultIndexLock = normalizePath (
393- await this . execGit ( [ 'rev-parse' , '--path-format=absolute' , '--git-path' , 'index.lock' ] )
394- )
392+ /** Needs to be run serially because of locking Git operation */
393+ for ( const { files } of addCommands ) {
394+ debugLog ( 'Updating active Git index: %s' , activeIndexFile )
395+ await this . execGit ( [ 'add' , '--' , ...files ] )
396+ debugLog ( 'Done updating Git index: %s' , activeIndexFile )
397+
398+ if ( activeIndexFile ?. endsWith ( '.lock' ) ) {
399+ const defaultIndexLock = normalizePath (
400+ await this . execGit ( [ 'rev-parse' , '--path-format=absolute' , '--git-path' , 'index.lock' ] )
401+ )
395402
396- /**
397- * If the active index file is a non-default lockfile, we are committing with a pathspec
398- * without having explicitly run `git add`. In this case we need to also update the
399- * default index, otherwise there will be leftover diff after committing
400- */
401- if ( activeIndexFile !== defaultIndexLock ) {
402- debugLog ( 'Updating default Git index again: %s' , defaultIndexLock )
403- await this . execGit ( [ 'add' , '--' , ...addableFiles ] , {
404- env : {
405- GIT_INDEX_FILE : defaultIndexLock ,
406- } ,
407- } )
408- debugLog ( 'Done updating default Git index lock: %s' , defaultIndexLock )
403+ /**
404+ * If the active index file is a non-default lockfile, we are committing with a pathspec
405+ * without having explicitly run `git add`. In this case we need to also update the
406+ * default index, otherwise there will be leftover diff after committing
407+ */
408+ if ( activeIndexFile !== defaultIndexLock ) {
409+ debugLog ( 'Updating default Git index again: %s' , defaultIndexLock )
410+ await this . execGit ( [ 'add' , '--' , ...files ] , {
411+ env : {
412+ GIT_INDEX_FILE : defaultIndexLock ,
413+ } ,
414+ } )
415+ debugLog ( 'Done updating default Git index lock: %s' , defaultIndexLock )
416+ }
409417 }
410418 }
411419
0 commit comments