lefthook icon indicating copy to clipboard operation
lefthook copied to clipboard

Documentation to work with commitlint?

Open radiosilence opened this issue 4 years ago • 9 comments

Unfortunately the commitlint documentation only has these instructions:

{
  "husky": {
    "hooks": {
      "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
    }
  }
}

And it's unclear whether lefthook exposes whatever HUSKY_GIT_PARAMS are in such a way that it works with commitlint.

radiosilence avatar Mar 24 '20 16:03 radiosilence

Hi @radiosilence It is impossible to store documentation for every tool in the world. Check the full guide and if you have a questions feel free to ask.

Maybe that what a you looking for? https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#bash-script-example

Arkweid avatar Mar 25 '20 09:03 Arkweid

I use it like this:

pre-commit:
  parallel: true
  commands:
    lint-commit:
      run: npx commitlint --from HEAD~1 --to HEAD

muuvmuuv avatar May 05 '20 10:05 muuvmuuv

@muuvmuuv Somehow, this doesn't work for me. I also read, that the pre-commit hook should not work for this usecase. Apparently, the commit-msg hook should be used instead. Though this won't work for me as well :/

tillsanders avatar Jun 13 '20 13:06 tillsanders

@Arkweid I took a look at your example using the bash script. I'm trying to figure out how this could be used with a tool like commitlint without using a separate script file. Is there a way to access the commit message in the run command? Maybe using an environment variable or an argument?

tillsanders avatar Jun 13 '20 13:06 tillsanders

For simple run lefthook not pass arguments from git. If you need just a commit message, so as a mentioned above you could use a various git commands like git show

Arkweid avatar Jun 15 '20 09:06 Arkweid

Oh, sorry, noticed that my above does not work, but this one does:

commit-msg:
  commands:
    lint-commit-msg:
      run: npx commitlint --edit
  --edit, -e             read last commit message from the specified file or fallbacks to ./.git/COMMIT_EDITMSG

muuvmuuv avatar Jun 15 '20 10:06 muuvmuuv

I have a similar use-case with git-notify, where I need to pass Git parameters to post-merge, post-rewrite and post-checkout hooks, with the params being dynamic — https://github.com/jevakallio/git-notify#installing-hooks-by-any-other-means

Has someone had success doing this?

EDIT: I can see in https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#git-hook-argument-shorthands-in-commands that a specific Git hook argument at position i can be accessed with {i}, but is there a way I can access all arguments?

paambaati avatar Mar 03 '21 05:03 paambaati

@paambaati where is this related to Commitlint? Post a new issue for this please.

muuvmuuv avatar Mar 03 '21 05:03 muuvmuuv

commit-msg:
  commands:
    lint-commit-msg:
      run: npx commitlint --edit

For me the above one was linting previous commit message instead of the current one. After reading lefthooks for a while I solved doing the following.

run:

lefthook add -d commit-msg
# will create .lefthook/commit-msg dir

Inside .lefthook/commit-msg create a bash script called commit_check and add the following.

if ! npx commitlint --edit --verbose; then
    exit 1
fi

Then in lefthook.yml add the following

commit-msg:
  scripts:
    'commit_check':
      runner: bash

Ref: https://github.com/evilmartians/lefthook/blob/master/docs/full_guide.md#managing-scripts

PawFV avatar Jan 25 '22 18:01 PawFV

I think we can close this since this guide already solved this issue.

tenshiAMD avatar Sep 01 '22 21:09 tenshiAMD