ey dee ey em
ey dee ey em

Reputation: 8623

how to make commitizen override default git commit command

I am currently creating custom script to run commitizen commit command by doing npm run commit but I want to just let it over ride the default git commit with npm run commit somehow..... So anyone does git commit will automatically direct the person to the commitizen interface and ignore whatever the person put after git commit when commitizen is available.

How can I do that? I did google, can't find a viable solution.

Thanks

Upvotes: 4

Views: 2339

Answers (2)

keponk
keponk

Reputation: 320

This is in fact possible. As per the official docs this is achievable using husky

"husky": {
  "hooks": {
    "prepare-commit-msg": "exec < /dev/tty && git cz --hook || true",
  }
}

There's a few gotchas with this also part of the same docs :)

Upvotes: 2

eedrah
eedrah

Reputation: 2395

It is impossible to override the default git command through git itself, but you can put the following in your .bashrc:

function git() {
    case $* in
        commit* ) npm run commit ;; # or yarn commit
        * ) command git "$@" ;;
    esac
}

This will override the git command if the second argument is "commit", and use the normal git command if not. (The command ensures that we do not use our function recursively - it will go directly to the git executable, not back to our defined function.)

See here this answer for more information.

Note the warning in the commitizen docs:

NOTE: if you are using precommit hooks thanks to something like husky, you will need to name your script some thing other than "commit" (e.g. "cm": "git-cz"). The reason is because npm-scripts has a "feature" where it automatically runs scripts with the name prexxx where xxx is the name of another script. In essence, npm and husky will run "precommit" scripts twice if you name the script "commit," and the work around is to prevent the npm-triggered precommit script.

I'd recommend yarn cz/npm run cz instead.

Upvotes: 3

Related Questions