Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use husky to check a git commit message format?

I'm trying to enforce a git commit message policy to keep my repositories clean and tidy. I've seen the official docs about server-side and client-side hooks and then I bumped on husky.

So far I could work with the first but couldn't set up husky, I've still got plenty to learn. The main idea is to be able to work on a new workstation without having to manually set up any client-side hooks.

Could someone explain how I can set up husky to check my commit messages or even make an example?

This is my commit-msg hook in project-root/githooks folder:

#!/usr/bin/env ruby

message_file = ARGV[0]
message = File.read(message_file)

$regex = /([resolved|fixed]) #([0-9])* ([A-Z])\w+/

if !$regex.match(message)  
  puts "[POLICY] Your message is not formatted correctly!"  
  puts "Message format must be like:"  
  puts "resolved #123 Case title (for features)"  
  puts "fixed #123 Case title    (for bugs)"  
  puts "First letter of 'Case title' must be capitalized!"  
  exit 1  
end  

I've tried to add the script to the package.json:

"scripts": {  
  ... : ...,  
  "commitmsg": "sh hooks/commit-msg",  
  ... : ...  
}  

The hook does not work. All messages pass. If put in .git/hooks it works normally.

package.json and commit-msg hook in a test project

Here's a screenshot of a test project with the package.json, the commit-msg hook and the error it gives out.

The same hook, put in .git/hooks folder, works just fine.

like image 275
arjel Avatar asked Jan 22 '17 00:01

arjel


People also ask

How do I see all commit messages?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do you use a husky Commitlint?

Firstly, we need to install the commitlint CLI and add a commitlint config (in this case the default Conventional Commits Config). Now we need to install husky to run commitlint as a pre-commit hook. Now that we are done installing husky, we need to add a pre-commit hook to run commitlint before the code is committed.

What is husky used for Git?

Husky is a dotnet tool that allows you to run arbitrary actions on specific git hooks. These could be to enforce the same sort of things that you would do centrally, but here they would run before committing code into git. For example, you could ensure your code built and passed all unit tests.

How should git commit message look like?

It suggests that a commit message should consist of a header, a body, and a footer with a blank line between each section because tools like rebase in Git get confused if we run them together without space. The header consists of a type and a summary part. Some add an optional “scope” in between.


1 Answers

See issue 81

First, check

npm config get ignore-scripts # should be false

Then in a git repo:

npm install husky --save-dev

You can then add hooks (here a pre-commit and pre-push) to npm (package.json), the idea being those hook definitions are versions in that package.json file (part of your git repo sources).

Although, Ron Wertlen comments that npm scripts for anything beside build/package are an anti-pattern.
See Jerry Zheng's answer for a better practice.

https://camo.githubusercontent.com/89b1f62d0f2f8b73cad2c70faec7b45d9957c41f/68747470733a2f2f692e696d6775722e636f6d2f794844734d32522e706e67

You can also declare existing regular bash hooks (issue 92)

{
  "scripts": {
    "precommit": "sh scripts/my-specific-hook.sh"
  }
}

You can then use validate-commit-msg to validate your commit message.

add "commitmsg": "validate-commit-msg" to your npm scripts in package.json.

like image 188
VonC Avatar answered Sep 20 '22 11:09

VonC