Gitのコミットメッセージ ~ fit-commitを参考に
これまであまりメッセージの書き方を意識していなかったので、メッセージをチェックしてくれるfit-commitを参考に記事にしてみる。
fit-commit
A Git hook to validate your commit messages based on community standards.
チェック内容
以下、ファイル単位で内容を記載。
capitalize_subject.rb
if lineno == 1 && text[0] =~ /[[:lower:]]/
add_error(lineno, "Begin all subject lines with a capital letter.")
end
- 1行目は大文字で始める。
frathouse.rb
if Swearjar.default.profane?(text)
add_error(lineno, "No frat house commit messages in shared branches.")
end
- 汚い言葉を使わない。
line_length.rb
if lineno == 1 && text.empty?
add_error(lineno, "Subject line cannot be blank.")
elsif lineno == 2 && !text.empty?
add_error(lineno, "Second line must be blank.")
elsif line_too_long?(text)
add_error(lineno, format("Lines should be <= %i chars. (%i)",
max_line_length, text.length))
elsif lineno == 1 && text.length > subject_warn_length
add_warning(lineno, format("Subject line should be <= %i chars. (%i)",
subject_warn_length, text.length))
end
- 1行目は空行にしない。
- 2行目は空行にする。
- 一つの行を長くしない。(デフォルト:72文字)
- 1行目を長くしない。(デフォルト:50文字)
subject_period.rb
if lineno == 1 && text.end_with?(".")
add_error(lineno, "Do not end your subject line with a period.")
end
- 1行目は「.」(ピリオド)で終わる。
tense.rb
VERB_BLACKLIST = %w(
adds adding added
# (以下省略)
)
def validate_line(lineno, text)
if lineno == 1 && starts_with_blacklisted_verb?(text)
add_error(lineno, "Message must use imperative present tense.")
end
end
def starts_with_blacklisted_verb?(text)
first_word = text.split.first(2).detect { |w| w =~ /\A\w/ }
first_word && VERB_BLACKLIST.include?(first_word.downcase)
end
- メッセージには現在時制を使わなければならない。
現在時制(げんざいじせい)とは、言語で現在の行為、現象、状態等を表現する時制。用いられる範囲は言語により異なる。一般には過去時制および未来時制と対立するが、この区別も言語によって大きく異なる。
wip.rb
if lineno == 1 && text.split.any? { |word| word == "WIP" }
add_error(lineno, "Do not commit WIPs to shared branches.")
end
- 作業中(Work in Progress:WIP)のものを共有リポジトリにコミットしない。(デフォルト:master)
fit-commit活用
これに「Gitのコミットメッセージの書き方」の内容やプロジェクトごとのルールを追加していけば自然ときれいなメッセージが身につきそう。