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