Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a pattern for git commit messages?

Tags:

git

I want to restrict the people whoever commits to have a specific commit message format, how do I do that?

For example: Pair_Name|Story_Number|Commit_Message

like image 422
Infant Dev Avatar asked Jan 04 '13 05:01

Infant Dev


People also ask

How to write a commit message in Git?

A single line is almost never sufficient for a commit message, so it is almost always better to write your commit message in an editor. The default editor, Vim, is not very user friendly but Git allows you to set the editor of your choice for writing commit messages (see how to Set Your Git Commit Message Editor ). 2. First line is the subject

How to write a git commit subject line without a period?

In the same way you don’t end an email subject line with a period, your Git commit subject line does not need a period either (plus with a 50 character limit you’ll want to save every character you can). 3. Skip a line after the subject This visually separates the subject line from the body. 4. The rest of the commit is the body

How do I Save changes in Git?

However, before you can save changes in Git, you have to tell Git which changes you want to save as you might have made tons of edits. A great way to do that is by adding a commit message to identify your changes. This option sets the commit's message. This option automatically commits all (including new) tracked, modified or deleted files.

What is the first line of a commit message?

First line is the subject In the same way you summarize an email with a subject line, the first line of your commit should summarize your commit. The easiest way for me to write a commit message in the imperative mood is to imagine the words “This will” and then whatever comes next becomes my commit message subject line.


1 Answers

There is a pre-commit-msg or commit-msg hook, that you could use:

  • http://www.kernel.org/pub/software/scm/git/docs/githooks.html

Git repos come with sample hooks, e.g. the sample commit-msg hook under git/hooks/commit-msg.sample catches duplicate Signed-off-by lines.

# This example catches duplicate Signed-off-by lines.

test "" = "$(grep '^Signed-off-by: ' "$1" |
    sort | uniq -c | sed -e '/^[   ]*1[    ]/d')" || {
    echo >&2 Duplicate Signed-off-by lines.
    exit 1
}

To enable a hook, don't forget to make it executable.


 

Here's some fictional example, which would only accept commit messages of the london|120|something ... and the like:

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

# $regex = /\[ref: (\d+)\]/

PAIRS = ["london", "paris", "moscow"] # only these names allowed
STORIES = "\d{2,4}"                   # story must be a 2, 3 or 4 digit number
MESSAGE = ".{5,}"                     # message must be at least 5 chars long

$regex = "( (#{PAIRS.join('|')})\|#{STORIES}\|#{MESSAGE} )"

if !$regex.match(message)
  puts "[POLICY] Your message is not formatted correctly"
  exit 1
end

In usage:

$ git ci -m "berlin|120"
[POLICY] Your message is not formatted correctly
$ git ci -m "london|120|XX"    
[POLICY] Your message is not formatted correctly
$ git ci -m "london|120|Looks good."    
[master 853e622] london|120|Looks good.
 1 file changed, 1 insertion(+)
like image 136
miku Avatar answered Oct 13 '22 14:10

miku