2 # Copyright: 2008-2011, Nick Treleaven
3 # License: GNU GPL V2 or later, as published by the Free Software Foundation, USA.
6 # Searches a ChangeLog file for a line matching 'matchstring', then matches
7 # all lines until two consecutive empty lines are found. The process then
8 # repeats until all matching blocks of text are found.
9 # Results are printed in reverse, hence in chronological order (as ChangeLogs
10 # are usually written in reverse date order).
12 # The resulting lines are then formatted to be easier to read and edit into a
15 # Example ChangeLog format:
16 #2009-04-03 Joe Author <joe@example.net>
18 # * src/file.c, src/file.h,
20 # Some change description,
21 # spanning several lines.
22 # * foo.c: Combined line.
27 my $argc = $#ARGV + 1;
28 my ($matchstr, $infile);
37 ($matchstr, $infile) = @ARGV;
42 $0 changelogfile >outfile
43 $0 matchstring changelogfile >outfile
45 matchstring is a case-insensitive regular expression, e.g. 'joe|fred'.
50 or die "Couldn't open $infile for reading: $!\n";
52 my $entry; # the current matching block of text
53 my @entries; # changelog entries, one per date
55 # first parse each ChangeLog entry into an array
57 my $found = 0; # if we're in a matching block of text
58 my $blank = 0; # whether the last line was empty
61 my $line = $_; # read a line, including \n char
64 ($line =~ m/$matchstr/) and $found = 1;
66 if (length($line) <= 1) { # current line is empty
67 if ($blank > 0) { # previous line was also empty
68 push(@entries, $entry); # append entry
70 $found = 0; # now look for next match
85 foreach $entry (reverse @entries) {
86 my @lines = split(/\n/, $entry);
87 my $fl = 0; # in file list lines
88 my $cm = 0; # in commit message lines
90 foreach my $line (@lines){
93 # strip trailing space
97 # check if in filelist
98 ($line =~ m/ \* /) and $fl = 1;
99 # join filelist together on one line
100 $fl and ($line =~ s/^ / /);
101 if ($fl and ($line =~ m/:/)){
103 # separate ' * foo.c: Some edit.' messages:
104 if (!($line =~ m/:$/)) {
105 ($line =~ s/:/:\n*/);
108 $fl and ($line =~ m/,$/) or $fl = 0;
111 # Asterisk commit messages
112 if (!$cm and ($line =~ m/^ /)){
114 $line =~ s/^( )/$1* /;
116 $cm and ($line =~ s/^( )/$1 /); # indent continuing lines
118 $cm and ($line =~ m/\.$/) and $cm = 0;
120 #~ print $fl.','.$cm.','.$line."\n"; next; # debug
122 # change file list start char to easily distinguish between file list and commit messages
123 $line =~ s/^ \* /@ /g;
124 # strip <email> from date line
125 $line =~ s/^([0-9-]+.*?)\s+<.+>$/$1/g;
131 (!$fl) and print "\n";