Skip to content

Instantly share code, notes, and snippets.

@pgilad
Last active March 31, 2024 22:30
Show Gist options
  • Save pgilad/5d7e4db725a906bd7aa7 to your computer and use it in GitHub Desktop.
Save pgilad/5d7e4db725a906bd7aa7 to your computer and use it in GitHub Desktop.
Git commit-msg hook to validate for jira issue or the word merge
#!/usr/bin/env bash
# set this to your active development branch
develop_branch="develop"
current_branch="$(git rev-parse --abbrev-ref HEAD)"
# only check commit messages on main development branch
[ "$current_branch" != "$develop_branch" ] && exit 0
# regex to validate in commit msg
commit_regex='(wap-[0-9]+|merge)'
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-1111') or 'Merge'"
if ! grep -iqE "$commit_regex" "$1"; then
echo "$error_msg" >&2
exit 1
fi

Instructions

  • copy the file commit-msg to .git/hooks/commit-msg
  • make sure your delete the sample file .git/hooks/commit-msg.sample
  • Make commit msg executable. chmod +x .git/hooks/commit-msg
  • Edit commit-msg to better fit your development branch, commit regex and error message
  • Profit $$

Shell example

curl https://gist.githubusercontent.com/pgilad/5d7e4db725a906bd7aa7/raw/feba0ca462f87a382cfbc3eddfcc529ceb9b7350/commit-msg.sh > .git/hooks/commit-msg

rm .git/hooks/commit-msg.sample

chmod +x .git/hooks/commit-msg

vim .git/hooks/commit-msg

echo "Profit $$"
@aaditya89
Copy link

The commit msg hook can be enforced on local repository by adding it manually.
Is there a way we can push this hook to remote repository and everyone who clones it will be enforced to commit messages in certain format ?

@thekiwicoder0
Copy link

You'd need to write a server-side commit hook for that

This might help: https://git-scm.com/book/uz/v2/Customizing-Git-An-Example-Git-Enforced-Policy

@michalkleiner
Copy link

Alternatively, if your projects use any sort of automation, e.g. using Composer or npm or other to install dependencies, you can use that to install the git hook.

@sebinsua
Copy link

sebinsua commented Jun 19, 2017

@opyate
Copy link

opyate commented Oct 10, 2017

@bcowgill
Copy link

bcowgill commented Jan 24, 2018

I believe that last line should be:

echo "Profit \$$$"

@swmagesh
Copy link

swmagesh commented Mar 17, 2018

How to validate the code error free before commit? Anyone please help!!!

@pgilad
Copy link
Author

pgilad commented Apr 11, 2018

@swmagesh, what do you mean by error free? Do you mean running lint or tests before the commit?

@dberstein
Copy link

@pgilad inspired by your and @opyate work, I've crafted this: https://gist.github.com/dberstein/dcc50e171163c3f6e0f23b2b5de5dd49

It auto inserts into the message Jira issues found in branch's name but not in the content otherwise behaves like yours. For example commit in branch "WAP-1234.ThisIsAPullRequestBranch" with message "support for WAP-567" will commit message "WAP-1234, support for WAP-567".

@legend80s
Copy link

configuration free git-commit-msg-linter

@UncleCJ
Copy link

UncleCJ commented May 6, 2020

Found this old gem - much appreciated! I expanded it and shared with the corporation:

#!/usr/bin/env bash

commit_regex = '^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|Publish)(\([a-zA-Z0-9 /_,.-]+?\))?: .{1,59}\w (\([A-Z]+-[0-9]+\)|\(#[0-9]+\)|\[skip ci\]|\[ci\])|Merge branch .+|(fixup|squash)! .+|WIP: .+)$'

error_msg = '
.git/hooks/commit-msg: Aborting commit, try again with a valid message. The regexp checks for:

* a conventional commit message subject (to be parsed into changelogs, see https://www.conventionalcommits.org/en/v1.0.0 ):
   - a change type, either of "build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test" or "Publish"
   - optionally, in parenthesis, subsystem(s) affected by the change (comma delimited list)
   - a colon, space (": ") and a brief subject (60 characters maximum) not ending with a period
   - a space and in parenthesis, an issue id " (FOO-123)" or " (#123)"... or the tags "[skip ci]" or "[ci]" (do we use these?)
* ... or, a Git autosquash line, i.e. "fixup! My previous commit" or "squash! Another previous commit" (see https://thoughtbot.com/blog/autosquashing-git-commits )
* ... or, a merge commit "Merge branch x..."
* ... or, a work-in-progress tag "WIP: "
* optionally (not validated), two line breaks and a commit message body
This script is written by CJ Sveningsson ([email protected]), inspired by https://gist.github.com/pgilad/5d7e4db725a906bd7aa7 (Gilad Peleg)'

if ! grep -iqE "${commit_regex}" "$1"; then
    echo "${error_msg}" >&2
    exit 1
fi

@amahpour
Copy link

I wrote a very simple syntax checker for N amount of characters and numbers as the Jira key (e.g. ABCDE-12345):

#!/bin/bash
[[ $(cat $1) =~ [A-Z]+-[0-9]+.* ]] || {
    echo >&2 "ERROR: Commit message requires JIRA key. Example: \"ABC-123: Test Commit\""
    exit 1
}

Hope it helps for anyone looking for a super basic checker.

@Reimirno
Copy link

Reimirno commented Jul 3, 2022

Hi,

I wrote a regex for commit message convention 1.0.0 which is based off the Angular Convention.

commit_regex='^(feat|fix|build|chore|docs|style|refactor|perf|test)(\(.+\))?!?: (.+[^.\r\n])([\r\n]+(.+[\r\n]+)+)?$'

This is a simple, lightweight commit message convention enforcing a format like this:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

This should work out of box but you can modify it the way you want. Hope it's helpful!

@umutdz
Copy link

umutdz commented Nov 18, 2022

Hi Remirno, you should add the '?' question mark before group 2 because the scope is not a positional argument in the conventional commit,
so it should be like this;
^(feat|fix|build|chore|docs|style|refactor|perf|test)?(\(.+\))?!?: (.+[^.\r\n])([\r\n]+(.+[\r\n]+)+)?$

@KetulRadadiya
Copy link

You'd need to write a server-side commit hook for that

This might help: https://git-scm.com/book/uz/v2/Customizing-Git-An-Example-Git-Enforced-Policy

@thekiwicoder0 I know this is an old issue. can you provide an example reference or more details??

@umutdz
Copy link

umutdz commented Nov 29, 2022

@jorimvanhove
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment