prepare-ChangeLog.pl revision 9243
12248N/A#!/usr/bin/perl -w
12251N/A# -*- Mode: perl; indent-tabs-mode: nil; c-basic-offset: 2 -*-
12248N/A
12248N/A# Perl script to create a ChangeLog entry with names of files
12248N/A# and functions from a cvs diff.
12248N/A#
12248N/A# Darin Adler <darin@eazel.com>, started 20 April 2000
12248N/A# Java support added by Maciej Stachowiak <mjs@eazel.com>
12248N/A# Multiple ChangeLog support added by Laszlo (Laca) Peter <laca@sun.com>
12248N/A# last updated 23 May 2006
12248N/A#
12248N/A# (Someone put a license in here, like maybe GPL.)
12248N/A#
12248N/A# TODO:
12248N/A# Provide option to put new ChangeLog into a separate file
12248N/A# instead of editing the ChangeLog.
12248N/A# For new files, just say "New file" instead of listing
12248N/A# function names.
12248N/A# List functions that have been removed too.
12248N/A# Decide what a good logical order is for the changed files
12248N/A# other than a normal text "sort" (top level first?)
12248N/A# (group directories?) (.h before .c?)
12248N/A# Leave a diff file behind if asked, but in unified format.
12248N/A# Handle C++ and yacc source files too (other languages?).
12248N/A# Help merge when there are ChangeLog conflicts or if there's
12248N/A# already a partly written ChangeLog entry.
12248N/A# Add command line option to put the ChangeLog into a separate
12248N/A# file or just spew it out stdout.
12248N/A# Figure out how to allow -z options from .cvsrc to work without
12248N/A# letting other bad options work. Currently the -f disables
12248N/A# everything from the .cvsrc.
12248N/A# Add CVS version numbers for each file too (can't do that until
12248N/A# the changes are checked in, though).
12248N/A# Work around diff stupidity where deleting a function that starts
12248N/A# with a comment makes diff think that the following function
12251N/A# has been changed (if the following function starts with a comment
12251N/A# with the same first line, such as /**)
12248N/A# Work around diff stupidity where deleting an entire function and
12248N/A# the blank lines before it makes diff think you've changed the
12248N/A# previous function.
12248N/A
12248N/Ause diagnostics;
12248N/Ause strict;
12248N/A
12248N/Ause English;
12248N/Ause Text::Wrap;
12248N/Ause File::Basename;
12248N/A
12248N/A# Check for cvs or svn system
12248N/Amy $command;
12248N/Aif (-e "CVS/Root")
12248N/A {
12248N/A $command = "cvs";
12248N/A }
12248N/Aelsif (-e ".svn/entries")
12248N/A {
12248N/A $command = "svn";
12248N/A }
12248N/Aelse
12248N/A {
12248N/A die "There is not known revision system.\n"
12248N/A }
12248N/A
12248N/A# Update the change log file.
12248N/Asub update_change_log ($) {
12248N/A my $logname = shift;
12248N/A if ($command eq "cvs") {
12248N/A print STDERR " Updating $logname from cvs repository.\n";
12248N/A open ERRORS, "cvs update $logname |" or die "The cvs update of ChangeLog failed: $OS_ERROR.\n";
12248N/A } else {
12248N/A print STDERR " Updating $logname from svn repository.\n";
12248N/A open ERRORS, "svn update $logname |" or die "The cvs update of ChangeLog failed: $OS_ERROR.\n";
12248N/A }
12248N/A print STDERR " $ARG" while <ERRORS>;
12248N/A close ERRORS;
12248N/A}
12248N/A
12248N/A# For each file, build a list of modified lines.
12248N/A# Use line numbers from the "after" side of each diff.
12248N/Amy %changed_line_ranges;
12248N/Amy $file;
12248N/Aif ($command eq "cvs")
12248N/A {
12248N/A print STDERR " Running cvs diff to find changes.\n";
12248N/A open DIFF, "cvs -fq diff -N |" or die "The cvs diff failed: $OS_ERROR.\n";
12248N/A }
12248N/Aelse
12248N/A {
12248N/A print STDERR " Running svn diff to find changes.\n";
12248N/A open DIFF, "svn --non-interactive diff --diff-cmd diff -x \"-b\" |" or die "The cvs diff failed: $OS_ERROR.\n";
12248N/A }
12248N/A
12248N/Awhile (<DIFF>)
12248N/A {
12248N/A $file = $1 if /^Index: (\S+)$/;
12248N/A my $basename = basename ($file);
12248N/A if (defined $file
12248N/A and $basename ne "ChangeLog"
12248N/A and (/^\d+(,\d+)?[acd](\d+)(,(\d+))?/ or /^Binary files/ or /^Cannot display: file marked as a binary type./) )
12248N/A {
12248N/A push @{$changed_line_ranges{$file}}, [ $2, $4 || $2 ];
12248N/A }
12248N/A }
12248N/Aclose DIFF;
12248N/Aif (!%changed_line_ranges)
12251N/A {
12248N/A print STDERR " No changes found.\n";
12248N/A exit;
12248N/A }
12248N/A
12248N/A# For each ".c" file, convert line range to function list.
12248N/Aprint STDERR " Extracting affected function names from C source files.\n";
12248N/Amy %function_lists;
12248N/Aforeach my $file (keys %changed_line_ranges)
12248N/A {
12248N/A # An empty function list still indicates that something changed.
12248N/A $function_lists{$file} = "";
12248N/A
12248N/A # Only look for function names in .c files.
12248N/A next unless $file =~ /\.(c|java|cs)/;
12248N/A
12248N/A # Find all the functions in the file.
12248N/A open SOURCE, $file or next;
12248N/A my @function_ranges = get_function_line_ranges(\*SOURCE, $file);
12248N/A close SOURCE;
12248N/A
12248N/A # Find all the modified functions.
12248N/A my @functions;
12248N/A my %saw_function;
12248N/A my @change_ranges = (@{$changed_line_ranges{$file}}, []);
12248N/A my @change_range = (0, 0);
12248N/A FUNCTION: foreach my $function_range_ref (@function_ranges)
12248N/A {
12248N/A my @function_range = @$function_range_ref;
12248N/A
12248N/A # Advance to successive change ranges.
12248N/A for (;; @change_range = @{shift @change_ranges})
12248N/A {
12248N/A last FUNCTION unless @change_range;
12248N/A
12248N/A # If past this function, move on to the next one.
12248N/A next FUNCTION if $change_range[0] > $function_range[1];
12248N/A
12248N/A # If an overlap with this function range, record the function name.
12248N/A if ($change_range[1] >= $function_range[0]
12248N/A and $change_range[0] <= $function_range[1])
12248N/A {
12248N/A if (!$saw_function{$function_range[2]})
12248N/A {
12248N/A $saw_function{$function_range[2]} = 1;
12248N/A push @functions, $function_range[2];
12248N/A }
12248N/A next FUNCTION;
12248N/A }
12248N/A }
12248N/A }
12248N/A
12248N/A # Format the list of functions now.
12248N/A $function_lists{$file} = " (" . join("), (", @functions) . "):" if @functions;
12248N/A }
12248N/A
12248N/A# Write out a new ChangeLog file.
12248N/Aprint STDERR " Finding ChangeLog files:\n";
12248N/Amy %changelogs;
12248N/Aforeach my $file (sort keys %function_lists) {
12248N/A $file = dirname ($file);
12248N/A while ($file ne '.' and $file ne '/') {
12248N/A if (-f "$file/ChangeLog") {
12248N/A $changelogs{"./$file"} = 1;
12248N/A last;
12248N/A }
12248N/A $file = dirname ($file);
12248N/A }
12248N/A}
12248N/A$changelogs{'.'} = 1;
12248N/A
12248N/Aforeach my $chl (reverse sort keys %changelogs) {
12248N/A print STDERR "\t${chl}/ChangeLog\n";
12248N/A}
12248N/A
12248N/Aprint STDERR " Editing the ChangeLog file(s).\n";
12248N/Amy $date = sprintf "%d-%02d-%02d",
12248N/A 1900 + (localtime $BASETIME)[5], # year
12248N/A 1 + (localtime $BASETIME)[4], # month
12248N/A (localtime $BASETIME)[3]; # day within month
12248N/Amy $name = $ENV{CHANGE_LOG_NAME}
12248N/A || $ENV{REAL_NAME}
12248N/A || (getpwuid $REAL_USER_ID)[6]
12248N/A || "set REAL_NAME environment variable";
12248N/Amy $email_address = $ENV{CHANGE_LOG_EMAIL_ADDRESS}
12248N/A || $ENV{EMAIL_ADDRESS}
12248N/A || "set EMAIL_ADDRESS environment variable";
12248N/A
12248N/Aforeach my $chlog (reverse sort keys %changelogs) {
12248N/A update_change_log ("$chlog/ChangeLog");
12248N/A # It's less efficient to read the whole thing into memory than it would be
12248N/A # to read it while we prepend to it later, but I like doing this part first.
12248N/A open OLD_CHANGE_LOG, "${chlog}/ChangeLog" or die "Could not open ChangeLog file: $OS_ERROR.\n";
12248N/A my @old_change_log = <OLD_CHANGE_LOG>;
12248N/A close OLD_CHANGE_LOG;
12248N/A open CHANGE_LOG, "> ${chlog}/ChangeLog" or die "Could not write ChangeLog\n.";
12248N/A print CHANGE_LOG "$date $name <$email_address>\n\n";
12248N/A print CHANGE_LOG "\treviewed by: <delete if not using a buddy>\n\n";
12248N/A foreach my $file (sort keys %function_lists) {
12248N/A my $fname = "./$file";
12248N/A if ($fname =~ /^${chlog}\//) {
12248N/A $fname =~ s/^${chlog}\///;
12248N/A my $lines = wrap("\t", "\t", "XX$fname:$function_lists{$file}");
12251N/A $lines =~ s/^\tXX/\t* /;
12251N/A print CHANGE_LOG "$lines\n";
12251N/A delete ($function_lists{$file});
12248N/A }
12248N/A }
12248N/A print CHANGE_LOG "\n", @old_change_log;
12248N/A close CHANGE_LOG;
12248N/A
# Done.
print STDERR " Done editing ${chlog}/ChangeLog.\n";
last if not (keys %function_lists);
}
exit;
sub get_function_line_ranges
{
my ($file_handle, $file_name) = @_;
if ($file_name =~ /\.c$/) {
return get_function_line_ranges_for_c ($file_handle, $file_name);
} elsif ($file_name =~ /\.java$/) {
return get_function_line_ranges_for_java ($file_handle, $file_name);
} elsif ($file_name =~ /\.cs$/) {
#FIXME write a function to extract from .cs files
return get_function_line_ranges_for_java ($file_handle, $file_name);
}
return ();
}
# 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_for_c
{
my ($file_handle, $file_name) = @_;
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 at line $INPUT_LINE_NUMBER in $file_name\n";
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 in $file_name\n" if $in_braces;
warn "mismatched parentheses in $file_name\n" if $in_parentheses;
return @ranges;
}
# Read a file and get all the line ranges of the things that look like Java
# classes, interfaces and methods.
#
# A class or interface name is the word that immediately follows
# `class' or `interface' when followed by an open curly brace and not
# a semicolon. It can appear at the top level, or inside another class
# or interface block, but not inside a function block
#
# A class or interface starts at the first character after the first close
# brace or after the function name and ends at the close brace.
#
# A function name is the last word before an open parenthesis before
# an open brace rather than a semicolon. It can appear at top level or
# inside a class or interface block, but not inside a function block.
#
# A function starts at the first character after the first close
# brace or after 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_for_java
{
my ($file_handle, $file_name) = @_;
my @current_scopes;
my @ranges;
my $in_comment = 0;
my $in_macro = 0;
my $in_parentheses = 0;
my $in_braces = 0;
my $in_non_block_braces = 0;
my $class_or_interface_just_seen = 0;
my $word = "";
my $potential_start = 0;
my $potential_name = "";
my $potential_name_is_class_or_interface = 0;
my $start = 0;
my $name = "";
my $current_name_is_class_or_interface = 0;
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 at line $INPUT_LINE_NUMBER in $file_name\n";
s-$match.*--;
}
}
}
# Find function names.
while (m-(\w+|[(){};])-g)
{
# Open parenthesis.
if ($1 eq "(")
{
if (!$in_parentheses) {
$potential_name = $word;
$potential_name_is_class_or_interface = 0;
}
$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/class/interface body?).
if (!$in_non_block_braces
and (!$in_braces or $current_name_is_class_or_interface)
and $potential_start)
{
if ($name)
{
push @ranges, [ $start, ($INPUT_LINE_NUMBER - 1),
join ('.', @current_scopes) ];
}
$current_name_is_class_or_interface = $potential_name_is_class_or_interface;
$start = $potential_start;
$name = $potential_name;
push (@current_scopes, $name);
} else {
$in_non_block_braces++;
}
$potential_name = "";
$potential_start = 0;
$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_non_block_braces)
{
if ($name)
{
push @ranges, [ $start, $INPUT_LINE_NUMBER,
join ('.', @current_scopes) ];
pop (@current_scopes);
if (@current_scopes)
{
$current_name_is_class_or_interface = 1;
$start = $INPUT_LINE_NUMBER + 1;
$name = $current_scopes[$#current_scopes-1];
}
else
{
$current_name_is_class_or_interface = 0;
$start = 0;
$name = "";
}
}
}
else
{
$in_non_block_braces-- if $in_non_block_braces;
}
$potential_start = 0;
$potential_name = "";
next;
}
# Semicolon.
if ($1 eq ";")
{
$potential_start = 0;
$potential_name = "";
next;
}
if ($1 eq "class" or $1 eq "interface") {
$class_or_interface_just_seen = 1;
next;
}
# Word.
$word = $1;
if (!$in_parentheses)
{
if ($class_or_interface_just_seen) {
$potential_name = $word;
$potential_start = $INPUT_LINE_NUMBER;
$class_or_interface_just_seen = 0;
$potential_name_is_class_or_interface = 1;
next;
}
}
if (!$potential_start)
{
$potential_start = $INPUT_LINE_NUMBER;
$potential_name = "";
}
$class_or_interface_just_seen = 0;
}
}
warn "mismatched braces in $file_name\n" if $in_braces;
warn "mismatched parentheses in $file_name\n" if $in_parentheses;
return @ranges;
}