Skip to content

Instantly share code, notes, and snippets.

@matsubo
Forked from iberianpig/commit-msg
Created January 17, 2024 13:01
Show Gist options
  • Save matsubo/a6976c781e56a0c1ba44daca1cee738d to your computer and use it in GitHub Desktop.
Save matsubo/a6976c781e56a0c1ba44daca1cee738d to your computer and use it in GitHub Desktop.
IssueID_xxxxなブランチ名の時に自動でコミットメッセージの先頭に[#IssueID]を付与するrubyスクリプト
#!/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 number([#1234]) from branch(1234_foobar)
number = `git branch | grep "*"`[/\d+/].to_i
# add [#1234] to commit message
if 0 < number
message = "[##{number}] #{message}"
end
File.write(message_file, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment