0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gitシリーズ - ②コミットコメントにフォーマットバリデーションを追加してみた

0
Last updated at Posted at 2017-07-24

はじめに

  • 前回に続いて、今回もhook scriptを使ってみました。

フォーマットの作成

今回は、コミット時にコミットメッセージが "[コミット種別] #チケット番号 コミットメッセージ" のフォーマットになっているか確認します。

フォーマット

[fix|hotfix|add|modify|change|clean|remove|upgrade|revert] #(数字を1文字以上) (コメントを1文字以上)

バリデーションの設定

$ vi .git/hooks/commit-msg
commit-msg
# !/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message.  The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit.  The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".

# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1" 

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

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

# コミットコメントのチェック
#   コミットメッセージが以下のフォーマットでない場合、コミットさせない
#   [fix|hotfix|add|modify|change|clean|remove|upgrade|revert] #(数字を一文字以上) メッセージを一文字以上
if grep "\[\(fix\|hotfix\|add\|modify\|change\|clean\|remove\|upgrade\|revert\)\] \#\([0-9]\+\) \(.\+\)" $1 > /dev/null; then
  exit 0
else
  echo "コミットコメントが不正です!!" 
  echo "コミットコメントは \"[{コミット種別}] #{チケット番号} メッセージ\" のフォーマットで入力してください。" 
  echo "{コミット種別} : fix, hotfix, add, modify, change, clean, remove, upgrade, revert のいずれかを格納" 
  echo "{チケット番号} : 数値を格納" 

  exit 1
fi

シリーズページリンク

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?