rename_rev.pl script for reviewing renames

From: Dan Carpenter
Date: Thu Feb 03 2011 - 05:08:49 EST


There are a lot of refactoring patches where people change camel case
names to kernel style names etc. I've written a script to make it
easier to review them. It's attached.

For example the linked patch is about 7900 lines long. It renames
A_BOOL to bool, TRUE to true and FALSE to false.
http://driverdev.linuxdriverproject.org/pipermail/devel/2011-February/011810.html

cat email.txt | ./rename_rev.pl A_BOOL bool TRUE true FALSE false | less

The resulting diff is 78 lines long (99% reduced). Woohoo!

regards,
dan carpenter

#!/usr/bin/perl

use File::Temp qw/ :mktemp /;

sub usage() {
print "cat diff | transform.pl old new old new old new...\n";
die;
}

my @subs;

sub filter($) {
my $line = shift();

foreach my $sub (@subs) {
$line =~ s/$sub->[0]/$sub->[1]/g;
}

# white space at the end of lines
$line =~ s/ *$//g;
$line =~ s/\t*$//g;

# remove the first char
$line =~ s/^[ +-]//;

# tabs to spaces
$line =~ s/\ {8}/\t/g;

return $line;
}

($oldfh, $oldfile) = mkstemp("/tmp/oldXXXXX");
($newfh, $newfile) = mkstemp("/tmp/newXXXXX");

while (my $param1 = shift()) {
my $param2 = shift;

if ($param2 =~ /^$/) {
usage();
}
push @subs, [$param1, $param2];
}

while (<>) {
my $line = $_;

if ($line =~ /^---/) {
next;
}
if ($line =~ /^\+\+\+/) {
next;
}

my $output = filter($line);
if ($line =~ /^-/) {
print $oldfh $output;
next;
}
if ($line =~ /^\+/) {
print $newfh $output;
next;
}
print $oldfh $output;
print $newfh $output;
}

system("diff -u $oldfile $newfile");

unlink($oldfile);
unlink($newfile);