Skip to content

Instantly share code, notes, and snippets.

@nishio-dens
Last active October 6, 2015 12:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishio-dens/1437642199087fa23290 to your computer and use it in GitHub Desktop.
Save nishio-dens/1437642199087fa23290 to your computer and use it in GitHub Desktop.
自分用railsなプロジェクトでgitのコミットメッセージを一部自動生成するスクリプト
#!/usr/bin/env ruby
#
# .git/hooks/prepare-commit-msg に配置する
# RAILS用コミットメッセージ追加
if ARGV[1] && ARGV[1].include?("message")
files = `git diff --cached --name-only`
change_files = files.split("\n")
prefix = []
if change_files.any? { |v| v.include?("app/controllers/") }
prefix << "controller"
end
if change_files.any? { |v| v.include?("app/models/") }
prefix << "model"
end
if change_files.any? { |v| v.include?("app/views/") }
prefix << "view"
end
if change_files.any? { |v| v.include?("app/decorators/") }
prefix << "decorator"
end
if change_files.any? { |v| v.include?("app/services/") }
prefix << "service"
end
if change_files.any? { |v| v.include?("app/helpers/") }
prefix << "helper"
end
if change_files.any? { |v| v.include?("app/assets/javascripts/") }
prefix << "js"
end
if change_files.any? { |v| v.include?("app/assets/stylesheets/") }
prefix << "css"
end
if change_files.any? { |v| v.include?("spec/") }
prefix << "spec"
end
if change_files.any? { |v| v.include?("lib/") }
prefix << "lib"
end
if change_files.any? { |v| v.include?("db/migrate") }
prefix << "db migrate"
end
commit_messages = File.readlines(ARGV[0]).join("")
# : を含んでいるときは自分でprefixを書いてる時
if commit_messages.include?(":")
puts "SKIP RAILS PREFIX"
elsif commit_messages.include?("@@ignore")
puts "IGNORE PREFIX"
message = commit_messages.gsub("@@ignore ","")
open(ARGV[0], 'w') do |file|
file.puts message
end
else
puts "ADD RAILS PREFIX -- #{prefix.join(", ")}"
open(ARGV[0], 'w') do |file|
branch_raw = `git rev-parse --abbrev-ref HEAD`
# ブランチ名は projectname-xxx (xxxはissue番号)
branch = branch_raw.match(/projectname-(\d+)/)
if branch
file.print "[PROJECTNAME-#{branch[1]}] "
end
if prefix.any?
file.print "#{prefix.join(" & ")}"
file.print(": ")
end
file.puts commit_messages
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment