Comments (minor fixes of some header conventions).
[emacs.git] / lib-src / grep-changelog
blob3c44306ce44a760a158addc09735aedf7007ef8e
1 #! /usr/bin/perl
3 # Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 # 2005, 2006, 2007, 2008, 2009 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 3 of the License, or
11 # (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
22 # Extract entries from ChangeLogs matching specified criteria.
23 # Optionally format the resulting output to a form suitable for RCS
24 # logs, like they are used in Emacs, for example. In this format,
25 # author lines, leading spaces, and file names are removed.
27 require 5;
28 use strict;
30 # Parse command line options.
32 use vars qw($author $regexp $exclude $from_date $to_date
33 $rcs_log $with_date $version $help $reverse
34 @entries);
36 use Getopt::Long;
38 my $result;
40 if (@ARGV == 0) {
42 # No arguments cannot possibly mean "show everything"!!
43 $result = 0;
45 } else {
47 $result = GetOptions ("author=s" => \$author,
48 "text=s" => \$regexp,
49 "exclude=s" => \$exclude,
50 "from-date=s" => \$from_date,
51 "to-date=s" => \$to_date,
52 "rcs-log" => \$rcs_log,
53 "with-date" => \$with_date,
54 "reverse!" => \$reverse,
55 "version" => \$version,
56 "help" => \$help);
58 # If date options are specified, check that they have the format
59 # YYYY-MM-DD.
61 $result = 0 if $from_date && $from_date !~ /^\d\d\d\d-\d\d-\d\d$/;
62 $result = 0 if $to_date && $to_date !~ /^\d\d\d\d-\d\d-\d\d$/;
65 # Print usage information and exit when necessary.
67 if ($result == 0 || $help) {
68 print <<USAGE;
70 Usage: $0 [options] [CHANGELOG...]
72 Print entries in ChangeLogs matching various criteria.
73 Valid options are:
75 --author=AUTHOR Match entries whose author line matches
76 regular expression AUTHOR
77 --text=TEXT Match entries whose text matches regular
78 expression TEXT
79 --exclude=TEXT Exclude entries matching TEXT
80 --from-date=YYYY-MM-DD Match entries not older than given date
81 --to-date=YYYY-MM-DD Match entries not younger than given date
82 --rcs-log Format output suitable for RCS log entries
83 --with-date Print short date line in RCS log
84 --reverse Show entries in reverse (chronological) order
85 --version Print version info
86 --help Print this help
88 If no CHANGELOG is specified scan the files "ChangeLog" and
89 "ChangeLog.N+" in the current directory. Old-style dates in ChangeLogs
90 are not recognized.
91 USAGE
92 exit !$help;
95 # Print version info and exit if `--version' was specified.
97 if ($version) {
98 print "0.3\n";
99 exit 0;
103 # Value is non-zero if HEADER matches according to command line
104 # options specified, i.e. it matches $author, and its date is in
105 # the range $from_date <= date <= $to_date.
107 sub header_match_p {
108 my $header = shift;
110 return 0 unless $header;
112 # No match if AUTHOR-regexp specified and doesn't match.
113 return 0 if $author && $header !~ /$author/;
115 # Check that the date of the entry matches if date options
116 # `--from-date' and/or `--to-date' were specified . Old-style
117 # dates in ChangeLogs are not recognized, and never match.
118 if ($from_date || $to_date) {
119 if ($header =~ /^(\d\d\d\d-\d\d-\d\d)/) {
120 my $date = $1;
121 return 0 if $from_date && $date lt $from_date;
122 return 0 if $to_date && $date gt $to_date;
123 } else {
124 # Don't bother recognizing old-style dates.
125 return 0;
129 return 1;
133 # Value is non-zero if ENTRY matches the criteria specified on the
134 # command line, i.e. it matches $regexp, and it doesn't match
135 # $exclude.
137 sub entry_match_p {
138 my $entry = shift;
140 return 0 unless $entry;
142 if ($regexp) {
143 return 1 if ($entry =~ /$regexp/
144 && (!$exclude || $entry !~ $exclude));
145 } else {
146 return 1 if !$exclude || $entry !~ $exclude;
149 return 0;
153 # Print HEADER and/or ENTRY in a format suitable for what was
154 # specified on the command line. If $rcs_log is specified, author
155 # lines are not printed, and leading spaces and file names are removed
156 # from ChangeLog entries.
158 sub print_log {
159 my ($header, $entry) = @_;
160 my $output = '';
162 if ($rcs_log) {
163 # Remove leading whitespace from entry.
164 $entry =~ s/^\s+//mg;
165 # Remove file name parts.
166 $entry =~ s/^\*.*\(/(/mg;
167 # Remove file name parts, 2.
168 $entry =~ s/^\*.*://mg;
169 if ($with_date) {
170 $header =~ /(\d\d\d\d-\d\d-\d\d)/;
171 $output = "!changelog-date $1\n";
173 $output .= $entry;
174 } else {
175 $output .= $header . $entry;
178 if ($reverse) {
179 push @entries, $output;
180 } else {
181 print $output;
185 # Scan LOG for matching entries, and print them to standard output.
187 sub parse_changelog {
188 my $log = shift;
189 my $entry = undef;
190 my $header = undef;
192 @entries = () if $reverse;
194 # Open the ChangeLog.
195 open (IN, "< $log") || die "Cannot open $log: $!";
197 while (defined(my $line = <IN>)) {
198 if ($line =~ /^\S/) {
199 # Line is an author-line. Print previous entry if
200 # it matches.
201 print_log ($header, $entry)
202 if header_match_p ($header) && entry_match_p ($entry);
204 $entry = "";
205 $header = $line;
207 # Add empty lines below the header.
208 while (defined($line = <IN>) && $line =~ /^\s*$/) {
209 $header = "$header$line";
213 last unless defined $line;
215 if ($line =~ /^\s*\*/) {
216 # LINE is the first line of a ChangeLog entry. Print
217 # previous entry if it matches.
218 print_log ($header, $entry)
219 if header_match_p ($header) && entry_match_p ($entry);
220 $entry = $line;
221 } else {
222 # Add LINE to the current entry.
223 $entry = "$entry$line";
227 # Print last entry if it matches.
228 print_log ($header, $entry)
229 if header_match_p ($header) && entry_match_p ($entry);
231 close IN;
233 if ($reverse) {
234 for (my $entry = @entries; $entry; $entry--) {
235 print $entries[$entry-1];
241 # Main program. Process ChangeLogs.
243 # If files were specified on the command line, parse those files in the
244 # order supplied by the user; otherwise parse default files ChangeLog and
245 # ChangeLog.NNN according to $reverse.
246 unless (@ARGV > 0) {
247 @ARGV = ("ChangeLog");
249 push @ARGV,
250 map {"ChangeLog.$_"}
251 sort {$b <=> $a}
252 map {/\.(\d+)$/; $1}
253 do {
254 opendir D, '.';
255 grep /^ChangeLog\.\d+$/, readdir D;
258 @ARGV = reverse @ARGV if $reverse;
261 while (defined (my $log = shift @ARGV)) {
262 parse_changelog ($log) if -f $log;
266 # arch-tag: 9e4f6749-e053-4bb7-b3ad-11947318418e
267 # grep-changelog ends here.