lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

How prevent commit with empty diff during pre-commit stage?

Open zarabotaet opened this issue 4 years ago • 1 comments

My config

pre-commit:
  parallel: true
  commands:
    eslint:
      glob: "*.{js,jsx}"
      run: npx eslint {staged_files} --fix && git add {staged_files}`

In that case where eslint fixes style rules and diff stand empty I want to prevent commit. 
How can I rich such behavior?

zarabotaet avatar Sep 25 '20 08:09 zarabotaet

Same question here. Here's my lefthook file:

pre-commit:
  parallel: true
  commands:
    lint:
      glob: "*.{js,ts}"
      run: eslint --fix {staged_files} && git add {staged_files}

Now if I make a change to my JS files that ESLint catches and automatically fixes, I should have no commit. However, lefthook does end up creating the commit even though nothing in the file will be different.

AustinGil avatar Jan 15 '21 20:01 AustinGil

You can use these:

pre-commit:
    git_staged:
      # This check helps to avoid a situation where fixes were applied and thus Lefthook raises no fault, but those
      # changes weren't committed, and so wouldn't be pushed to the remote.
      fail_text: Staged changes exist in your Git working copy. Please commit or stash them before pushing.
      files: git diff --name-only --staged
      run: git diff --exit-code --name-status --staged
    git_untracked:
      # This check helps to avoid a situation where Lefthook linters succeed because of an untracked configuration
      # file, and then fail when the repository is cloned without this configuration file.
      fail_text: Untracked changes exist in your Git working copy. Please remove or index them before pushing.
      files: git ls-files --exclude-standard --others
      run: exit 9
    git_unstaged:
      # This check helps to avoid a situation where fixes were applied and thus Lefthook raises no fault, but those
      # changes weren't committed, and so wouldn't be pushed to the remote.
      fail_text: Unstaged changes exist in your Git working copy. Please commit or stash them before pushing.
      files: git diff --name-only
      run: git diff --exit-code --name-status -- {files}

sanmai-NL avatar Dec 14 '23 09:12 sanmai-NL

@zarabotaet Does that resolve the issue? If so, please close.

sanmai-NL avatar Dec 14 '23 09:12 sanmai-NL