Update years in copyright notice; nfc.
[bpt/emacs.git] / lib-src / grep-changelog
CommitLineData
056565f7 1#! /usr/bin/perl
6dde7e38 2
92b47a4a
TTN
3# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4# 2005 Free Software Foundation, Inc.
6dde7e38
GM
5#
6# This file is part of GNU Emacs.
7#
8# GNU Emacs is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2, or (at your option)
11# any later version.
12#
13# GNU Emacs is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with GNU Emacs; see the file COPYING. If not, write to the
364c38d3
LK
20# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21# Boston, MA 02110-1301, USA.
6dde7e38
GM
22
23
24# Extract entries from ChangeLogs matching specified criteria.
25# Optionally format the resulting output to a form suitable for RCS
26# logs, like they are used in Emacs, for example. In this format,
3205760a 27# author lines, leading spaces, and file names are removed.
6dde7e38
GM
28
29require 5;
db3cd0ae 30use strict;
6dde7e38
GM
31
32# Parse command line options.
33
db3cd0ae 34use vars qw($author $regexp $exclude $from_date $to_date
3205760a
GM
35 $rcs_log $with_date $version $help $reverse
36 @entries);
db3cd0ae 37
6dde7e38 38use Getopt::Long;
db3cd0ae
GM
39my $result = GetOptions ("author=s" => \$author,
40 "text=s" => \$regexp,
41 "exclude=s" => \$exclude,
42 "from-date=s" => \$from_date,
43 "to-date=s" => \$to_date,
44 "rcs-log" => \$rcs_log,
45 "with-date" => \$with_date,
3205760a 46 "reverse!" => \$reverse,
db3cd0ae
GM
47 "version" => \$version,
48 "help" => \$help);
6dde7e38
GM
49
50# If date options are specified, check that they have the format
51# YYYY-MM-DD.
52
53$result = 0 if $from_date && $from_date !~ /^\d\d\d\d-\d\d-\d\d$/;
54$result = 0 if $to_date && $to_date !~ /^\d\d\d\d-\d\d-\d\d$/;
55
56# Print usage information and exit when necessary.
57
58if ($result == 0 || $help) {
59 print <<USAGE;
bdfd0369 60
6dde7e38 61Usage: $0 [options] [CHANGELOG...]
6dde7e38 62
bdfd0369
JB
63Print entries in ChangeLogs matching various criteria.
64Valid options are:
65
66 --author=AUTHOR Match entries whose author line matches
6dde7e38 67 regular expression AUTHOR
bdfd0369
JB
68 --text=TEXT Match entries whose text matches regular
69 expression TEXT
70 --exclude=TEXT Exclude entries matching TEXT
71 --from-date=YYYY-MM-DD Match entries not older than given date
72 --to-date=YYYY-MM-DD Match entries not younger than given date
73 --rcs-log Format output suitable for RCS log entries
74 --with-date Print short date line in RCS log
75 --reverse Show entries in reverse (chronological) order
76 --version Print version info
77 --help Print this help
6dde7e38
GM
78
79If no CHANGELOG is specified scan the files "ChangeLog" and
bdfd0369 80"ChangeLog.1+" in the current directory. Old-style dates in ChangeLogs
6dde7e38
GM
81are not recognized.
82USAGE
bdfd0369 83 exit !$help;
6dde7e38
GM
84}
85
86# Print version info and exit if `--version' was specified.
87
88if ($version) {
bdfd0369 89 print "0.2\n";
6dde7e38
GM
90 exit 0;
91}
92
93
94# Value is non-zero if HEADER matches according to command line
95# options specified, i.e. it matches $author, and its date is in
96# the range $from_date <= date <= $to_date.
97
bdfd0369 98sub header_match_p {
6dde7e38
GM
99 my $header = shift;
100
b6a6731a
GM
101 return 0 unless $header;
102
6dde7e38
GM
103 # No match if AUTHOR-regexp specified and doesn't match.
104 return 0 if $author && $header !~ /$author/;
105
106 # Check that the date of the entry matches if date options
107 # `--from-date' and/or `--to-date' were specified . Old-style
108 # dates in ChangeLogs are not recognized, and never match.
109 if ($from_date || $to_date) {
110 if ($header =~ /^(\d\d\d\d-\d\d-\d\d)/) {
111 my $date = $1;
112 return 0 if $from_date && $date lt $from_date;
113 return 0 if $to_date && $date gt $to_date;
114 } else {
115 # Don't bother recognizing old-style dates.
116 return 0;
117 }
118 }
119
120 return 1;
121}
122
123
2ca8a587 124# Value is non-zero if ENTRY matches the criteria specified on the
6dde7e38
GM
125# command line, i.e. it matches $regexp, and it doesn't match
126# $exclude.
127
bdfd0369 128sub entry_match_p {
6dde7e38
GM
129 my $entry = shift;
130
b6a6731a
GM
131 return 0 unless $entry;
132
6dde7e38 133 if ($regexp) {
2ca8a587 134 return 1 if ($entry =~ /$regexp/
6dde7e38
GM
135 && (!$exclude || $entry !~ $exclude));
136 } else {
137 return 1 if !$exclude || $entry !~ $exclude;
138 }
139
140 return 0;
141}
142
143
144# Print HEADER and/or ENTRY in a format suitable for what was
145# specified on the command line. If $rcs_log is specified, author
146# lines are not printed, and leading spaces and file names are removed
147# from ChangeLog entries.
148
bdfd0369 149sub print_log {
6dde7e38 150 my ($header, $entry) = @_;
3205760a 151 my $output = '';
6dde7e38
GM
152
153 if ($rcs_log) {
154 # Remove leading whitespace from entry.
155 $entry =~ s/^\s+//mg;
156 # Remove file name parts.
157 $entry =~ s/^\*.*\(/(/mg;
158 # Remove file name parts, 2.
159 $entry =~ s/^\*.*://mg;
160 if ($with_date) {
161 $header =~ /(\d\d\d\d-\d\d-\d\d)/;
3205760a 162 $output = "!changelog-date $1\n";
6dde7e38 163 }
3205760a 164 $output .= $entry;
6dde7e38 165 } else {
3205760a
GM
166 $output .= $header . $entry;
167 }
168
169 if ($reverse) {
170 push @entries, $output;
171 } else {
172 print $output;
6dde7e38
GM
173 }
174}
175
176# Scan LOG for matching entries, and print them to standard output.
177
bdfd0369 178sub parse_changelog {
6dde7e38 179 my $log = shift;
db3cd0ae
GM
180 my $entry = undef;
181 my $header = undef;
3205760a
GM
182
183 @entries = () if $reverse;
6dde7e38
GM
184
185 # Open the ChangeLog.
186 open (IN, "< $log") || die "Cannot open $log: $!";
187
db3cd0ae 188 while (defined(my $line = <IN>)) {
6dde7e38
GM
189 if ($line =~ /^\S/) {
190 # Line is an author-line. Print previous entry if
191 # it matches.
2ca8a587 192 print_log ($header, $entry)
6dde7e38
GM
193 if header_match_p ($header) && entry_match_p ($entry);
194
195 $entry = "";
196 $header = $line;
197
198 # Add empty lines below the header.
41848daa 199 while (defined($line = <IN>) && $line =~ /^\s*$/) {
6dde7e38
GM
200 $header = "$header$line";
201 }
2ca8a587 202 }
6dde7e38 203
3eb7ddf3
GM
204 last unless defined $line;
205
6dde7e38
GM
206 if ($line =~ /^\s*\*/) {
207 # LINE is the first line of a ChangeLog entry. Print
208 # previous entry if it matches.
2ca8a587 209 print_log ($header, $entry)
6dde7e38
GM
210 if header_match_p ($header) && entry_match_p ($entry);
211 $entry = $line;
212 } else {
213 # Add LINE to the current entry.
214 $entry = "$entry$line";
215 }
216 }
217
218 # Print last entry if it matches.
2ca8a587 219 print_log ($header, $entry)
6dde7e38
GM
220 if header_match_p ($header) && entry_match_p ($entry);
221
222 close IN;
3205760a
GM
223
224 if ($reverse) {
bdfd0369
JB
225 for (my $entry = @entries; $entry; $entry--) {
226 print $entries[$entry-1];
3205760a
GM
227 }
228 }
6dde7e38
GM
229}
230
231
232# Main program. Process ChangeLogs.
233
3205760a
GM
234# If files were specified on the command line, parse those files in the
235# order supplied by the user; otherwise parse default files ChangeLog and
bdfd0369 236# ChangeLog.1+ according to $reverse.
3205760a 237unless (@ARGV > 0) {
bdfd0369
JB
238 @ARGV = ("ChangeLog");
239
240 push @ARGV,
241 map {"ChangeLog.$_"}
242 sort {$b <=> $a}
243 map {/\.(\d+)$/; $1}
244 do {
245 opendir D, '.';
246 grep /^ChangeLog\.\d+$/, readdir D;
247 };
248
3205760a
GM
249 @ARGV = reverse @ARGV if $reverse;
250}
251
252while (defined (my $log = shift @ARGV)) {
253 parse_changelog ($log) if -f $log;
6dde7e38
GM
254}
255
256
ab5796a9 257# arch-tag: 9e4f6749-e053-4bb7-b3ad-11947318418e
2ca8a587 258# grep-changelog ends here.