File indexing completed on 2024-05-19 15:38:53

0001 #!/usr/bin/env ruby
0002 # This script attempts to make sense of my commits into a decent CHANGELOG.
0003 # SPDX-FileCopyrightText: 2020 L. E. Segovia <amy@amyspark.me>
0004 # SPDX-License-Identifier: BSD-3-Clause
0005 
0006 # frozen_string_literal: true
0007 
0008 require 'date'
0009 require 'optparse'
0010 require 'ostruct'
0011 require 'pathname'
0012 
0013 options = OpenStruct.new
0014 
0015 opts = OptionParser.new do |o|
0016   o.on_tail('-h', '--help', 'Show this usage statement') do |_h|
0017     options.help = true
0018   end
0019 
0020   o.on_tail('-f', '--from TAG', 'Start counting from this Git tag...') do |f|
0021     options.from = f
0022   end
0023 
0024   o.on_tail('-t', '--to TAG', '...to this tag inclusive') do |t|
0025     options.to = t
0026   end
0027 end
0028 
0029 begin
0030   opts.parse!(ARGV)
0031   system "git rev-parse #{options.from} > /dev/null", exception: true
0032   system "git rev-parse #{options.to} > /dev/null", exception: true
0033 rescue StandardError => e
0034   warn e, '', opts
0035   exit 1
0036 end
0037 
0038 if options.help
0039   puts opts
0040   exit
0041 end
0042 
0043 log = `git log --oneline --reverse #{options.from}..#{options.to}`
0044 
0045 date = `git log -1 --format=%aI #{options.to} 2> /dev/null` unless options.to.match? 'HEAD'
0046 
0047 log2 = log
0048        .lines(chomp: true)
0049        .map { |a| a.split(' ', 2) }
0050        .map { |a| "#{a[1]} (#{a[0]})" }
0051 
0052 log2 = log2.group_by do |a|
0053   keyword = a.split(' ')[0].downcase
0054   case keyword
0055   when 'enable', 'reimplement', 'standardize', 'implement', 'add', 'use'
0056     'Added'
0057   when 'automagically', 'rebrand', 'complete', 'automatically', 'hide'
0058     'Added'
0059   when 'extract', 'expose', 'improve', 'install', 'factor', 'integrate'
0060     'Added'
0061   when 'restore', 'properly', 'sort', 'detect', 'only', 'fix', 'correct'
0062     'Fixed'
0063   when 'work', 'do', 'ward', 'propagate', 'ensure', 'forcibly'
0064     'Fixed'
0065   when 'differentiate', 'more'
0066     'Fixed'
0067   when 'drop', 'assorted', 'cleanup', 'general', 'clean', 'remove'
0068     'Removed'
0069   else
0070     'Changed'
0071   end
0072 end
0073 
0074 log3 = []
0075 
0076 # log3 << "# Changelog"
0077 # log3 << "\n"
0078 # log3 << "All notable changes to this project will be documented in this file."
0079 # log3 << "The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),"
0080 # log3 << "and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)."
0081 # log3 << "\n"
0082 
0083 date_string = if date
0084                 "(#{DateTime.iso8601(date).strftime('%B %d, %Y')})"
0085               else
0086                 ''
0087               end
0088 
0089 version_string = if options.to.match? 'HEAD'
0090                    'Unreleased'
0091                  else
0092                    options.to
0093                  end
0094 
0095 log3 << "## #{version_string} #{date_string}"
0096 log3 << "\n"
0097 
0098 %w[Added Fixed Removed Changed].each do |k|
0099   log3 << "### #{k}"
0100   log3 << "\n"
0101 
0102   next unless log2[k]
0103 
0104   log2[k].each do |c|
0105     log3 << "- #{c}"
0106   end
0107 
0108   log3 << "\n"
0109 end
0110 
0111 tarballs = Dir.glob "kseexpr-#{options.to[1..-1]}.tar.gz"
0112 
0113 if tarballs
0114   log3 << '### Release hashes'
0115   log3 << "\n"
0116 
0117   tarballs.each do |i|
0118     log3 << "md5sum: #{`md5sum #{i}`}"
0119     log3 << "sha256: #{`sha256sum #{i}`}"
0120   end
0121 end
0122 
0123 puts log3