Last active
August 29, 2015 14:08
-
-
Save eregon/3f8182f48b2aa667041e to your computer and use it in GitHub Desktop.
A script to fill the ruby/ruby ChangeLog with the commit information as a Git commit hook.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# cd .git/hooks && mv PATH/TO/THIS_FILE.rb post-commit && chmod +x post-commit | |
# https://gist.github.com/eregon/3f8182f48b2aa667041e | |
def error msg | |
$stderr.puts msg | |
exit 0 # We would not want to stop anything | |
end | |
error "Need ruby >= 2.0" unless RUBY_VERSION >= "2.0" and RUBY_ENGINE == 'ruby' | |
commit = `git log --format=medium --date=raw -1`.lines.map(&:chomp) | |
error commit[2] unless /^Date: (?<date>\d+)/ =~ commit[2] | |
date = Time.at(Integer(date)) | |
time = date.localtime("+09:00").ctime | |
error commit[1] unless /^Author: (?<author>.+) <(?<email>.+)>$/ =~ commit[1] | |
me = `git config user.name`.chomp | |
error "Ignoring commit by #{author}" unless me == author | |
git_padding = " " | |
description = commit[4..-1].map { |line| | |
error line unless line.start_with? git_padding | |
line[git_padding.size..-1] | |
}.grep(/^(?:\* | )/).slice_before(/^\*/).map { |entry| | |
entry.map { |line| "\t#{line}" }.join("\n") | |
}.join("\n\n") | |
entry = <<-EOS | |
#{time} #{author} <#{email}> | |
#{description} | |
EOS | |
# Prevent recursion | |
previous_entry = open("ChangeLog") { |file| | |
file.each_line.take(entry.lines.count) | |
}.join | |
if previous_entry == entry | |
exit 0 | |
end | |
puts "Writing ChangeLog" | |
contents = entry + `git show HEAD^1:ChangeLog` | |
File.write("ChangeLog", contents) | |
system 'git add ChangeLog' | |
system 'git commit -q --amend -C HEAD' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment