Skip to content

Commit 37e8e1a

Browse files
committed
fix: restore chunking of git add command after running tasks
1 parent 2c75f1d commit 37e8e1a

3 files changed

Lines changed: 55 additions & 20 deletions

File tree

‎lib/gitWorkflow.js‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { execGit } from './execGit.js'
88
import * as figures from './figures.js'
99
import { readFile, unlink, writeFile } from './file.js'
1010
import { getDiffCommand } from './getDiffCommand.js'
11+
import { chunkFilesForCommand } from './getSpawnedTasks.js'
1112
import { normalizePath } from './normalizePath.js'
1213
import { parseGitZOutput } from './parseGitZOutput.js'
1314
import { 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

‎lib/runAll.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ export const runAll = async (
295295
gitConfigDir,
296296
logger: thisLogger,
297297
matchedFiles,
298+
maxArgLength,
298299
topLevelDir,
299300
})
300301

‎test/integration/gitWorkFlow.test.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,32 @@ describe('gitWorkflow', () => {
328328
})
329329

330330
describe('updateIndex', () => {
331+
it(
332+
'should chunk files for git add',
333+
withGitIntegration(async ({ cwd, expect, writeFile: writeRepoFile }) => {
334+
const files = ['first.js', 'second.js'].map((filename) =>
335+
normalizePath(path.join(cwd, filename))
336+
)
337+
338+
await Promise.all(files.map((filename) => writeRepoFile(filename, 'test')))
339+
340+
const gitWorkflow = new GitWorkflow({
341+
logger: makeConsoleMock(),
342+
topLevelDir: cwd,
343+
gitConfigDir: path.join(cwd, './.git'),
344+
matchedFiles: new Set(files.map((filepath) => ({ filepath, status: 'A' }))),
345+
maxArgLength: 1,
346+
})
347+
const execGit = vi.fn(gitWorkflow.execGit)
348+
gitWorkflow.execGit = execGit
349+
350+
await gitWorkflow.updateIndex(getInitialState())
351+
352+
expect(execGit).toHaveBeenNthCalledWith(1, ['add', '--', files[0]])
353+
expect(execGit).toHaveBeenNthCalledWith(2, ['add', '--', files[1]])
354+
})
355+
)
356+
331357
it(
332358
"should not override GIT_INDEX_FILE value when it's the default value",
333359
withGitIntegration(async ({ cwd, execGit, expect }) => {

0 commit comments

Comments
 (0)