3 # Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
5 # This file is part of GNU Emacs.
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)
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.
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
19 # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 # Boston, MA 02111-1307, USA.
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,
26 # author lines, leading spaces, and file names are removed.
31 # Parse command line options.
33 use vars
qw($author $regexp $exclude $from_date $to_date
34 $rcs_log $with_date $version $help $reverse
38 my $result = GetOptions ("author=s" => \$author,
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,
45 "reverse!" => \$reverse,
46 "version" => \$version,
49 # If date options are specified, check that they have the format
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$/;
55 # Print usage information and exit when necessary.
57 if ($result == 0 || $help) {
59 Usage: $0 [options] [CHANGELOG...]
60 Print entries in ChangeLogs matching various criteria. Valid options
63 --author=AUTHOR match entries whose author line matches
64 regular expression AUTHOR
65 --text=TEXT match entries whose text matches regular
67 --exclude=TEXT exclude entries matching TEXT.
68 --from-date=YYYY-MM-DD match entries not older than given date
69 --to-date=YYYY-MM-DD match entries not younger than given date
70 --rcs-log format output suitable for RCS log entries.
71 --with-date print short date line in RCS log
72 --reverse show entries in reverse (chronological) order
73 --version print version info
74 --help print this help
76 If no CHANGELOG is specified scan the files "ChangeLog" and
77 "ChangeLog.[9-1]" in the current directory. Old-style dates in ChangeLogs
83 # Print version info and exit if `--version' was specified.
91 # Value is non-zero if HEADER matches according to command line
92 # options specified, i.e. it matches $author, and its date is in
93 # the range $from_date <= date <= $to_date.
95 sub header_match_p
($) {
98 return 0 unless $header;
100 # No match if AUTHOR-regexp specified and doesn't match.
101 return 0 if $author && $header !~ /$author/;
103 # Check that the date of the entry matches if date options
104 # `--from-date' and/or `--to-date' were specified . Old-style
105 # dates in ChangeLogs are not recognized, and never match.
106 if ($from_date || $to_date) {
107 if ($header =~ /^(\d\d\d\d-\d\d-\d\d)/) {
109 return 0 if $from_date && $date lt $from_date;
110 return 0 if $to_date && $date gt $to_date;
112 # Don't bother recognizing old-style dates.
121 # Value is non-zero if ENTRY matches the criteria specified on the
122 # command line, i.e. it matches $regexp, and it doesn't match
125 sub entry_match_p
($) {
128 return 0 unless $entry;
131 return 1 if ($entry =~ /$regexp/
132 && (!$exclude || $entry !~ $exclude));
134 return 1 if !$exclude || $entry !~ $exclude;
141 # Print HEADER and/or ENTRY in a format suitable for what was
142 # specified on the command line. If $rcs_log is specified, author
143 # lines are not printed, and leading spaces and file names are removed
144 # from ChangeLog entries.
147 my ($header, $entry) = @_;
151 # Remove leading whitespace from entry.
152 $entry =~ s/^\s+//mg;
153 # Remove file name parts.
154 $entry =~ s/^\*.*\(/(/mg;
155 # Remove file name parts, 2.
156 $entry =~ s/^\*.*://mg;
158 $header =~ /(\d\d\d\d-\d\d-\d\d)/;
159 $output = "!changelog-date $1\n";
163 $output .= $header . $entry;
167 push @entries, $output;
173 # Scan LOG for matching entries, and print them to standard output.
175 sub parse_changelog
($) {
180 @entries = () if $reverse;
182 # Open the ChangeLog.
183 open (IN
, "< $log") || die "Cannot open $log: $!";
185 while (defined(my $line = <IN
>)) {
186 if ($line =~ /^\S/) {
187 # Line is an author-line. Print previous entry if
189 print_log
($header, $entry)
190 if header_match_p
($header) && entry_match_p
($entry);
195 # Add empty lines below the header.
196 while (defined($line = <IN
>) && $line =~ /^\s*$/) {
197 $header = "$header$line";
201 last unless defined $line;
203 if ($line =~ /^\s*\*/) {
204 # LINE is the first line of a ChangeLog entry. Print
205 # previous entry if it matches.
206 print_log
($header, $entry)
207 if header_match_p
($header) && entry_match_p
($entry);
210 # Add LINE to the current entry.
211 $entry = "$entry$line";
215 # Print last entry if it matches.
216 print_log
($header, $entry)
217 if header_match_p
($header) && entry_match_p
($entry);
222 while (defined (my $entry = pop @entries)) {
229 # Main program. Process ChangeLogs.
231 # If files were specified on the command line, parse those files in the
232 # order supplied by the user; otherwise parse default files ChangeLog and
233 # ChangeLog.9...ChangeLog.1 according to $reverse.
235 @ARGV = ("ChangeLog", map {"ChangeLog.$_"} reverse 1..9);
236 @ARGV = reverse @ARGV if $reverse;
239 while (defined (my $log = shift @ARGV)) {
240 parse_changelog
($log) if -f
$log;
244 # grep-changelog ends here.