summaryrefslogtreecommitdiff
path: root/changelog-consolidator.pl
blob: 03c71d535a1e295029dac0d356206bccc0d93825 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#! /usr/bin/perl -w

#
# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#


use strict;
use warnings;
use diagnostics;
use CGI;
use Getopt::Long;

# Currently available output formats:
#  short - short log (default)
#  long - full logs
#  html - short log with cgit links
#  stat - diff stats
#
# Input is provided as a list of modules with a from and to version, or
# "-" if no version is included, such as:
#
#                                MODULE   7.5       7.6
#                                ------  -----     ------
#                           applewmproto 1.4.1     1.4.1
#                               bdftopcf 1.0.2     1.0.3
# [...]


my $output_type = 'short';

my $result = GetOptions ("type=s" => \$output_type);

my $usage = "Usage: [--type=short|long|html|stat] <filename>\n";

if ((!$result) ||
    (($output_type ne 'short') && ($output_type ne 'long') &&
     ($output_type ne 'html')&& ($output_type ne 'stat') )) {
  print STDERR $usage;
  exit 1;
}

my @modtypes=qw(app data doc driver font lib proto util xcb .);

my %modmap = (
	      'libXres' => 'libXRes',
	      'libpthread-stubs' => 'pthread-stubs',
	      'util-macros' => 'macros',
	      'xbitmaps' => 'bitmaps',
	      'xcb-proto' => 'proto',
	      'xcursor-themes' => 'cursors',
	      'xorg-server' => 'xserver',
	      'xproto' => 'x11proto',
	      'xtrans' => 'libxtrans',
	     );

my %module_info = ();

while ($_= <>) {
  chomp($_);
  if ($_ =~ m/(\S+)\s+([\-\.\d]+)\s+([\.\d\-\*]+)/) {
    my ($module, $old, $new) = ($1, $2, $3);
    my $modtype = "?";
    my $moddir = $module;

    next if $new eq '-';

    if (exists($modmap{$module})) {
      $moddir = $modmap{$module};
    }

    if ($module =~ m/font-(.*)/) {
      $moddir = $1;
      $modtype = 'font';
    } else {
      foreach my $m (@modtypes) {
	if (-d "$m/$moddir") {
	  $modtype = $m;
	  last;
	}
      }
    }

    if (($modtype ne '?') && (($old ne $new) || ($output_type eq 'html'))) {
      if ($output_type ne 'html') {
	print "======= $modtype/$module ($old..$new)\n\n";
      }
      my $oldtag = find_tag($modtype, $moddir, $module, $old);
      my $newtag = find_tag($modtype, $moddir, $module, $new);
#      print "$modtype/$module: $oldtag -> $newtag\n";
#      system('git', "--git-dir=$modtype/$moddir/.git", 'log',
#	     '--pretty=short', "$oldtag..$newtag");

      # special cases for X11R7.7
      if ($module eq 'xkeyboard-config') {
	  $oldtag = 'xkeyboard-config-2.0';
      }

      my $tagrange = ($oldtag ne '') ? "$oldtag..$newtag" : "$newtag";

      my $output_flags = ($output_type eq 'long') ? '' : '--pretty=short';

      my $git_subcmd = ($output_type eq 'stat') ? 'diff --shortstat' : 'log';

      my $git_log_cmd = "git --git-dir=$modtype/$moddir/.git" .
	  " $git_subcmd $output_flags $tagrange";

      if ($output_type eq 'short') {
	system("$git_log_cmd | git shortlog");
      } elsif (($output_type eq 'long') || ($output_type eq 'stat')) {
	system("$git_log_cmd");
      } else {
	my %changes = ();
	open my $gsl, '-|', $git_log_cmd or die;
	while (my $ll = <$gsl>) {
	  chomp($ll);
	  my $commit = {};
	  my $author;

	  if ($ll =~ m{^commit (\w+)$}) {
	    $commit->{id} = $1;

	    while ($ll = <$gsl>) {
	      chomp($ll);
	      last if $ll =~ m{^\s*$};
	      if ($ll =~ m{^Author:\s*(.*)\s+\<.*\>}) {
		$author = $1;
	      } elsif ($ll !~ m{^Merge:}) {
		die "Author match failed: $modtype/$module/$commit->{id}\n$ll\n";
	      }
	    }

	    my $desc = "";
	    while ($ll = <$gsl>) {
	      last if $ll =~ m{^\s*$};
	      $ll =~ s{^    }{};
	      $desc .= $ll;
	    }
	    chomp($desc);
	    $commit->{desc} = $desc;

	    if (!exists $changes{$author}) {
	      $changes{$author} = [ ];
	    }
	    unshift @{$changes{$author}}, $commit;
	  }
	}
	close $gsl;
	my $newtagdate = `git --git-dir=$modtype/$moddir/.git log -1 --tags --simplify-by-decoration --pretty="format:%ad" --date=short $newtag`;
	$module_info{"$modtype/$module"} =
	  {
	   changes => \%changes,
	   oldtag => $oldtag,
	   newtag => $newtag,
	   newtagdate => $newtagdate,
	   modtype => $modtype,
	   moddir => $moddir,
	   module => $module,
	   oldvers => $old,
	   newvers => $new
	  };
      }
    }
  } else {
#    print $_, "\n";
  }
}

if ($output_type eq 'html') {
  my $q = new CGI;

  my $title = 'Consolidated ChangeLog for X11R7.7';
  print $q->start_html(-title => $title,
		       -style=>{-src =>'http://cgit.freedesktop.org/cgit.css',
				-code => '.modules { float: left; }' .
				    '.modules > td { padding-left: 3px; }'
			       },
		       -encoding => 'utf-8',
		       -head => [
			  $q->Link({-rel => 'home',
				    -href => 'http://www.x.org/'}),
			  $q->Link({-rel => 'SHORTCUT ICON',
				    -href => 'favicon.ico'}),
			  $q->Link({-rel => 'up',
				    -href => 'index.html'}),
		       ],
		       -itemscope => undef,
		       -itemtype => 'http://schema.org/WebPage'
      ), "\n";
  print
      $q->div({ -style => 'text-align: center;',
		-itemprop => 'publisher', -content => 'X.Org Foundation' },
	      $q->a({ -href => 'http://www.x.org/', -rel => 'home'},
		    $q->img({ -src => 'logo.png', -border => '0',
			      -alt => 'X.Org Foundation' }))), "\n",
      $q->h1($title), "\n";

  print $q->start_table({ -class => 'modules', -cols => '4' }), "\n",
    $q->Tr($q->th({-colspan=>"2"}, 'Module'),
	   $q->th([' X11R7.6 ', ' X11R7.7 '])), "\n";

  my $midpoint = scalar(keys %module_info) / 2;

  foreach my $m (sort keys %module_info) {
    my $modname = $module_info{$m}->{module};
    my $moddisplay = $m;
    print $q->Tr($q->td( [
			  $module_info{$m}->{modtype},
			  $q->a({href=>"#$modname"},
				$module_info{$m}->{moddir}),
			  $module_info{$m}->{oldvers},
			  $module_info{$m}->{newvers}
			 ])), "\n";
    if (--$midpoint == 0) {
      print $q->end_table();
      print $q->start_table({ -class => 'modules', -cols => '4' }).
	$q->Tr($q->th({-colspan=>"2"}, 'Module'),
	       $q->th([' X11R7.6 ', ' X11R7.7 '])), "\n";
    }
  }
  print $q->end_table();
  print $q->br({-clear => "all"}), "\n";

  foreach my $m (sort keys %module_info) {
    my $modname = $module_info{$m}->{module};
    my $modtype = $module_info{$m}->{modtype};
    my $moddir = $module_info{$m}->{moddir};
    my $modvers = $module_info{$m}->{newvers};
    my $modtar = "$modname-$modvers.tar.bz2";
    my $moddisplay = $m;
    $moddisplay =~ s{^\./}{};
    print
	$q->start_div({-class => "content", -itemscope => undef,
		       -itemtype=>'http://schema.org/SoftwareApplication'}),
	$q->h2($q->span({-itemprop => "name"},
	       cgit_link($q, $modtype, $moddir, 'top', $modname, $moddisplay)),
	       $q->span({ -itemprop => 'version' }, $modvers)),
	"\n";
    print
	$q->a({ -href => "src/everything/$modtar",
		-itemprop => 'downloadURL' }, $modtar),
	' &mdash; ', $q->span({ -itemprop => 'datePublished' },
			      $module_info{$m}->{newtagdate}), "\n";
    print $q->h3('Commits from',
		 ($module_info{$m}->{oldtag} eq '') ?
		 'the beginning' :
		 cgit_link($q, $modtype, $moddir, 'tag',
			   $module_info{$m}->{oldtag},
			   $module_info{$m}->{oldtag}
			  ),
		 'to',
		 cgit_link($q, $modtype, $moddir, 'tag',
			   $module_info{$m}->{newtag},
			   $module_info{$m}->{newtag}
			  )
		), "\n";

    print $q->start_ul({ -class => 'authors', -itemprop => 'versionChanges' });
    my $changes = $module_info{$m}->{changes};
    foreach my $a (sort keys %{$changes}) {
      my @au_changes = @{$changes->{$a}};
      my $count = scalar @au_changes;
      print $q->li("$a ($count):",
		   $q->ul({ -class => 'commits' },
			  $q->li([map {
		     cgit_link($q, $modtype, $moddir, 'commit',
			       $_->{id}, $_->{desc}) } @au_changes]))
		  ), "\n";
    }
    print $q->end_ul();
    print $q->end_div(), "\n";
  }
  print $q->end_html(), "\n";
}

sub cgit_link {
  my ($q, $modtype, $moddir, $type, $id, $body) = @_;
  # http://cgit.freedesktop.org/xorg/xserver/tag/?id=xorg-server-1.7.1
  # http://cgit.freedesktop.org/xorg/xserver/commit/?id=9a2f6135bfb0f12ec28f304c97917d2f7c64db05
  if ($modtype ne 'xcb') {
    if ($modtype eq '.') {
      $modtype = "xorg";
      # special cases for X11R7.6
#      if ($id eq '9edb9e9b4dde6f73dc5241d078425a7a70699ec9') {
#	$type = 'commit';
#      }
    } else {
      $modtype = "xorg/$modtype";
    }
  }
  my $modpath = "$modtype/$moddir";
  if ($moddir eq "xkeyboard-config") {
      $modpath = "xkeyboard-config";
  }

  my %link_attrs = (-href => "http://cgit.freedesktop.org/$modpath/");
  if ($type eq 'top') {
    $link_attrs{-name} = $id;
  } else {
    $link_attrs{-href} .= "$type/?id=$id";
  }
  my $link = $q->a(\%link_attrs, $q->escapeHTML($body));
  return $link;
}

sub find_tag {
  my ($modtype, $moddir, $module, $vers) = @_;

  if ($vers eq '*') {
    return 'HEAD';
  }

  if ($vers eq '-') {
    return '';
  }

  my $oldvers = $vers;
  $oldvers =~ s/\./_/g;

  if (-f "$modtype/$moddir/.git/refs/tags/$module-$vers") {
    return "$module-$vers";
  } elsif (-f "$modtype/$moddir/.git/refs/tags/$vers") {
    return "$vers";
  } else {
    if (-f "$modtype/$moddir/.git/refs/tags/$module-$oldvers") {
      return "$module-$oldvers";
    }
  }

  $vers =~ s/\./\\./g;

  open(my $tag_fh, '-|', "git --git-dir=$modtype/$moddir/.git tag -l")
    or die "Failed to run git --git-dir=$modtype/$moddir/.git tag -l";

  my $found;
  while (my $t = <$tag_fh>) {
    chomp($t);
    if (($t =~ m/$vers$/) || ($t =~ m/$oldvers$/)) {
      $found = $t;
    }
  }
  close($tag_fh);
  if ($found) {
      return $found;
  }

  if (-f "$modtype/$moddir/.git/refs/tags/XORG-7_1") {
    return 'XORG-7_1';
  }

  return '';
}

#    if [[ -d "$TOP/$d/.git" && ! -f "$TOP/$d/NO-PULL" ]] ; then
#	cd $TOP/$d
#	LAST_TAG="$(git describe --abbrev=0)"
#	if [[ -z "${LAST_TAG}" ]] ; then
#	    LAST_TAG="$(git describe --abbrev=0 --all HEAD^)"
#	fi
#	git log "${LAST_TAG}"..