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