hamburger

主に日記

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

はじめに

手順は9割以下のサイトと同じです。やってみたら少しだけ詰まったので、差分をメモしています

mono0926.com

目的

issue番号をブランチ名にしているので、それをcommitメッセージにも追加することであとから辿りやすくしたい

手順

hooksディレクトリの確認

本来なら.git/hooksディレクトリがある。ない場合はリポジトリrootでgit initコマンドを実行する

任意のリポジトリ内の.git/hooks/commit-msg.sample.git/hooks/commit-msgにリネームする

直接ファイル作成をするとcommit時に正しく起動しなかったので、.sampleファイルをリネームした

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の設定からコミットテンプレートを変更する

f:id:burgerham:20210405143316p:plain

メッセージが置き換わらない

hookのスクリプトが動いていない

stackoverflow.com

$ pwd
<repo>/.git/hooks
$ chmod +x commit-msg