hamburger-tech-nits

主にプログラミングのNITSな話

Sourcetreeでcommitメッセージにブランチ名を挿入する

過去記事

SourceTreeでブランチ名をコミットメッセージの先頭に自動挿入する - hamburger


commitメッセージにブランチ名を挿入することで、あとからログを追うときに作業ブランチ名を追いやすくしたいというのがモチベーション。issue番号をブランチ名に関連付けて開発しているときに便利。

commit-msgファイルを作成する

$ cd .git/hooks/
$ cp commit-msg.sample commit-msg

commit-msgファイルを編集する

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file, :encoding => Encoding::UTF_8)

current_branch = `git branch | grep '*'`.chomp.sub('* ', '')
current_branch = current_branch[current_branch.rindex("/")+1 .. current_branch.length]

newmessage = message.sub(/branchname/, current_branch)

File.write(message_file, newmessage)

Sourcetreeの設定 > コミットで、メッセージテンプレートを編集