diff --git a/lib/gitWorkflow.js b/lib/gitWorkflow.js index 128a96b13..ac178163c 100644 --- a/lib/gitWorkflow.js +++ b/lib/gitWorkflow.js @@ -15,18 +15,19 @@ const STASH = 'lint-staged automatic backup' const gitApplyArgs = ['apply', '-v', '--whitespace=nowarn', '--recount', '--unidiff-zero'] class GitWorkflow { - constructor(cwd) { - this.execGit = (args, options = {}) => execGit(args, { ...options, cwd }) + constructor({ gitDir, stagedFileChunks }) { + this.execGit = (args, options = {}) => execGit(args, { ...options, cwd: gitDir }) this.unstagedDiff = null - this.cwd = cwd + this.gitDir = gitDir + this.stagedFileChunks = stagedFileChunks /** * These three files hold state about an ongoing git merge * Resolve paths during constructor */ - this.mergeHeadFile = path.resolve(this.cwd, '.git', MERGE_HEAD) - this.mergeModeFile = path.resolve(this.cwd, '.git', MERGE_MODE) - this.mergeMsgFile = path.resolve(this.cwd, '.git', MERGE_MSG) + this.mergeHeadFile = path.resolve(this.gitDir, '.git', MERGE_HEAD) + this.mergeModeFile = path.resolve(this.gitDir, '.git', MERGE_MODE) + this.mergeMsgFile = path.resolve(this.gitDir, '.git', MERGE_MSG) } /** @@ -107,7 +108,11 @@ class GitWorkflow { debug('Detected files modified by tasks:') debug(modifiedFiles) debug('Adding files to index...') - await this.execGit(['add', '.']) + await Promise.all( + // stagedFileChunks includes staged files that lint-staged originally detected. + // Add only these files so any 3rd-party edits to other files won't be included in the commit. + this.stagedFileChunks.map(stagedFiles => this.execGit(['add', ...stagedFiles])) + ) debug('Done adding files to index!') } diff --git a/lib/runAll.js b/lib/runAll.js index 1ae43f911..7a4621245 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -61,8 +61,8 @@ module.exports = async function runAll( debugLog('Loaded list of staged files in git:\n%O', files) - const chunkedFiles = chunkFiles({ files, gitDir, maxArgLength, relative }) - const chunkCount = chunkedFiles.length + const stagedFileChunks = chunkFiles({ files, gitDir, maxArgLength, relative }) + const chunkCount = stagedFileChunks.length if (chunkCount > 1) { debugLog(`Chunked staged files into ${chunkCount} part`, chunkCount) } @@ -78,7 +78,7 @@ module.exports = async function runAll( const listrTasks = [] - for (const [index, files] of chunkedFiles.entries()) { + for (const [index, files] of stagedFileChunks.entries()) { const chunkTasks = generateTasks({ config, cwd, gitDir, files, relative }) const chunkListrTasks = chunkTasks.map(task => { const subTasks = makeCmdTasks({ @@ -139,7 +139,7 @@ module.exports = async function runAll( return 'No tasks to run.' } - const git = new GitWorkflow(gitDir) + const git = new GitWorkflow({ gitDir, stagedFileChunks }) const runner = new Listr( [