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