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(cli): Implement --hook option for git hooks integration #615

Merged
merged 2 commits into from Apr 18, 2019

Conversation

olgn
Copy link
Contributor

@olgn olgn commented Mar 19, 2019

fixes #448
re #462

This pr allows project maintainers to enforce Commitizen generated commit messages as part of the workflow triggered by the git commit command.

  • implements the --hook flag, which directs Commitizen to edit the .git/COMMIT_EDITMSG file directly.
  • documents the use of the --hook flag in the README, both through traditional git hooks and husky.

@olgn
Copy link
Contributor Author

olgn commented Mar 19, 2019

First of all I'd like to say thanks to all the contributors and maintainers for making such a useful tool. A little backstory on this pull request - as a developer on the open-source BIDS-Validator project, I wished to use commitizen as a way to ensure that commit messages were conducive to automated builds from semantic-release. Adding commitlint in a husky commit-msg hook ensures that bad commit messages do not get into the repo - which is good for the project, but can be somewhat discouraging to contributors who are not familiar with commitizen. I can imagine frustration (and a corresponding influx of new github issues to deal with) from would-be contributors that don't know about yarn commit and use git commit... resulting in an error. Can we get commitizen to run when the contributors type git commit? There is user demand for this feature but no existing attempts to implement - so I hope this serves at least as a jumping off point for this feature.

@olgn
Copy link
Contributor Author

olgn commented Mar 19, 2019

Snippet from the README for use-cases

Optional: Running Commitizen on git commit

This example shows how to incorporate Commitizen into the existing git commit workflow by using git hooks and the --hook command-line option. This is useful for project maintainers
who wish to ensure the proper commit format is enforced on contributions from those unfamiliar with Commitizen.

Once either of these methods is implemented, users running git commit will be presented with an interactive Commitizen session that helps them write useful commit messages.

NOTE: This example assumes that the project has been set up to use Commitizen locally.

Traditional git hooks

Update .git/hooks/prepare-commit-msg with the following code:

#!/bin/bash
exec < /dev/tty
node_modules/.bin/git-cz --hook
Husky

For husky users, add the following configuration to the project's package.json:

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

Why exec < /dev/tty? By default, git hooks are not interactive. This command allows the user to use their terminal to interact with Commitizen during the hook.

@olgn olgn force-pushed the add-hook-option branch 5 times, most recently from 4917d18 to 0630aef Compare March 19, 2019 23:56
@olgn olgn changed the title feat(cli): Implement --hook option for git hooks integration [WIP] feat(cli): Implement --hook option for git hooks integration Mar 19, 2019
@olgn olgn force-pushed the add-hook-option branch 7 times, most recently from 88d32b1 to db15306 Compare March 20, 2019 18:02
using git cz --hook in a git hook will allow 'git commit' to trigger commitizen

fix commitizen#448, re commitizen#462
examples of how to incorporate commitizen into traditional git hooks + husky hooks
@olgn olgn changed the title [WIP] feat(cli): Implement --hook option for git hooks integration feat(cli): Implement --hook option for git hooks integration Mar 20, 2019
@wilddancers
Copy link

hi,I use this option,but it still have a problem like this when I run [git commit]:

git exited with error code 129
husky > prepare-commit-msg hook failed (cannot be bypassed with --no-verify due to Git specs)

@olgn
Copy link
Contributor Author

olgn commented Mar 22, 2019

@wilddancers thanks for finding a bug! i'd like to figure out if this is an error with the code itself or the documentation.What happens if you try the following hook?

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

Also what OS are you using?

@wilddancers
Copy link

@olgn thanks for replaying me,my env is:
macOS Mojave 10.14.3
package.json info:
{ "name": "gittest", "version": "1.0.1", "description": "changeLogs", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -w -r 0" }, "author": "", "license": "ISC", "devDependencies": { "commitizen": "^3.0.7", "cz-conventional-changelog": "^2.1.0", "husky": "^1.3.1" }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" } }, "husky": { "hooks": { "prepare-commit-msg": "exec < /dev/tty && git-cz --hook" } } }

@olgn
Copy link
Contributor Author

olgn commented Mar 25, 2019

@wilddancers I have been able to reproduce this locally due to a failure linking this pr branch to the package manager registry - please try the following steps to get this to work.

First of all, this is a new feature, so you should make sure that the your project's commitzen cli is pointing at the correct branch (via yarn or npm link)

  1. clone the repo that is the source of this pr: git clone https://github.com/olgn/cz-cli
  2. get into the source repo and checkout the pr branch: cd cz-cli && git checkout add-hook-option
  3. tell the yarn or npm registry to use this folder as the source of commitizen: yarn link or npm link
  4. tell your project to use the linked version of commitizen when you run the git cz command: cd <project repo> -> yarn link commitizen or npm link commitizen
  5. verify that you are using the linked version by running the following command in the project repo: ls -la ./node_modules/commitizen - this should look something like ./node_modules/commitizen -> ../../../../.config/yarn/link/commitizen

If all that works out and you still get error code 129, let me know.

@olgn
Copy link
Contributor Author

olgn commented Mar 25, 2019

if you want to use this feature in production, I suggest you wait until this pr is merged... but all review is welcome!

@eladchen
Copy link
Contributor

@olgn I too encountered that error, but the cause was streamich/git-cz adapter.

I have a feeling the '--hook' flag is not being removed from the args and is propagating to the git commit command.

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

Successfully merging this pull request may close these issues.

Finding a way to use git commit
4 participants