Last Updated: February 25, 2016
·
1.636K
· jdennes

Generate a HISTORY.md file from git tags and commits

This is a quick Ruby script I recently used to generate release notes for several libraries based on tags and commits in their respective git repositories. See the gist for full details, but the script is below.

# Generates a HISTORY.md file for your repo based on tags and commits
# Requires: gem install grit
# Usage: ruby history.rb /your/repo/directory

require 'grit'

if ARGV.size < 1
  p "Usage: ruby history.rb /your/repo/directory"
  exit
end

output_file = 'HISTORY.md'
repo_dir = ARGV[0]
output = "# #{File.basename(repo_dir)} history\n\n"

repo = Grit::Repo.new(repo_dir)
tags = repo.tags
tags.sort! {|x,y| y.commit.authored_date <=> x.commit.authored_date}

tagcount = 0
tags.each do |tag|
  output << "## #{tag.name} - #{tag.commit.authored_date.strftime("%-d %b, %Y")}\
   (#{tag.commit.sha[0,8]})\n\n"
  if (tagcount != tags.size - 1)
    repo.commits_between(tags[tagcount + 1].name, tag.name).each do |c|
      output << "* #{c.message}\n"
    end
  else
    output << "* Initial release.\n"
  end
  output << "\n"
  tagcount += 1
end

File.open(output_file, 'w') { |f| f.write(output) }