Initial revision
[bpt/emacs.git] / lib-src / grep-changelog
1 #! /usr/local/bin/perl
2 # $Id: grep-changelog,v 1.19 1999/08/10 13:22:23 gerd Exp $
3
4 # Copyright (C) 1999 Free Software Foundation, Inc.
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
29 require 5;
30
31 # Parse command line options.
32
33 use Getopt::Long;
34 $result = GetOptions ("author=s" => \$author,
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
52 if ($result == 0 || $help) {
53 print <<USAGE;
54 Usage: $0 [options] [CHANGELOG...]
55 Print entries in ChangeLogs matching various criteria. Valid options
56 are
57
58 --author=AUTHOR match entries whose author line matches
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
70 If no CHANGELOG is specified scan the files "ChangeLog" and
71 "ChangeLog.[9-1]" in the current directory. Old-style dates in ChangeLogs
72 are not recognized.
73 USAGE
74 exit $help ? 0 : 1;
75 }
76
77 # Print version info and exit if `--version' was specified.
78
79 if ($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
89 sub header_match_p ($) {
90 my $header = shift;
91
92 # No match if AUTHOR-regexp specified and doesn't match.
93 return 0 if $author && $header !~ /$author/;
94
95 # Check that the date of the entry matches if date options
96 # `--from-date' and/or `--to-date' were specified . Old-style
97 # dates in ChangeLogs are not recognized, and never match.
98 if ($from_date || $to_date) {
99 if ($header =~ /^(\d\d\d\d-\d\d-\d\d)/) {
100 my $date = $1;
101 return 0 if $from_date && $date lt $from_date;
102 return 0 if $to_date && $date gt $to_date;
103 } else {
104 # Don't bother recognizing old-style dates.
105 return 0;
106 }
107 }
108
109 return 1;
110 }
111
112
113 # Value is non-zero if ENTRY matches the ciiteria specified on the
114 # command line, i.e. it matches $regexp, and it doesn't match
115 # $exclude.
116
117 sub entry_match_p ($) {
118 my $entry = shift;
119
120 if ($regexp) {
121 return 1 if ($entry =~ /$regexp/
122 && (!$exclude || $entry !~ $exclude));
123 } else {
124 return 1 if !$exclude || $entry !~ $exclude;
125 }
126
127 return 0;
128 }
129
130
131 # Print HEADER and/or ENTRY in a format suitable for what was
132 # specified on the command line. If $rcs_log is specified, author
133 # lines are not printed, and leading spaces and file names are removed
134 # from ChangeLog entries.
135
136 sub print_log ($$) {
137 my ($header, $entry) = @_;
138
139 if ($rcs_log) {
140 # Remove leading whitespace from entry.
141 $entry =~ s/^\s+//mg;
142 # Remove file name parts.
143 $entry =~ s/^\*.*\(/(/mg;
144 # Remove file name parts, 2.
145 $entry =~ s/^\*.*://mg;
146 if ($with_date) {
147 $header =~ /(\d\d\d\d-\d\d-\d\d)/;
148 print "!changelog-date $1\n";
149 }
150 print $entry;
151 } else {
152 print $header, $entry;
153 }
154 }
155
156 # Scan LOG for matching entries, and print them to standard output.
157
158 sub parse_changelog ($) {
159 my $log = shift;
160 my $entry;
161 my $match;
162
163 # Open the ChangeLog.
164 open (IN, "< $log") || die "Cannot open $log: $!";
165
166 while ($line = <IN>) {
167 if ($line =~ /^\S/) {
168 # Line is an author-line. Print previous entry if
169 # it matches.
170 print_log ($header, $entry)
171 if header_match_p ($header) && entry_match_p ($entry);
172
173 $entry = "";
174 $header = $line;
175
176 # Add empty lines below the header.
177 while (($line = <IN>) && $line =~ /^\s*$/) {
178 $header = "$header$line";
179 }
180 }
181
182 if ($line =~ /^\s*\*/) {
183 # LINE is the first line of a ChangeLog entry. Print
184 # previous entry if it matches.
185 print_log ($header, $entry)
186 if header_match_p ($header) && entry_match_p ($entry);
187 $entry = $line;
188 } else {
189 # Add LINE to the current entry.
190 $entry = "$entry$line";
191 }
192 }
193
194 # Print last entry if it matches.
195 print_log ($header, $entry)
196 if header_match_p ($header) && entry_match_p ($entry);
197
198 close IN;
199 }
200
201
202 # Main program. Process ChangeLogs.
203
204 if (@ARGV > 0) {
205 # If files were specified on the command line, parse those files.
206 while ($log = shift @ARGV) {
207 parse_changelog ($log);
208 }
209 } else {
210 # Parse default files ChangeLog and ChangeLog.9...ChangeLog.1 in
211 # that order.
212 parse_changelog ("ChangeLog");
213 for ($i = 9; $i >= 1; --$i) {
214 my $log = "ChangeLog.$i";
215 parse_changelog ($log) if -f $log;
216 }
217 }
218
219
220 # gre-changelog ends here.