|
From: Muli Ben-Y. <mu...@mu...> - 2003-01-18 09:36:08
|
On Sat, Jan 18, 2003 at 05:52:32AM +0200, Shlomi Fish wrote:
> On Sat, 11 Jan 2003, Muli Ben-Yehuda wrote:
>
> > On Sat, Jan 11, 2003 at 10:48:07AM +0200, Shlomi Fish wrote:
> >
> > > 2. Secondly, should I edit the changelog directly, or do I need to use
> > > some tool for that. Muli mentioned some Perl script he got and wrote
> > > another python script to wrap it. Do I need them?
> >
> > I use a script called make-changelog.pl, you can google for it. You
> > don't have to use it if you don't want to, just maintain the same
> > format.
> >
>
> Muli, the only Google entry for it is your Python lecture. Can you please
> send it to me. (gzip it if it's too large or put it in the CVS somewhere
> like under /utils)
Attached.
#!/usr/bin/perl -w
# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 2 -*-
# Perl script to create a ChangeLog entry with names of files
# and functions from a cvs diff.
#
# Darin Adler <da...@ea...>, started 20 April 2000
# last updated 15 May 2000
#
# (Someone put a license in here, like maybe GPL.)
#
# TODO:
# Provide option to put new ChangeLog into a separate file
# instead of editing the ChangeLog.
# For new files, just say "New file" instead of listing
# function names.
# List functions that have been removed too.
# Decide what a good logical order is for the changed files
# other than a normal text "sort" (top level first?)
# (group directories?) (.h before .c?)
# Leave a diff file behind if asked, but in unified format.
# Handle C++ and yacc source files too (other languages?).
# Help merge when there are ChangeLog conflicts or if there's
# already a partly written ChangeLog entry.
# Find appropriate ChangeLog to edit for each changed file
# instead of always using ChangeLog in current directory.
# Add command line option to put the ChangeLog into a separate
# file or just spew it out stdout.
# Figure out how to allow -z options from .cvsrc to work without
# letting other bad options work. Currently the -f disables
# everything from the .cvsrc.
# Add CVS version numbers for each file too (can't do that until
# the changes are checked in, though).
# Work around diff stupidity where deleting a function that starts
# with a comment makes diff think that the following function
# has been changed (if the following function starts with a comment
# with the same first line, such as /**)
# Work around diff stupidity where deleting an entire function and
# the blank lines before it makes diff think you've changed the
# previous function.
use diagnostics;
use strict;
use English;
use Text::Wrap;
# Read the old change log file.
# It's less efficient to read the whole thing into memory than it would be
# to read it while we prepend to it later, but I like doing this part first.
print STDERR " Updating ChangeLog from cvs repository.\n";
open ERRORS, "cvs update ChangeLog |" or die "The cvs update of ChangeLog failed: $OS_ERROR.\n";
print STDERR " $ARG" while <ERRORS>;
close ERRORS;
open OLD_CHANGE_LOG, "ChangeLog" or die "Could not open ChangeLog file: $OS_ERROR.\n";
my @old_change_log = <OLD_CHANGE_LOG>;
close OLD_CHANGE_LOG;
# For each file, build a list of modified lines.
# Use line numbers from the "after" side of each diff.
print STDERR " Running cvs diff to find changes.\n";
my %changed_line_ranges;
my $file;
open DIFF, "cvs -fq diff -N |" or die "The cvs diff failed: $OS_ERROR.\n";
while (<DIFF>)
{
$file = $1 if /^Index: (\S+)$/;
if (defined $file
and $file ne "ChangeLog"
and /^\d+(,\d+)?[acd](\d+)(,(\d+))?/)
{
push @{$changed_line_ranges{$file}}, [ $2, $4 || $2 ];
}
}
close DIFF;
if (!%changed_line_ranges)
{
print STDERR " No changes found.\n";
exit;
}
# For each ".c" file, convert line range to function list.
print STDERR " Extracting affected function names from C source files.\n";
my %function_lists;
foreach my $file (keys %changed_line_ranges)
{
# An empty function list still indicates that something changed.
$function_lists{$file} = "";
# Only look for function names in .c files.
next unless $file =~ /\.c$/;
# Find all the functions in the file.
open SOURCE, $file or next;
my @function_ranges = get_function_line_ranges(\*SOURCE);
close SOURCE;
# Find all the modified functions.
my @functions;
my @change_ranges = (@{$changed_line_ranges{$file}}, []);
my @change_range = (0, 0);
FUNCTION: foreach my $function_range_ref (@function_ranges)
{
my @function_range = @$function_range_ref;
# Advance to successive change ranges.
for (;; @change_range = @{shift @change_ranges})
{
last FUNCTION unless @change_range;
# If past this function, move on to the next one.
next FUNCTION if $change_range[0] > $function_range[1];
# If an overlap with this function range, record the function name.
if ($change_range[1] >= $function_range[0]
and $change_range[0] <= $function_range[1])
{
push @functions, $function_range[2];
next FUNCTION;
}
}
}
# Format the list of functions now.
$function_lists{$file} = " (" . join("), (", @functions) . "):" if @functions;
}
# Write out a new ChangeLog file.
print STDERR " Editing the ChangeLog file.\n";
my $date = sprintf "%d-%02d-%02d %02d:%02d:%02d",
1900 + (localtime $BASETIME)[5], # year
1 + (localtime $BASETIME)[4], # month
(localtime $BASETIME)[3], # day within month
(localtime $BASETIME)[2], # hour
(localtime $BASETIME)[1], # min
(localtime $BASETIME)[0]; # sec
my $name = $ENV{CHANGE_LOG_NAME}
|| $ENV{REAL_NAME}
|| (getpwuid $REAL_USER_ID)[6]
|| "set REAL_NAME environment variable";
my $email_address = $ENV{CHANGE_LOG_EMAIL_ADDRESS}
|| $ENV{EMAIL_ADDRESS}
|| "set EMAIL_ADDRESS environment variable";
open CHANGE_LOG, "> ChangeLog" or die "Could not write ChangeLog\n.";
print CHANGE_LOG "$date $name <$email_address>\n\n";
foreach my $file (sort keys %function_lists)
{
my $lines = wrap("\t", "\t", "XX$file:$function_lists{$file}");
$lines =~ s/^\tXX/\t* /;
print CHANGE_LOG "$lines\n";
}
print CHANGE_LOG "\n", @old_change_log;
close CHANGE_LOG;
# Done.
print STDERR " Done editing ChangeLog.\n";
exit;
# Read a file and get all the line ranges of the things that look like C functions.
# A function name is the last word before an open parenthesis before the outer
# level open brace. A function starts at the first character after the last close
# brace or semicolon before the function name and ends at the close brace.
# Comment handling is simple-minded but will work for all but pathological cases.
#
# Result is a list of triples: [ start_line, end_line, function_name ].
sub get_function_line_ranges
{
my $file_handle = shift;
my @ranges;
my $in_comment = 0;
my $in_macro = 0;
my $in_parentheses = 0;
my $in_braces = 0;
my $word = "";
my $potential_start = 0;
my $potential_name = "";
my $start = 0;
my $name = "";
while (<$file_handle>)
{
# Handle continued multi-line comment.
if ($in_comment)
{
next unless s-.*\*/--;
$in_comment = 0;
}
# Handle continued macro.
if ($in_macro)
{
$in_macro = 0 unless /\\$/;
next;
}
# Handle start of macro (or any preprocessor directive).
if (/^\s*\#/)
{
$in_macro = 1 if /^([^\\]|\\.)*\\$/;
next;
}
# Handle comments and quoted text.
while (m-(/\*|//|\'|\")-) # \' and \" keep emacs perl mode happy
{
my $match = $1;
if ($match eq "/*")
{
if (!s-/\*.*?\*/--)
{
s-/\*.*--;
$in_comment = 1;
}
}
elsif ($match eq "//")
{
s-//.*--;
}
else # ' or "
{
if (!s-$match([^\\]|\\.)*?$match--)
{
warn "mismatched quotes";
s-$match.*--;
}
}
}
# Find function names.
while (m-(\w+|[(){};])-g)
{
# Open parenthesis.
if ($1 eq "(")
{
$potential_name = $word unless $in_parentheses;
$in_parentheses++;
next;
}
# Close parenthesis.
if ($1 eq ")")
{
$in_parentheses--;
next;
}
# Open brace.
if ($1 eq "{")
{
# Promote potiential name to real function name at the
# start of the outer level set of braces (function body?).
if (!$in_braces and $potential_start)
{
$start = $potential_start;
$name = $potential_name;
}
$in_braces++;
next;
}
# Close brace.
if ($1 eq "}")
{
$in_braces--;
# End of an outer level set of braces.
# This could be a function body.
if (!$in_braces and $name)
{
push @ranges, [ $start, $INPUT_LINE_NUMBER, $name ];
$name = "";
}
$potential_start = 0;
$potential_name = "";
next;
}
# Semicolon.
if ($1 eq ";")
{
$potential_start = 0;
$potential_name = "";
next;
}
# Word.
$word = $1;
if (!$in_parentheses)
{
$potential_start = 0;
$potential_name = "";
}
if (!$potential_start)
{
$potential_start = $INPUT_LINE_NUMBER;
$potential_name = "";
}
}
}
warn "mismatched braces" if $in_braces;
warn "mismatched parentheses" if $in_parentheses;
return @ranges;
}
--
Muli Ben-Yehuda
http://www.mulix.org
|