Skip to content

Instantly share code, notes, and snippets.

@vikytech
Last active January 16, 2018 12:19
Show Gist options
  • Save vikytech/7b550991af76a3b9741756af80b497d1 to your computer and use it in GitHub Desktop.
Save vikytech/7b550991af76a3b9741756af80b497d1 to your computer and use it in GitHub Desktop.
Git commit message hook
#!/usr/bin/env ruby
$regex = /\A(\[(\w+)(\/(\w*))?(\/(\w*))?\])\s+(\[#\w{2,10}\])\s+(.*)$/i
message_file = ARGV[0]
message = File.read(message_file)
def red_background(text);"\e[41m#{text}\e[0m";end
# enforced short and crisp commit message
if message.length > 150
puts red_background("Change has not been committed!")
puts "Commit message should be short and crisp
Maximum: 150 Characters
If you really need to add content to your message use git description and add `--no-verify` at end of commit command"
exit 1
end
# enforced custom commit message format
unless $regex.match(message)
puts red_background("Change has not been committed!")
puts "Please fix the message and commit again
1) Should start with [Pair Name/Your Name]
2) And #storyNumber OR #Tech OR #Anything else
3) Then should end with your commit message
Example: `[Name1/Name2] [#01] Blah Blah Blah.......`"
exit 1
end
#!/usr/bin/env bash
if [ ! -x .git/hooks/commit-msg ]
then
echo -e "\033[33m Setting Up Git commit Hook......" # Add -e flag if you execute this file as part of gradle task, so colors will appear correctly
mkdir -p .git/hooks/
echo "General format:
1) Should start with [Pair Name/Your Name]
2) And #storyNumber OR #Tech OR #Anything else
3) Then should end with your commit message
Example: [Name1/Name2] #000 Blah Blah Blah......."
else
echo -e "\033[36m Updating Git commit Hook......"
fi
cp commit-msg .git/hooks/commit-msg
chmod +x .git/hooks/commit-msg
echo -e "\033[32m Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment