2 # $Id: grep-changelog,v 1.5 2001/07/20 10:02:06 gerd Exp $
4 # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
6 # This file is part of GNU Emacs.
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)
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.
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.
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.
31 # Parse command line options.
34 $result = GetOptions
("author=s" => \
$author,
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,
44 # If date options are specified, check that they have the format
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$/;
50 # Print usage information and exit when necessary.
52 if ($result == 0 || $help) {
54 Usage: $0 [options] [CHANGELOG...]
55 Print entries in ChangeLogs matching various criteria. Valid options
58 --author=AUTHOR match entries whose author line matches
59 regular expression AUTHOR
60 --text=TEXT match entries whose text matches regular
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
70 If no CHANGELOG is specified scan the files "ChangeLog" and
71 "ChangeLog.[9-1]" in the current directory. Old-style dates in ChangeLogs
77 # Print version info and exit if `--version' was specified.
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.
89 sub header_match_p
($) {
92 return 0 unless $header;
94 # No match if AUTHOR-regexp specified and doesn't match.
95 return 0 if $author && $header !~ /$author/;
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)/) {
103 return 0 if $from_date && $date lt $from_date;
104 return 0 if $to_date && $date gt $to_date;
106 # Don't bother recognizing old-style dates.
115 # Value is non-zero if ENTRY matches the criteria specified on the
116 # command line, i.e. it matches $regexp, and it doesn't match
119 sub entry_match_p
($) {
122 return 0 unless $entry;
125 return 1 if ($entry =~ /$regexp/
126 && (!$exclude || $entry !~ $exclude));
128 return 1 if !$exclude || $entry !~ $exclude;
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.
141 my ($header, $entry) = @_;
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;
151 $header =~ /(\d\d\d\d-\d\d-\d\d)/;
152 print "!changelog-date $1\n";
156 print $header, $entry;
160 # Scan LOG for matching entries, and print them to standard output.
162 sub parse_changelog
($) {
167 # Open the ChangeLog.
168 open (IN
, "< $log") || die "Cannot open $log: $!";
170 while ($line = <IN
>) {
171 if ($line =~ /^\S/) {
172 # Line is an author-line. Print previous entry if
174 print_log
($header, $entry)
175 if header_match_p
($header) && entry_match_p
($entry);
180 # Add empty lines below the header.
181 while (defined($line = <IN
>) && $line =~ /^\s*$/) {
182 $header = "$header$line";
186 last unless defined $line;
188 if ($line =~ /^\s*\*/) {
189 # LINE is the first line of a ChangeLog entry. Print
190 # previous entry if it matches.
191 print_log
($header, $entry)
192 if header_match_p
($header) && entry_match_p
($entry);
195 # Add LINE to the current entry.
196 $entry = "$entry$line";
200 # Print last entry if it matches.
201 print_log
($header, $entry)
202 if header_match_p
($header) && entry_match_p
($entry);
208 # Main program. Process ChangeLogs.
211 # If files were specified on the command line, parse those files.
212 while ($log = shift @ARGV) {
213 parse_changelog
($log);
216 # Parse default files ChangeLog and ChangeLog.9...ChangeLog.1 in
218 parse_changelog
("ChangeLog");
219 for ($i = 9; $i >= 1; --$i) {
220 my $log = "ChangeLog.$i";
221 parse_changelog
($log) if -f
$log;
226 # grep-changelog ends here.