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

27,841

Solution 1

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.

Solution 2

Like this:

First, add validate script in your husky config:

// package.json
{
 ...
 "husky": {
  "hooks": {
     "pre-commit": "npm test",
     // if you use validate-commit-msg, this can be "validate-commit-msg"
+    "commit-msg": "sh scripts/my-specific-hook.sh",
     ....
  }
 }
}

And then, have a try...

Everything seems to be OK.

Solution 3

This is for commitlint integration as per the docs:

# Add hook
npx husky add .husky/commit-msg 'npx --no-install commitlint --edit $1'
# or
yarn husky add .husky/commit-msg 'yarn commitlint --edit $1'

Most probably; you may easily customize it with something like this:

yarn husky add .husky/commit-msg 'sh scripts/my-specific-hook.sh $1'
Share:
27,841
arjel
Author by

arjel

Updated on July 09, 2022

Comments

  • arjel
    arjel almost 2 years

    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.

  • arjel
    arjel over 7 years
    First of all, thank you for your help! As mentioned in the edit, I tried to configure the package.json file but it doesn't work at all. I tried the validate-commit-msg too, and that also doesn't work... So far I could run a script ("postinstall": "sh hooks/setup_hooks.sh") to move my hooks in the .git/hooks folder.
  • VonC
    VonC over 7 years
    @remmargorp on which OS are you working on? With which version of npm?
  • arjel
    arjel over 7 years
    I'm on Ubuntu 16.04 and npm is version 4.0.5
  • VonC
    VonC over 7 years
    @remmargorp would register a npm test as pre-commit hook work? (just to get back to a simpler test case, before debugging your more complex script)
  • arjel
    arjel over 7 years
    I tried that "precommit": "npm test" and it works fine. There's no test set up though ...
  • VonC
    VonC over 7 years
    @remmargorp sure: is it possible to modify that npm test to make it do something more (like an echo). And then build up your commit hook from there?
  • arjel
    arjel over 7 years
    As you can see in the Edit2 the "install" script is executed upon npm install. It moves my hook from test/hooks/ to test/.git/hooks. This way it works fine. If I skip this part and the hook remains in the test/hooks folder and I try to commit I get the error shown in the image (bottom terminal).
  • arjel
    arjel over 7 years
    Regarding the "npm test" approach, if I get it right I would just change the call slightly, but trying it out I get the same error. I have the idea that my hook uses variables that exist/are seen only if in the .git/hooks folder :(
  • VonC
    VonC over 7 years
    @remmargorp you could try and implement your script in bash instead of ruby. That way, you should use only known variables.
  • mjakic
    mjakic almost 5 years
    This doesn't pass args to my-specific-hook.sh.
  • Samuel Mburu
    Samuel Mburu over 4 years
    @mjakic husky passes certain arguments to your script detailed here -- npmjs.com/package/husky#access-git-params-and-stdin
  • Ron Wertlen
    Ron Wertlen over 2 years
    npm scripts for enything beside build/package are an anti-pattern. The answer stackoverflow.com/a/55203979/1334781 is much safer and superior to this one.
  • VonC
    VonC over 2 years
    @RonWertlen Good point. I have included your comment in the answer for more visibility.