Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw error to prevent empty commits unless --allow-empty is used #762

Merged
merged 1 commit into from Jan 8, 2020

Conversation

iiroj
Copy link
Member

@iiroj iiroj commented Jan 8, 2020

BREAKING CHANGE: Previously, lint-staged would allow empty commits in the situation where a linter task like "prettier --write" reverts all staged changes automatically. Now the default behaviour is to throw an error with a helpful warning message. The --allow empty option can be used to allow empty commits, or allowEmpty: true for the Node.js API.

What do you think, @okonet?

BREAKING CHANGE: Previously, lint-staged would allow empty commits in the situation where a linter task like "prettier --write" reverts all staged changes automatically. Now the default behaviour is to throw an error with a helpful warning message. The --allow empty option can be used to allow empty commits, or `allowEmpty: true` for the Node.js API.
@iiroj iiroj requested a review from okonet January 8, 2020 18:52
@codecov
Copy link

codecov bot commented Jan 8, 2020

Codecov Report

Merging #762 into beta will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@         Coverage Diff         @@
##           beta   #762   +/-   ##
===================================
  Coverage   100%   100%           
===================================
  Files        13     13           
  Lines       417    429   +12     
  Branches     90     97    +7     
===================================
+ Hits        417    429   +12
Impacted Files Coverage Δ
lib/runAll.js 100% <100%> (ø) ⬆️
lib/gitWorkflow.js 100% <100%> (ø) ⬆️
lib/index.js 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 30b4809...fa2cb8a. Read the comment docs.

@okonet okonet merged commit 8bdeec0 into beta Jan 8, 2020
@okonet
Copy link
Collaborator

okonet commented Jan 8, 2020

I love it!

@okonet okonet deleted the prevent-empty-commit branch January 8, 2020 19:53
@okonet
Copy link
Collaborator

okonet commented Jan 8, 2020

🎉 This PR is included in version 10.0.0-beta.15 🎉

The release is available on:

Your semantic-release bot 📦🚀

@okonet
Copy link
Collaborator

okonet commented Jan 19, 2020

🎉 This PR is included in version 10.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Frozen-byte
Copy link

I do not understand properly how to migrate this change.

Previously I had the following config (git add already removed):

"lint-staged": {
    "app/javascript/**/*.{css,scss}": [
      "./node_modules/.bin/prettier --write"
    ],
    "app/javascript/**/*.{js,jsx}": [
      "./node_modules/.bin/eslint --fix",
      "./node_modules/.bin/prettier --write",
      "./node_modules/.bin/esplint --stage-record-file"
    ]
  }

When I submit a commit with version ^10 with only JSX changes, I will see an error.

I do not want to merge these blocks, due eslint and esplint should not be executed for CSS files.

I am not aware if the following

"lint-staged": {
    "app/javascript/**/*.{css,scss,js,jsx}": [
      "./node_modules/.bin/prettier --write"
    ],
    "app/javascript/**/*.{js,jsx}": [
      "./node_modules/.bin/eslint --fix",
      "./node_modules/.bin/esplint --stage-record-file"
    ]
  }

still ensures prettier is called after eslint and before esplint.

Any Conclusions how to handle it aside just adding allowEmpty: true to my config?

@jinggk
Copy link

jinggk commented Apr 15, 2020

@Frozen-byte i have the same problem, how can i make allowEmpty work with my .lintstagedrc.js file, anyone know?

@iiroj
Copy link
Member Author

iiroj commented Apr 15, 2020

you should add the cli flag to the command where you invoke lint-staged itself, maybe from husky:

{
    "husky": {
        "pre-commit": "lint-staged --allow-empty"
    }
}

@jinggk
Copy link

jinggk commented Apr 15, 2020

I tried it but it seems useless

@iiroj
Copy link
Member Author

iiroj commented Apr 15, 2020

@jinggk That's too bad to hear. Do you mean it didn't work, or that the feature itself is useless? Make sure have the latest version of lint-staged, at the time of this comment v10.1.3.

@jinggk
Copy link

jinggk commented Apr 16, 2020

@iiroj thank you, i confirm my error again, the detail is:

husky > pre-commit (node v12.16.1)
  ✖ Preparing...
    → You do not have the initial commit yet
  ↓ Running tasks... [skipped]
    → Skipped because of previous git error.
  ↓ Applying modifications... [skipped]
    → Skipped because of previous git error.
  ↓ Cleaning up... [skipped]
    → Skipped because of previous git error.

  ✖ lint-staged failed due to a git error.

    The initial commit is needed for lint-staged to work.
    Please use the --no-verify flag to skip running lint-staged.
fatal: bad revision 'HEAD'
fatal: bad revision 'HEAD'
fatal: Needed a single revision
You do not have the initial commit yet
husky > pre-commit hook failed (add --no-verify to bypass)

my configs file content:

module.exports = {
  hooks: {
    'pre-commit': 'lint-staged --allow-empty',
  },
};
module.exports = {
  "*.{gql,html,json,yaml,yml,css,less,js,jsx,ts,tsx}": [
    "prettier --write"
  ],
  "*.{js,jsx,ts,tsx}": [
    "eslint -c .eslintrc.pro.js"
  ]
}

and here is my operation detail:

mkdir test 
cd test
yarn init -y
git init
mkdir src
touch an index.js and write some js content
add my configs 
then git add .
git commit -m 'test. commit'

then the error show

i do not understand why i need an initial commit?? when i add staged, i think it's enough.. can you help me?

add my package.json version

  "husky": "^4.2.5",
   "lint-staged": "^10.1.3"

@iiroj
Copy link
Member Author

iiroj commented Apr 16, 2020

@jinggk different issue, please see the PR that fixes this: #838

Currently lint-staged creates a backup using git stash, and that command requires at least the initial commit.

@jinggk
Copy link

jinggk commented Apr 16, 2020

@iiroj very thank you ,i will check it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

4 participants