Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

aaditya89 commented Jan 13, 2017

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 ?

@Mashewnutz

This comment has been minimized.

Copy link

Mashewnutz commented Jan 25, 2017

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

This comment has been minimized.

Copy link

michalkleiner commented May 26, 2017

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

This comment has been minimized.

Copy link

sebinsua commented Jun 19, 2017

@opyate

This comment has been minimized.

@bcowgill

This comment has been minimized.

Copy link

bcowgill commented Jan 24, 2018

I believe that last line should be:

echo "Profit \$$$"

@swmagesh

This comment has been minimized.

Copy link

swmagesh commented Mar 17, 2018

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

@pgilad

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

dberstein commented Apr 14, 2018

@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

This comment has been minimized.

Copy link

legend80s commented Jun 30, 2019

configuration free git-commit-msg-linter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.