Help us understand the problem. What is going on with this article?

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

はじめに

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

作業ブランチ
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)

以上です

QoopMk
Android & iOS Engineer Freelance
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした