4

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

Ezeewei
  • 7,162
  • 11
  • 57
  • 110
  • Would a pre-commit hook work for you? https://git-scm.com/docs/githooks#_pre_commit – VonC Dec 08 '17 at 06:01
  • @VonC tried it, the pre-commit hook will not terminate the original git commit, but continuing it, even commitizen popuped, so commitizen is not able to be utilized even it got ran... thought? – Ezeewei Dec 08 '17 at 13:36
  • A post-commit hook, then? – VonC Dec 08 '17 at 13:41
  • @VonC nop, that will execute the commit already – Ezeewei Dec 08 '17 at 15:25
  • > but continuing it, even commitizen popuped. If commitizen fails (exit statis -1, the pre-commit should block the commit. – VonC Dec 08 '17 at 16:00
  • but what I try to do is when someone do `git commit` just direct them to the git cz interface without his commit get commited.... maybe I do not understand what you try to convey. – Ezeewei Dec 08 '17 at 16:19
  • OK, I was just about executing the tool, not redirecting to the tool. – VonC Dec 08 '17 at 17:02
  • @VonC yea, the tool executes commitizen, but not really overriding the actual git commit. – Ezeewei Dec 08 '17 at 17:31
  • You should be able to use https://github.com/commitizen/cz-cli – thoroc Jun 17 '20 at 10:31

2 Answers2

3

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.

eedrah
  • 1,820
  • 14
  • 29
1

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 :)

keponk
  • 262
  • 1
  • 3
  • 14