LoginSignup
0
1

More than 3 years have passed since last update.

コピペで使える!!ブランチ名をコミット名に自動付与

Posted at

はじめに

コミットにチケット名入れるの自動化したので共有します。
たとえばこんな感じにします。

作業ブランチ
feature/issue/#9

コミットメッセージ
xxxの実装

↓コミット

コミット名
[#9] xxxの実装

準備

コミットにブランチ名を入れるために、git-hook機能を使います。
以下のコマンド実行してcommit-msgというファイルを作成します。

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

commit-msgファイル編集
スラッシュ区切りの最後の文字列を入れてるだけです。
アンダーバーとかに変更したい場合は任意に変更してください。

#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file, :encoding => Encoding::UTF_8)
# remove prefix issue number like [#1234] from COMMIT_EDITMSG
message = message.sub(/^\[#[0-9A-Za-z_].*\]/, "")
# remove comment
message = message.gsub(/^#(?! ------------------------ >8 ------------------------).*\n|^\n/, "")
if message =~ /(?=\A)# ------------------------ >8 ------------------------\n/
  puts "An empty message aborts the commit."
  exit 1
end
# get last path of current branch
current_branch = `git branch | grep '*'`.chomp.sub('* ', '')
current_branch = current_branch[current_branch.rindex("/")+1 .. current_branch.length] # スラッシュ区切りにしてるが任意の文字に変えることも可能
# add [last path of current branch] to commit message
newmessage = "[#{current_branch}] #{message}" # コミットメッセージ作成
File.write(message_file, newmessage)

以上です

0
1
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
1