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