(parse_changelog): Add another test for defined value to
[bpt/emacs.git] / lib-src / grep-changelog
CommitLineData
056565f7 1#! /usr/bin/perl
3eb7ddf3 2# $Id: grep-changelog,v 1.5 2001/07/20 10:02:06 gerd Exp $
6dde7e38 3
41848daa 4# Copyright (C) 1999, 2000, 2001 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
20# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21# Boston, MA 02111-1307, USA.
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,
27# author lines leading spaces, and file names are removed.
28
29require 5;
30
31# Parse command line options.
32
33use Getopt::Long;
2ca8a587 34$result = GetOptions ("author=s" => \$author,
6dde7e38
GM
35 "text=s" => \$regexp,
36 "exclude=s" => \$exclude,
37 "from-date=s" => \$from_date,
38 "to-date=s" => \$to_date,
39 "rcs-log" => \$rcs_log,
40 "with-date" => \$with_date,
41 "version" => \$version,
42 "help" => \$help);
43
44# If date options are specified, check that they have the format
45# YYYY-MM-DD.
46
47$result = 0 if $from_date && $from_date !~ /^\d\d\d\d-\d\d-\d\d$/;
48$result = 0 if $to_date && $to_date !~ /^\d\d\d\d-\d\d-\d\d$/;
49
50# Print usage information and exit when necessary.
51
52if ($result == 0 || $help) {
53 print <<USAGE;
54Usage: $0 [options] [CHANGELOG...]
55Print entries in ChangeLogs matching various criteria. Valid options
56are
57
2ca8a587 58 --author=AUTHOR match entries whose author line matches
6dde7e38
GM
59 regular expression AUTHOR
60 --text=TEXT match entries whose text matches regular
61 expression TEXT.
62 --exclude=TEXT exclude entries matching TEXT.
63 --from-date=YYYY-MM-DD match entries not older than given date
64 --to-date=YYYY-MM-DD match entries not younger than given date
65 --rcs-log format output suitable for RCS log entries.
66 --with-date print short date line in RCS log
67 --version print version info
68 --help print this help
69
70If no CHANGELOG is specified scan the files "ChangeLog" and
2ca8a587 71"ChangeLog.[9-1]" in the current directory. Old-style dates in ChangeLogs
6dde7e38
GM
72are not recognized.
73USAGE
74 exit $help ? 0 : 1;
75}
76
77# Print version info and exit if `--version' was specified.
78
79if ($version) {
80 print "0.1\n";
81 exit 0;
82}
83
84
85# Value is non-zero if HEADER matches according to command line
86# options specified, i.e. it matches $author, and its date is in
87# the range $from_date <= date <= $to_date.
88
89sub header_match_p ($) {
90 my $header = shift;
91
b6a6731a
GM
92 return 0 unless $header;
93
6dde7e38
GM
94 # No match if AUTHOR-regexp specified and doesn't match.
95 return 0 if $author && $header !~ /$author/;
96
97 # Check that the date of the entry matches if date options
98 # `--from-date' and/or `--to-date' were specified . Old-style
99 # dates in ChangeLogs are not recognized, and never match.
100 if ($from_date || $to_date) {
101 if ($header =~ /^(\d\d\d\d-\d\d-\d\d)/) {
102 my $date = $1;
103 return 0 if $from_date && $date lt $from_date;
104 return 0 if $to_date && $date gt $to_date;
105 } else {
106 # Don't bother recognizing old-style dates.
107 return 0;
108 }
109 }
110
111 return 1;
112}
113
114
2ca8a587 115# Value is non-zero if ENTRY matches the criteria specified on the
6dde7e38
GM
116# command line, i.e. it matches $regexp, and it doesn't match
117# $exclude.
118
119sub entry_match_p ($) {
120 my $entry = shift;
121
b6a6731a
GM
122 return 0 unless $entry;
123
6dde7e38 124 if ($regexp) {
2ca8a587 125 return 1 if ($entry =~ /$regexp/
6dde7e38
GM
126 && (!$exclude || $entry !~ $exclude));
127 } else {
128 return 1 if !$exclude || $entry !~ $exclude;
129 }
130
131 return 0;
132}
133
134
135# Print HEADER and/or ENTRY in a format suitable for what was
136# specified on the command line. If $rcs_log is specified, author
137# lines are not printed, and leading spaces and file names are removed
138# from ChangeLog entries.
139
140sub print_log ($$) {
141 my ($header, $entry) = @_;
142
143 if ($rcs_log) {
144 # Remove leading whitespace from entry.
145 $entry =~ s/^\s+//mg;
146 # Remove file name parts.
147 $entry =~ s/^\*.*\(/(/mg;
148 # Remove file name parts, 2.
149 $entry =~ s/^\*.*://mg;
150 if ($with_date) {
151 $header =~ /(\d\d\d\d-\d\d-\d\d)/;
152 print "!changelog-date $1\n";
153 }
154 print $entry;
155 } else {
156 print $header, $entry;
157 }
158}
159
160# Scan LOG for matching entries, and print them to standard output.
161
162sub parse_changelog ($) {
163 my $log = shift;
164 my $entry;
165 my $match;
166
167 # Open the ChangeLog.
168 open (IN, "< $log") || die "Cannot open $log: $!";
169
170 while ($line = <IN>) {
171 if ($line =~ /^\S/) {
172 # Line is an author-line. Print previous entry if
173 # it matches.
2ca8a587 174 print_log ($header, $entry)
6dde7e38
GM
175 if header_match_p ($header) && entry_match_p ($entry);
176
177 $entry = "";
178 $header = $line;
179
180 # Add empty lines below the header.
41848daa 181 while (defined($line = <IN>) && $line =~ /^\s*$/) {
6dde7e38
GM
182 $header = "$header$line";
183 }
2ca8a587 184 }
6dde7e38 185
3eb7ddf3
GM
186 last unless defined $line;
187
6dde7e38
GM
188 if ($line =~ /^\s*\*/) {
189 # LINE is the first line of a ChangeLog entry. Print
190 # previous entry if it matches.
2ca8a587 191 print_log ($header, $entry)
6dde7e38
GM
192 if header_match_p ($header) && entry_match_p ($entry);
193 $entry = $line;
194 } else {
195 # Add LINE to the current entry.
196 $entry = "$entry$line";
197 }
198 }
199
200 # Print last entry if it matches.
2ca8a587 201 print_log ($header, $entry)
6dde7e38
GM
202 if header_match_p ($header) && entry_match_p ($entry);
203
204 close IN;
205}
206
207
208# Main program. Process ChangeLogs.
209
210if (@ARGV > 0) {
211 # If files were specified on the command line, parse those files.
212 while ($log = shift @ARGV) {
213 parse_changelog ($log);
214 }
215} else {
216 # Parse default files ChangeLog and ChangeLog.9...ChangeLog.1 in
217 # that order.
218 parse_changelog ("ChangeLog");
219 for ($i = 9; $i >= 1; --$i) {
220 my $log = "ChangeLog.$i";
221 parse_changelog ($log) if -f $log;
222 }
223}
224
225
2ca8a587 226# grep-changelog ends here.