grep-changelog 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #! /usr/bin/perl
  2. # Copyright (C) 1999-2012 Free Software Foundation, Inc.
  3. #
  4. # This file is part of GNU Emacs.
  5. # GNU Emacs is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. # GNU Emacs is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. # Extract entries from ChangeLogs matching specified criteria.
  16. # Optionally format the resulting output to a form suitable for RCS
  17. # logs, like they are used in Emacs, for example. In this format,
  18. # author lines, leading spaces, and file names are removed.
  19. require 5;
  20. use strict;
  21. # Parse command line options.
  22. use vars qw($author $regexp $exclude $from_date $to_date
  23. $rcs_log $with_date $version $help $reverse
  24. @entries);
  25. use Getopt::Long;
  26. my $result;
  27. if (@ARGV == 0) {
  28. # No arguments cannot possibly mean "show everything"!!
  29. $result = 0;
  30. } else {
  31. $result = GetOptions ("author=s" => \$author,
  32. "text=s" => \$regexp,
  33. "exclude=s" => \$exclude,
  34. "from-date=s" => \$from_date,
  35. "to-date=s" => \$to_date,
  36. "rcs-log" => \$rcs_log,
  37. "with-date" => \$with_date,
  38. "reverse!" => \$reverse,
  39. "version" => \$version,
  40. "help" => \$help);
  41. # If date options are specified, check that they have the format
  42. # YYYY-MM-DD.
  43. $result = 0 if $from_date && $from_date !~ /^\d\d\d\d-\d\d-\d\d$/;
  44. $result = 0 if $to_date && $to_date !~ /^\d\d\d\d-\d\d-\d\d$/;
  45. }
  46. # Print usage information and exit when necessary.
  47. if ($result == 0 || $help) {
  48. print <<USAGE;
  49. Usage: $0 [options] [CHANGELOG...]
  50. Print entries in ChangeLogs matching various criteria.
  51. Valid options are:
  52. --author=AUTHOR Match entries whose author line matches
  53. regular expression AUTHOR
  54. --text=TEXT Match entries whose text matches regular
  55. expression TEXT
  56. --exclude=TEXT Exclude entries matching TEXT
  57. --from-date=YYYY-MM-DD Match entries not older than given date
  58. --to-date=YYYY-MM-DD Match entries not younger than given date
  59. --rcs-log Format output suitable for RCS log entries
  60. --with-date Print short date line in RCS log
  61. --reverse Show entries in reverse (chronological) order
  62. --version Print version info
  63. --help Print this help
  64. If no CHANGELOG is specified scan the files "ChangeLog" and
  65. "ChangeLog.N+" in the current directory. Old-style dates in ChangeLogs
  66. are not recognized.
  67. USAGE
  68. exit !$help;
  69. }
  70. # Print version info and exit if `--version' was specified.
  71. if ($version) {
  72. print "0.3\n";
  73. exit 0;
  74. }
  75. # Value is non-zero if HEADER matches according to command line
  76. # options specified, i.e. it matches $author, and its date is in
  77. # the range $from_date <= date <= $to_date.
  78. sub header_match_p {
  79. my $header = shift;
  80. return 0 unless $header;
  81. # No match if AUTHOR-regexp specified and doesn't match.
  82. return 0 if $author && $header !~ /$author/;
  83. # Check that the date of the entry matches if date options
  84. # `--from-date' and/or `--to-date' were specified . Old-style
  85. # dates in ChangeLogs are not recognized, and never match.
  86. if ($from_date || $to_date) {
  87. if ($header =~ /^(\d\d\d\d-\d\d-\d\d)/) {
  88. my $date = $1;
  89. return 0 if $from_date && $date lt $from_date;
  90. return 0 if $to_date && $date gt $to_date;
  91. } else {
  92. # Don't bother recognizing old-style dates.
  93. return 0;
  94. }
  95. }
  96. return 1;
  97. }
  98. # Value is non-zero if ENTRY matches the criteria specified on the
  99. # command line, i.e. it matches $regexp, and it doesn't match
  100. # $exclude.
  101. sub entry_match_p {
  102. my $entry = shift;
  103. return 0 unless $entry;
  104. if ($regexp) {
  105. return 1 if ($entry =~ /$regexp/
  106. && (!$exclude || $entry !~ $exclude));
  107. } else {
  108. return 1 if !$exclude || $entry !~ $exclude;
  109. }
  110. return 0;
  111. }
  112. # Print HEADER and/or ENTRY in a format suitable for what was
  113. # specified on the command line. If $rcs_log is specified, author
  114. # lines are not printed, and leading spaces and file names are removed
  115. # from ChangeLog entries.
  116. sub print_log {
  117. my ($header, $entry) = @_;
  118. my $output = '';
  119. if ($rcs_log) {
  120. # Remove leading whitespace from entry.
  121. $entry =~ s/^\s+//mg;
  122. # Remove file name parts.
  123. $entry =~ s/^\*.*\(/(/mg;
  124. # Remove file name parts, 2.
  125. $entry =~ s/^\*.*://mg;
  126. if ($with_date) {
  127. $header =~ /(\d\d\d\d-\d\d-\d\d)/;
  128. $output = "!changelog-date $1\n";
  129. }
  130. $output .= $entry;
  131. } else {
  132. $output .= $header . $entry;
  133. }
  134. if ($reverse) {
  135. push @entries, $output;
  136. } else {
  137. print $output;
  138. }
  139. }
  140. # Scan LOG for matching entries, and print them to standard output.
  141. sub parse_changelog {
  142. my $log = shift;
  143. my $entry = undef;
  144. my $header = undef;
  145. @entries = () if $reverse;
  146. # Open the ChangeLog.
  147. open (IN, "< $log") || die "Cannot open $log: $!";
  148. while (defined(my $line = <IN>)) {
  149. if ($line =~ /^\S/) {
  150. # Line is an author-line. Print previous entry if
  151. # it matches.
  152. print_log ($header, $entry)
  153. if header_match_p ($header) && entry_match_p ($entry);
  154. $entry = "";
  155. $header = $line;
  156. # Add empty lines below the header.
  157. while (defined($line = <IN>) && $line =~ /^\s*$/) {
  158. $header = "$header$line";
  159. }
  160. }
  161. last unless defined $line;
  162. if ($line =~ /^\s*\*/) {
  163. # LINE is the first line of a ChangeLog entry. Print
  164. # previous entry if it matches.
  165. print_log ($header, $entry)
  166. if header_match_p ($header) && entry_match_p ($entry);
  167. $entry = $line;
  168. } else {
  169. # Add LINE to the current entry.
  170. $entry = "$entry$line";
  171. }
  172. }
  173. # Print last entry if it matches.
  174. print_log ($header, $entry)
  175. if header_match_p ($header) && entry_match_p ($entry);
  176. close IN;
  177. if ($reverse) {
  178. for (my $entry = @entries; $entry; $entry--) {
  179. print $entries[$entry-1];
  180. }
  181. }
  182. }
  183. # Main program. Process ChangeLogs.
  184. # If files were specified on the command line, parse those files in the
  185. # order supplied by the user; otherwise parse default files ChangeLog and
  186. # ChangeLog.NNN according to $reverse.
  187. unless (@ARGV > 0) {
  188. @ARGV = ("ChangeLog");
  189. push @ARGV,
  190. map {"ChangeLog.$_"}
  191. sort {$b <=> $a}
  192. map {/\.(\d+)$/; $1}
  193. do {
  194. opendir D, '.';
  195. grep /^ChangeLog\.\d+$/, readdir D;
  196. };
  197. @ARGV = reverse @ARGV if $reverse;
  198. }
  199. while (defined (my $log = shift @ARGV)) {
  200. parse_changelog ($log) if -f $log;
  201. }
  202. # grep-changelog ends here.