diff --git a/README.md b/README.md index c30ed38f8..cab810f5d 100644 --- a/README.md +++ b/README.md @@ -229,15 +229,15 @@ module.exports = { ## Reformatting the code -Tools like [Prettier](https://prettier.io), ESLint/TSLint, or stylelint can reformat your code according to an appropriate config by running `prettier --write`/`eslint --fix`/`tslint --fix`/`stylelint --fix`. After the code is reformatted, we want it to be added to the same commit. This can be done using following config: +Tools like [Prettier](https://prettier.io), ESLint/TSLint, or stylelint can reformat your code according to an appropriate config by running `prettier --write`/`eslint --fix`/`tslint --fix`/`stylelint --fix`. Lint-staged will automatically add any modifications to the commit as long as there are no errors. ```json { - "*.js": ["prettier --write", "git add"] + "*.js": "prettier --write" } ``` -Starting from v8, lint-staged will stash your remaining changes (not added to the index) and restore them from stash afterwards if there are partially staged files detected. This allows you to create partial commits with hunks using `git add --patch`. See the [blog post](https://medium.com/@okonetchnikov/announcing-lint-staged-with-support-for-partially-staged-files-abc24a40d3ff) +Prior to version 10, tasks had to manually include `git add` as the final step. This behavior has been integrated into lint-staged itself in order to prevent race conditions with multiple tasks editing the same files. If lint-staged detects `git add` in task configurations, it will show a warning in the console. Please remove `git add` from your configuration after upgrading. ## Examples @@ -273,7 +273,7 @@ _Note we don’t pass a path as an argument for the runners. This is important s ```json { - "*.js": ["eslint --fix", "git add"] + "*.js": "eslint --fix" } ``` @@ -285,7 +285,7 @@ If you wish to reuse a npm script defined in your package.json: ```json { - "*.js": ["npm run my-custom-script --", "git add"] + "*.js": "npm run my-custom-script --" } ``` @@ -293,7 +293,7 @@ The following is equivalent: ```json { - "*.js": ["linter --arg1 --arg2", "git add"] + "*.js": "linter --arg1 --arg2" } ``` @@ -313,19 +313,19 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia ```json { - "*.{js,jsx}": ["prettier --write", "git add"] + "*.{js,jsx}": "prettier --write" } ``` ```json { - "*.{ts,tsx}": ["prettier --write", "git add"] + "*.{ts,tsx}": "prettier --write" } ``` ```json { - "*.{md,html}": ["prettier --write", "git add"] + "*.{md,html}": "prettier --write" } ``` @@ -338,19 +338,19 @@ For example, here is `jest` running on all `.js` files with the `NODE_ENV` varia } ``` -### Run PostCSS sorting, add files to commit and run Stylelint to check +### Run PostCSS sorting and Stylelint to check ```json { - "*.scss": ["postcss --config path/to/your/config --replace", "stylelint", "git add"] + "*.scss": "postcss --config path/to/your/config --replace", "stylelint" } ``` -### Minify the images and add files to commit +### Minify the images ```json { - "*.{png,jpeg,jpg,gif,svg}": ["imagemin-lint-staged", "git add"] + "*.{png,jpeg,jpg,gif,svg}": "imagemin-lint-staged" } ``` @@ -367,7 +367,7 @@ See more on [this blog post](https://medium.com/@tomchentw/imagemin-lint-staged- ```json { - "*.{js,jsx}": ["flow focus-check", "git add"] + "*.{js,jsx}": "flow focus-check" } ``` diff --git a/lib/runAll.js b/lib/runAll.js index 06915049b..21c1cef61 100644 --- a/lib/runAll.js +++ b/lib/runAll.js @@ -102,7 +102,7 @@ module.exports = async function runAll( if (hasDeprecatedGitAdd) { logger.warn(`${symbols.warning} ${chalk.yellow( - `Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command.` + `Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index.` )} `) } diff --git a/test/runAll.unmocked.spec.js b/test/runAll.unmocked.spec.js index b76e6b948..76a20c21f 100644 --- a/test/runAll.unmocked.spec.js +++ b/test/runAll.unmocked.spec.js @@ -340,7 +340,7 @@ describe('runAll', () => { expect(error.message).toMatch('Another git process seems to be running in this repository') expect(console.printHistory()).toMatchInlineSnapshot(` " - WARN ‼ Detected a task using \`git add\`. Lint-staged version 10 will automatically add any task modifications to the git index, and you should remove this command. + WARN ‼ Some of your tasks use \`git add\` command. Please remove it from the config since all modifications made by tasks will be automatically added to the git commit index. ERROR × lint-staged failed due to a git error. @@ -360,17 +360,17 @@ describe('runAll', () => { // But local modifications are gone expect(await execGit(['diff'])).not.toEqual(diff) expect(await execGit(['diff'])).toMatchInlineSnapshot(` - "diff --git a/test.js b/test.js - index f80f875..1c5643c 100644 - --- a/test.js - +++ b/test.js - @@ -1,3 +1,3 @@ - module.exports = { - - 'foo': 'bar', - -} - + foo: \\"bar\\" - +};" - `) + "diff --git a/test.js b/test.js + index f80f875..1c5643c 100644 + --- a/test.js + +++ b/test.js + @@ -1,3 +1,3 @@ + module.exports = { + - 'foo': 'bar', + -} + + foo: \\"bar\\" + +};" + `) expect(await readFile('test.js')).not.toEqual(testJsFileUgly + appended) expect(await readFile('test.js')).toEqual(testJsFilePretty) @@ -424,13 +424,13 @@ describe('runAll', () => { } expect(await readFile('test.js')).toMatchInlineSnapshot(` - "<<<<<<< HEAD - module.exports = \\"foo\\"; - ======= - module.exports = \\"bar\\"; - >>>>>>> branch-b - " - `) + "<<<<<<< HEAD + module.exports = \\"foo\\"; + ======= + module.exports = \\"bar\\"; + >>>>>>> branch-b + " + `) // Fix conflict and commit using lint-staged await writeFile('test.js', fileInBranchB) @@ -444,12 +444,12 @@ describe('runAll', () => { // Nothing is wrong, so a new commit is created and file is pretty expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('4') expect(await execGit(['log', '-1', '--pretty=%B'])).toMatchInlineSnapshot(` - "Merge branch 'branch-b' + "Merge branch 'branch-b' - # Conflicts: - # test.js - " - `) + # Conflicts: + # test.js + " + `) expect(await readFile('test.js')).toEqual(fileInBranchBFixed) }) @@ -490,13 +490,13 @@ describe('runAll', () => { expect(await execGit(['rev-list', '--count', 'HEAD'])).toEqual('1') expect(await execGit(['log', '-1', '--pretty=%B'])).toMatch('initial commit') expect(await readFile('README.md')).toMatchInlineSnapshot(` - "# Test + "# Test - ## Amended + ## Amended - ## Edited - " - `) + ## Edited + " + `) expect(await readFile('test-untracked.js')).toEqual(testJsFilePretty) const status = await execGit(['status']) expect(status).toMatch('modified: README.md')