summaryrefslogtreecommitdiffabout
path: root/mu-aux/gencl
blob: 715b6bd0e0f2df0e79fd801b7922a354c771546a (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
eval '(exit $?0)' && eval 'exec perl -wS "$0" "$@"'
  & eval 'exec perl -wS "$0" $argv:q'
    if 0;

# This file is part of GNU Mailutils.
# Copyright (C) 2017-2019 Free Software Foundation, Inc.
#
# GNU Mailutils is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3, or (at
# your option) any later version.
#
# GNU Mailutils is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use POSIX qw(strftime);
use Getopt::Long qw(:config gnu_getopt no_ignore_case);
use Text::Wrap;
use Data::Dumper;
use threads;
use Thread::Queue;
use Safe;
use Pod::Usage;
use Pod::Man;
use Pod::Find qw(pod_where);

my @append_files;
my $force;
my $verbose;
my $changelog_file = 'ChangeLog';
my $since_date;
my $until_date;
my $strip_cherry_pick;
my $amend_file;
my %amendment;
my $append_dot;
my $ignore_errors;
my $email;

my $exit_status = 0;

=head1 NAME

gencl - generate ChangeLog from git log output

=head1 SYNOPSIS

B<gencl>
[B<-fiv>]    
[B<-a> I<FILE>]
[B<-m> I<EMAIL>]    
[B<-F> I<FILE>]
[B<--amend=>I<FILE>]
[B<--append-dot>]
[B<--append=>I<FILE>]
[B<--email=>I<EMAIL>]    
[B<--file=>I<FILE>]
[B<--force>]
[B<--ignore-errors>]    
[B<--since=>I<DATE>]
[B<--strip-cherry-pick>]
[B<--until=>I<DATE>]    
[B<--verbose>]

B<gencl> B<-h> | B<--help> | B<--usage>

=head1 DESCRIPTION

Retrieves git log messages and reformats them as a valid ChangeLog
file.  The file begins with an automatically generated entry stating
the SHA1 hash of the git HEAD commit.  This entry is followed by
the log entries recreated from the git log, in reverse chronological
order.  By default, entire log is converted.  This can be changed by
using B<--since> and/or B<--until> options.  Files specified with the
B<--append> options (if any), are appended after the converted entries.
The file ends with the B<emacs> B<Local Variables> stanza.

If the B<ChangeLog> file exists, B<gencl> verifies if the source tree
has changed since the file was created.  The file is re-created only if
there were some changes (whether committed or not).  The the B<--force>
(B<-f>) option instructs B<gencl> to recreate the file unconditionally.

The file supplied with the B<--amend> option is used to correct spelling
(and other) errors in the log entries.  It consists of entries delimited
with one or more empty lines.  Each entry begins with a full SHA1 hash
of the commit it applies to.  The hash is followed by one or more lines
with a valid Perl code (typically, B<s///> statements).  Comments are
introduced with the B<#> sign.  For each git log entry, its hash is looked
up in that file.  If found, the B<$_> variable is set to the commit subject,
followed by the commit body and the code is evaluated.
        
=head1 OPTIONS

=over 4

=item B<-a>, B<--append=>I<FILE>

Append I<FILE> to the end of the generated file.  Multiple B<--append>
are processed in the order of their occurrence on the command line.
The content of I<FILE> is appended verbatim, except that the line beginning
with the text B<Local Variables:> is taken to mark the end of file.

=item B<-m>, B<--email=>I<EMAIL>

Sets email address to use in the topmost automatically generated entry.
In the absense of this option, B<gencl> constructs the email from the
current user name and the domain (or host) name.    
    
=item B<-F>, B<--file=>I<FILE>

Create I<FILE> instead of the B<ChangeLog>.

=item B<-f>, B<--force>

Force recreating the ChangeLog, even if no new commits were added to the
repository since its creation.

=item B<-i>, B<--ignore-errors>

Ignore non-fatal errors.  With this option in effect, B<gencl> exits with
code 0 even if some errors were encountered while running.    
    
=item B<-v>, B<--verbose>

Increase output verbosity.

=item B<--since=>I<DATE>

Convert only the logs since I<DATE> (B<YYYY-MM-DD>).

=item B<--until=>I<DATE>

Convert only the logs until I<DATE> (B<YYYY-MM-DD>).
    
=item B<--strip-cherry-pick>

Remove data inserted by B<git cherry-pick>.  This includes the "cherry picked
from commit ..." line, and the possible final "Conflicts:" paragraph.

=item B<--amend=>I<FILE>

Read amendment instructions from I<FILE>.

=item B<--append-dot>

Append a dot to the subject line of each commit message if there is no other
punctuation the end.     
    
=back

=head1 EXIT CODES

=over 4

=item B<0>

Success.

=item B<1>

Fatal error.

=item B<2>

Non-fatal error.  The B<--ignore-errors> (B<-i>) option instructs B<gencl>
to exit with code B<0>, instead of B<2>.

=back    
    
=head1 DIFFERENCES FROM GITLOG-TO-CHANGELOG

=over 4

=item 1

B<gencl> writes output to the disk file, whereas B<gitlog-to-changelog>
prints it to the standard output.

=item 2

The created B<ChangeLog> begins with an automatically generated entry and
ends with the B<Local Variables> stanza.

=item 3

The B<ChangeLog> file is re-created only if the source tree was changed
since it was written (whether these changes have been committed or not).

=item 4

Arbitrary number of files can be concatenated to the produced file.  This
is handy for projects that switched to B<git> from other VCS.

=item 5

Each entry is reformatted using B<Text::Wrap>.

=item 6

The following B<gitlab-to-changelog> options are not implemented: B<--cluster>,
B<--ignore-matching>, B<--ignore-line>.
    
=back
    
=cut    
    
sub pod_usage_msg {
    my ($obj) = @_;
    open my $fd, '>', \my $msg;

    pod2usage(-verbose => 99,
              -sections => 'NAME',
              -output => $fd,
              -exitval => 'NOEXIT');
    my @a = split /\n/, $msg;
    $msg = $a[1];
    $msg =~ s/^\s+//;
    $msg =~ s/ - /: /;
    return $msg;
}

GetOptions(
    'h' => sub {
	pod2usage(-message => pod_usage_msg(),
		  -exitstatus => 0,
		  -input => pod_where({-inc => 1}, $0))
    },
    'help' => sub {
	pod2usage(-exitstatus => 0,
		  -verbose => 2,
		  -input => pod_where({-inc => 1}, $0));
    },
    'usage' => sub {
	pod2usage(-exitstatus => 0,
                  -verbose => 0,
                  -input => pod_where({-inc => 1}, $0));
    },
    'append|a=s@' => \@append_files,
    'file|F=s' => \$changelog_file,
    'force|f' => \$force,
    'verbose|v' => \$verbose,
    'since=s' => \$since_date,
    'until=s' => \$until_date,
    'strip-cherry-pick' => \$strip_cherry_pick,
    'amend=s' => \$amend_file,
    'append-dot' => \$append_dot,
    'ignore-errors|i' => \$ignore_errors,
    'email|m=s' => \$email
    ) or exit(1);

if (! -d '.git') {
    exit 0;
}

unless (defined($email)) {
    if (exists($ENV{EMAIL})) {
	$email = $ENV{EMAIL};
    } else {
	my ($user, $domain);
	if (exists($ENV{USER})) {
	    $user = $ENV{USER};
	} elsif (my ($name) = getpwuid(getuid())) {
	    $user = $name;
	} else {
	    die "can't get user name";
	}

	if (chomp($domain = `domainname`) && $domain ne "(none)") {
	    ;
	} elsif (exists($ENV{HOSTNAME})) {
	    $domain = $ENV{HOSTNAME};
	} elsif ($domain = `hostname`) {
	    chomp $domain;
	} else {
	    $domain = 'localhost';
	}
	$email = $user . '@' . $domain;
    }
}

read_amend_file($amend_file) if $amend_file;
    
$Text::Wrap::columns = 72;
# Work over bug #79766 in Text::Wrap
$Text::Wrap::huge = 'overflow' if $Text::Wrap::VERSION eq '2012.0818';
    
create_changelog();
$exit_status = 0 if $ignore_errors;
exit $exit_status;

sub toplevel_entry {
    my ($hash, $date) = split / /,
          `git log --max-count=1 --pretty=format:'%H %ad' --date=short HEAD`;

    my @modlines;
    if (open(my $fd, '-|', 'git diff-index --name-status HEAD 2>/dev/null')) {
	chomp(@modlines = map {chomp; [split /\s+/, $_, 2]} <$fd>);
	close $fd;
    }

    if (@modlines) {
	$date = strftime '%Y-%m-%d', localtime;
    }

    my @header;

    push @header, "$date  Automatically generated  <$email>";
    push @header, '';
    push @header, "\tHEAD $hash.";
    push @header, '';

    my %status = (
	A => 'New file',
	C => 'Copied file',
	D => 'Removed file',
	M => 'Changed',
	R => 'Renamed',
	T => 'Type change',
	U => 'Unmerged',
	X => 'Unknown'
    );

    if (@modlines) {
	push @header, "\tUncommitted changes:";
	push @header, '';
	
	push @header, map {
	    "\t* $_->[1]: " . ($status{$_->[0]} || 'Unknown') . ";"
	} @modlines;
	push @header, '';
    }
    return @header;
}

sub headcmp {
    my $file = shift;
    if (open(my $fd, '<', $changelog_file)) {
	# Skip first line
	$_ = <$fd>;
	shift;
	while (<$fd>) {
	    my $line = shift;
	    last unless defined($line);
	    chomp;
	    return 0 unless $line eq $_;
	}
	return 1 unless @_;
    }
    return 0;
}

sub read_amend_file {
    my ($file) = @_;
    open(my $fd, '<', $file)
	or die "can't open $file for reading: $!";
    use constant {
	STATE_INIT => 1,
	STATE_HASH => 2,
    };
    my $state = STATE_INIT;
    my $silent;
    my $hash;
    my $code;
    my $locus;
    while (<$fd>) {
	chomp;
	s/^\s+//;
	next if /^#/;
	if ($state == STATE_INIT) {
	    if (/^([0-9a-fA-F]{40})$/) {
		$hash = lc $1;
		if (exists($amendment{$hash})) {
		    warn "$file:$.: duplicate SHA1 hash";
		    warn $amendment{$hash}{locus} . ": previously defined here";
		    $exit_status = 2;
		}
		$code = '';
		$locus = "$file:$.";
		$state = STATE_HASH;
		$silent = 0;
	    } elsif (/^$/) {
		next;
	    } else {
		warn "$file:$.: expected SHA1, but found $_"
		    unless $silent;
		$exit_status = 2;
		$silent = 1;
            }
	} elsif ($state == STATE_HASH) {
	    if (/^$/) {
		$amendment{$hash}{code} = $code;
		$amendment{$hash}{locus} = $locus;
		$state = STATE_INIT;
	    } else {
		$code .= "$_\n";
	    }
	}
    }
    if ($state == STATE_HASH) {
	$amendment{$hash}{code} .= $code;
	$amendment{$hash}{locus} = $locus;
    }
}	

sub tokenize_gitlog {
    my ($q) = @_;
    my @cmd = qw(git log --log-size --no-merges
                 --pretty=format:%H:%ct:%an:%ae%n%s%n%b);

    push @cmd, "--since=$since_date" if defined $since_date;
    push @cmd, "--until=$until_date" if defined $until_date;
    print STDERR "starting @cmd\n" if $verbose > 1;
    
    open(my $fd, '-|', @cmd)
	or die "failed to run git log: $!";
    while (<$fd>) {
	chomp;
	next if /^$/;
	my %ent = ();
	unless (/^log size (\d+)/) {
	    warn "unexpected input: '$_'";
	    $exit_status = 2;
	    next;
	}
	my $size = $1;
	my $log;
	read($fd, $log, $size) == $size or die "unexpected EOF";

	my ($head, $text) = split /\n/, $log, 2;
	($ent{hash},$ent{date},$ent{author},$ent{email}) = split /:/, $head;
	
	if (defined($amendment{$ent{hash}})) {
	    my $code = $amendment{$ent{hash}}{code};
	    print STDERR "amending $ent{hash}\n" if $verbose > 1;
	    print STDERR "code: $code\n" if $verbose > 1;
	    my $s = new Safe;
	    $_ = $text;
	    if (defined(my $r = $s->reval($code))) {
		$text = $_;
		delete $amendment{$ent{hash}};
	    } else {
		warn "$.:$ent{hash}: failed to eval \"$code\" on \"$_\": \n$@\n";
		warn $amendment{$ent{hash}}{locus} . ": code was defined here";
		$exit_status = 2;
	    }
	}

	my @body;
	($ent{subject}, @body) = split /\n/, $text;

	if ($append_dot && $ent{subject} !~ /[[:punct:]]$/) {
	    $ent{subject} .= '.';
	}
	
	foreach my $line (@body) {
	    if ($line =~ /^Co-authored-by:(.*)$/) {
		my $author = $1;
		if ($author =~ /\s*(.*?)<.+?>$/) {
		    push @{$ent{coauthor}}, [ $1, $2 ];
		}
	    } elsif ($line =~ /^(?:Signed-off-by
                                 |Copyright-paperwork-exempt
                                 |Tiny-change):\s*$/x) {
		next;
	    } elsif ($strip_cherry_pick
		     && $line =~ /^\s*
                                   (?:Conflicts:
                                    |\(cherry picked from commit [\da-f]+\)$)
                                 /x) {
		next;
	    } elsif ($line =~ /^\*/) {
		push @{$ent{body}}, $line;
	    } elsif ($line =~ /^(?:\s
                                 |(?:\(.+?\)\s*
                                   |\[.+?\]\s*
                                   |<.+?>\s*)+:)/x) {
		push @{$ent{body}}, $line;
	    } elsif (exists($ent{body})) {
		${$ent{body}}[-1] .= "\n" . $line;
	    } else {
		if (!exists($ent{description})
		    || ${$ent{description}}[-1] eq ''
		    || $line eq '') {
		    push @{$ent{description}}, $line;
		} else {
		    ${$ent{description}}[-1] .= "\n" . $line;
		}
	    }
	}
	if (exists($ent{body}) && ${$ent{body}}[-1] ne '') {
	    push @{$ent{body}}, '';
	}
	if (exists($ent{description}) && ${$ent{description}}[-1] ne '') {
	    push @{$ent{description}}, '';
	}
	    
	$q->enqueue(\%ent);

    }
    $q->enqueue(undef);
    close $fd;

    my @unused;
    while (my ($hash, $ref) = each %amendment) {
        my $line = $ref->{locus};
	$line =~ s/^.*://;
	push @unused, [ $line, $ref->{locus}, $hash ];
    }
    if (@unused) {
	$exit_status = 2;
	foreach my $ent (sort { $a->[0] <=> $b->[0] } @unused) {
	    warn "$ent->[1]: unused entry: $ent->[2]\n";
	}
    }
    print STDERR "tokenize_gitlog finished\n" if $verbose > 1;
}

sub convert_entry {
    my ($q) = @_;
    while (my $ent = $q->dequeue()) {
	print STDERR "Writing $ent->{hash}\n" if $verbose > 1;
	my $date = strftime('%Y-%m-%d', localtime($ent->{date}));
	print $date, '  ', $ent->{author}, '  <', $ent->{email}, ">\n";
	if (exists($ent->{coauthor})) {
	    foreach my $coauthor (@{$ent->{coauthor}}) {
		print '            ', $coauthor->[0], '  ', $coauthor->[1], "\n";
	    }
	}
	print "\n";

	my $tabs = "\t";
	print wrap($tabs, $tabs, $ent->{subject}), "\n\n";
	if (exists($ent->{description})) {
	    foreach my $para (@{$ent->{description}}) {
		print fill($tabs, $tabs, $para), "\n";
	    }
	}
	if (exists($ent->{body})) {
	    foreach my $para (@{$ent->{body}}) {
		print fill($tabs, $tabs, $para), "\n";
	    }
	}
    }
    print STDERR "convert_entry finished\n" if $verbose > 1;
}

sub create_changelog {
    my @header = toplevel_entry;
    if (!$force && headcmp($changelog_file, @header)) {
	print STDERR "$changelog_file is up to date\n" if $verbose > 1;
	return;
    }
    open(my $fd, '>', $changelog_file)
	or die "can't open $changelog_file for writing: $!";
    
    print "  GEN $changelog_file\n" if $verbose;
    print STDERR "updating $changelog_file\n" if $verbose > 1;
    $fd = select($fd);
    # Print header
    for (@header) {
	print "$_\n";
    }

    # Print converted entries
    my $q = Thread::Queue->new();
    my $tok_thr = threads->create(\&tokenize_gitlog, $q);
    my $cvt_thr = threads->create(\&convert_entry, $q);
    $tok_thr->join();
    $cvt_thr->join();
    if ($tok_thr->error() || $cvt_thr->error()) {
	exit 1;
    }
    
    # Print additional files
    foreach my $file (@append_files) {
	if (open(my $in, '<', $file)) {
	    while (<$in>) {
		chomp;
		last if /^Local Variables:/;
		next if /^\f$/;
		print "$_\n";
	    }
	    close $in;
	} else {
	    warn "can't open $file: $!";
	    $exit_status = 2;
	}
    }

    # Print trailer
    print "\f\nLocal Variables:\n";
    print <<'EOT';
mode: change-log
version-control: never
buffer-read-only: t
End:
EOT
;
    $fd = select($fd);
    close $fd;
}   

# Local Variables:
# mode: perl
# End:

Return to:

Send suggestions and report system problems to the System administrator.