Skip to content

Commit

Permalink
fix: update warning about git add, and to README
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 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.
  • Loading branch information
iiroj committed Nov 14, 2019
1 parent cce9809 commit 6467a66
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
28 changes: 14 additions & 14 deletions README.md
Expand Up @@ -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

Expand Down Expand Up @@ -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"
}
```

Expand All @@ -285,15 +285,15 @@ 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 --"
}
```

The following is equivalent:

```json
{
"*.js": ["linter --arg1 --arg2", "git add"]
"*.js": "linter --arg1 --arg2"
}
```

Expand All @@ -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"
}
```

Expand All @@ -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"
}
```

Expand All @@ -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"
}
```

Expand Down
2 changes: 1 addition & 1 deletion lib/runAll.js
Expand Up @@ -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.`
)}
`)
}
Expand Down
58 changes: 29 additions & 29 deletions test/runAll.unmocked.spec.js
Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
})

Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 6467a66

Please sign in to comment.