Skip to content

Commit

Permalink
fix: automatically add modifications only to originally staged files
Browse files Browse the repository at this point in the history
Using `git add .` does not work when an unrelated 3rd-party makes a file edit while lint-staged is running tasks. This change at least limits adding of modifications to those files that originally had staged changes. Unfortunately we have no way of detecting if a 3rd-party edits the same file.
  • Loading branch information
iiroj committed Dec 5, 2019
1 parent 814b9df commit 083b8e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
19 changes: 12 additions & 7 deletions lib/gitWorkflow.js
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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!')
}

Expand Down
8 changes: 4 additions & 4 deletions lib/runAll.js
Expand Up @@ -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)
}
Expand All @@ -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({
Expand Down Expand Up @@ -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(
[
Expand Down

0 comments on commit 083b8e7

Please sign in to comment.