summaryrefslogtreecommitdiff
path: root/tasks/changelog.rake
blob: 707b2ad4732ea11bc0597d0952afd7f5b34fc8c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
require 'time'

desc 'Generate the CHANGELOG file'
task :changelog do

  class Entry

    attr_reader :type

    def initialize(ref, author, time, message)
      @ref = ref
      @author = author
      @time = time
      @message = message
      if (m = /New: (.*)/i.match(@message))
        @type = :feature
        @message = m[1]
      elsif (m = /Fix: (.*)/i.match(@message))
        @type = :bugfix
        @message = m[1]
      else
        @type = :other
      end
    end

    def to_s
      "  * #{@message}\n"
    end

  end

  class Release

    attr_reader :date, :version, :tag

    def initialize(tag, predecessor)
      @tag = tag
      # We only support release tags in the form X.X.X
      @version = /\d+\.\d+\.\d+/.match(tag)

      # Construct a Git range.
      interval = predecessor ? "#{predecessor.tag}..#{@tag}" : @tag

      # Get the date of the release
      git_show_tag = `git show #{tag}`.encode('UTF-8', invalid: :replace,
                                              undef: :replace)
      date = Time.parse(/Date: (.*)/.match(git_show_tag)[1]).utc
      @date = date.strftime("%Y-%m-%d")

      @entries = []
      # Use -z option for git-log to get 0 bytes as separators.
      `git log -z #{interval}`.split("\0").each do |commit|
        # We ignore merges.
        next if commit =~ /^Merge: \d*/

        ref, author, time, _, message = commit.split("\n", 5)
        ref = ref[/commit ([0-9a-f]+)/, 1]
        author = author[/Author: (.*)/, 1].strip
        time = Time.parse(time[/Date: (.*)/, 1]).utc
        # Eleminate git-svn-id: lines
        message.gsub!(/git-svn-id: .*\n/, '')
        # Eliminate Signed-off-by: lines
        message.gsub!(/Signed-off-by: .*\n/, '')
        message.strip!
        @entries << Entry.new(ref, author, time, message)
      end
    end

    def empty?
      @entries.empty?
    end

    def to_s
      s = ''
      if hasFeatures? || hasFixes?
        if hasFeatures?
          s << "== New Features\n\n"
          @entries.each do |entry|
            s << entry.to_s if entry.type == :feature
          end
          s << "\n"
        end
        if hasFixes?
          s << "== Bug Fixes\n\n"
          @entries.each do |entry|
            s << entry.to_s if entry.type == :bugfix
          end
          s << "\n"
        end
      else
        @entries.each do |entry|
          s << entry.to_s
        end
      end
      s
    end

    private

    def hasFeatures?
      @entries.each do |entry|
        return true if entry.type == :feature
      end
      false
    end

    def hasFixes?
      @entries.each do |entry|
        return true if entry.type == :bugfix
      end
      false
    end

  end

  class ChangeLog

    def initialize
      @releases = []
      predecessor = nil
      getReleaseVersions.each do |version|
        @releases << (predecessor = Release.new(version, predecessor))
      end
    end

    def to_s
      s = ''
      @releases.reverse.each do |release|
        next if release.empty?

        # We use RDOC markup syntax to generate a title
        if release.version
          s << "= Release #{release.version} (#{release.date})\n\n"
        else
          s << "= Next Release (Some Day)\n\n"
        end
        s << release.to_s + "\n"
      end
      s
    end

    private

    # 'git tag' is not sorted numerically. This function implements a
    # numerical comparison for tag versions of the format 'release-X.X.X'. X
    # can be a multi-digit number.
    def compareTags(a, b)

      def versionToComparable(v)
        /\d+\.\d+\.\d+/.match(v)[0].split('.').map{ |l| sprintf("%03d", l.to_i)}.
                                                             join('.')
      end

      versionToComparable(a) <=> versionToComparable(b)
    end

    def getReleaseVersions
      # Get list of release tags from Git repository
      releaseVersions = `git tag`.split("\n").map { |r| r.chomp }.
        delete_if { |r| ! (/v\d+\.\d+\.\d+/ =~ r) }.
        sort{ |a, b| compareTags(a, b) }
      releaseVersions << 'HEAD'
    end

  end

  File.open('CHANGELOG', 'w+') do |changelog|
    changelog.puts ChangeLog.new.to_s
  end

end