4 # This program is Copyright 2005 by Rocco Caputo. All rights are
5 # reserved. This program is free software. It may be modified, used,
6 # and redistributed under the same terms as Perl itself.
8 # Generate a nice looking change log from the subversion logs for a
9 # Perl project. The log is also easy for machines to parse.
15 use Text::Wrap qw(wrap fill $columns $huge);
16 use POSIX qw(strftime);
20 Jan 01 Feb 02 Mar 03 Apr 04 May 05 Jun 06
21 Jul 07 Aug 08 Sep 09 Oct 10 Nov 11 Dec 12
24 $Text::Wrap::huge = "wrap";
25 $Text::Wrap::columns = 74;
27 my $days_back = 365; # Go back a year by default.
28 my $send_help = 0; # Display help and exit.
29 my $svn_repo; # Where to log from.
31 use constant LOG_REV => 0;
32 use constant LOG_DATE => 1;
33 use constant LOG_WHO => 2;
34 use constant LOG_MESSAGE => 3;
35 use constant LOG_PATHS => 4;
37 use constant PATH_PATH => 0;
38 use constant PATH_ACTION => 1;
39 use constant PATH_CPF_PATH => 2;
40 use constant PATH_CPF_REV => 3;
42 use constant TAG_REV => 0;
43 use constant TAG_TAG => 1;
44 use constant TAG_LOG => 2;
46 use constant MAX_TIMESTAMP => "9999-99-99 99:99:99";
49 "age=s" => \$days_back,
50 "repo=s" => \$svn_repo,
51 "help" => \$send_help,
54 # Find the trunk for the current repository if one isn't specified.
55 unless (defined $svn_repo) {
56 $svn_repo = `svn info . | grep '^URL: '`;
57 if (length $svn_repo) {
59 $svn_repo =~ s{^URL\:\s+(.+?)/trunk/?.*$}{$1};
68 " --repo REPOSITORY\n",
71 "REPOSITORY must have a trunk subdirectory and a tags directory where\n",
72 "release tags are kept.\n",
75 my $earliest_date = strftime "%F", gmtime(time() - $days_back * 86400);
77 ### 1. Gather a list of tags for the repository, their revisions and
82 open(TAG, "svn -v list $svn_repo/tags|") or die $!;
84 # The date is unused, however.
86 my ($rev, $date, $tag) = m{
87 (\d+).*?(\S\S\S\s+\d\d\s+(?:\d\d\d\d|\d\d:\d\d))\s+(v[0-9_.]+)
91 my @tag_log = gather_log("$svn_repo/tags/$tag", "--stop-on-copy");
92 die "Tag $tag has changes after tagging!\n" if @tag_log > 1;
94 my $timestamp = $tag_log[0][LOG_DATE];
103 # Fictitious "HEAD" tag for revisions that came after the last tag.
105 $tag{+MAX_TIMESTAMP} = [
107 "(untagged)", # TAG_TAG
111 ### 2. Gather the log for the trunk. Place log entries under their
114 my @tag_dates = sort keys %tag;
115 while (my $date = pop(@tag_dates)) {
117 # We're done if this date's before our earliest date.
118 if ($date lt $earliest_date) {
123 my $tag = $tag{$date}[TAG_TAG];
124 #warn "Gathering information for tag $tag...\n";
126 my $this_rev = $tag{$date}[TAG_REV];
129 $prev_rev = $tag{$tag_dates[-1]}[TAG_REV];
135 my @log = gather_log("$svn_repo/trunk", "-r", "$this_rev:$prev_rev");
137 $tag{$date}[TAG_LOG] = \@log;
140 ### 3. PROFIT! No, wait... generate the nice log file.
142 foreach my $timestamp (sort { $b cmp $a } keys %tag) {
143 my $tag_rec = $tag{$timestamp};
145 # Skip this tag if there are no log entries.
146 next unless @{$tag_rec->[TAG_LOG]};
148 my $tag_line = "$timestamp $tag_rec->[TAG_TAG]";
149 my $tag_bar = "=" x length($tag_line);
150 print $tag_bar, "\n", $tag_line, "\n", $tag_bar, "\n\n";
152 foreach my $log_rec (@{$tag_rec->[TAG_LOG]}) {
154 my @paths = @{$log_rec->[LOG_PATHS]};
157 $_->[PATH_PATH] ne "/trunk" or $_->[PATH_ACTION] ne "M"
161 my $time_line = wrap(
165 "$log_rec->[LOG_DATE] (r$log_rec->[LOG_REV]) by $log_rec->[LOG_WHO]",
166 map { "$_->[PATH_PATH] $_->[PATH_ACTION]" } @paths
170 if ($time_line =~ /\n/) {
173 "$log_rec->[LOG_DATE] (r$log_rec->[LOG_REV]) by $log_rec->[LOG_WHO]\n"
179 map { "$_->[PATH_PATH] $_->[PATH_ACTION]" } @paths
184 print $time_line, "\n\n";
186 # Blank lines should have the indent level of whitespace. This
187 # makes it easier for other utilities to parse them.
189 my @paragraphs = split /\n\s*\n/, $log_rec->[LOG_MESSAGE];
190 foreach my $paragraph (@paragraphs) {
192 # Trim off identical leading space from every line.
193 my ($whitespace) = $paragraph =~ /^(\s*)/;
194 if (length $whitespace) {
195 $paragraph =~ s/^$whitespace//mg;
198 # Re-flow the paragraph if it isn't indented from the norm.
199 # This should preserve indented quoted text, wiki-style.
200 unless ($paragraph =~ /^\s/) {
201 $paragraph = fill(" ", " ", $paragraph);
205 print join("\n \n", @paragraphs), "\n\n";
215 ### Z. Helper functions.
218 my ($url, @flags) = @_;
222 my $parser = XML::Parser->new(
225 my ($self, $tag, %att) = @_;
226 push @stack, [ $tag, \%att ];
227 if ($tag eq "logentry") {
229 $log[-1][LOG_WHO] = "(nobody)";
233 my ($self, $text) = @_;
234 $stack[-1][1]{0} .= $text;
237 my ($self, $tag) = @_;
238 die "close $tag w/out open" unless @stack;
239 my ($pop_tag, $att) = @{pop @stack};
241 die "$tag ne $pop_tag" if $tag ne $pop_tag;
243 if ($tag eq "date") {
244 my $timestamp = $att->{0};
245 my ($date, $time) = split /[T.]/, $timestamp;
246 $log[-1][LOG_DATE] = "$date $time";
250 if ($tag eq "logentry") {
251 $log[-1][LOG_REV] = $att->{revision};
256 $log[-1][LOG_MESSAGE] = $att->{0};
260 if ($tag eq "author") {
261 $log[-1][LOG_WHO] = $att->{0};
265 if ($tag eq "path") {
266 my $path = $att->{0};
267 $path =~ s{^/trunk/}{};
269 @{$log[-1][LOG_PATHS]}, [
271 $att->{action}, # PATH_ACTION
275 $log[-1][LOG_PATHS][-1][PATH_CPF_PATH] = $att->{"copyfrom-path"} if (
276 exists $att->{"copyfrom-path"}
279 $log[-1][LOG_PATHS][-1][PATH_CPF_REV] = $att->{"copyfrom-rev"} if (
280 exists $att->{"copyfrom-rev"}
289 my $cmd = "svn -v --xml @flags log $url";
290 #warn "Command: $cmd\n";
292 open(LOG, "$cmd|") or die $!;
293 $parser->parse(*LOG);