Re: rename_rev.pl script for reviewing renames

From: Dan Carpenter
Date: Sun Feb 06 2011 - 07:26:00 EST


I've added the some flags:
-e : gets executed on old lines
-ea: executed on everything
-nc: strips one line comments

So say you have a patch that adds parenthesis around a bunch of macros
like this:
http://marc.info/?l=linux-kernel&m=129685142419550&w=2

Then you can just type:
cat /home/dcarpenter/tmp/html23/parens.patch | \
./rename_rev.pl -e 's/(define \w+) (.*)/$1 ($2)/'

Or if you have a camel case script that changes "ThisVariable" to
"this_variable". Then the command would be:
git show c659c38 | ./rename_rev.pl -ea '$_ = lc' -ea 's/_//g'
Which changes everything to lower case and strips out all the
underscores. You might want to combine it with some other flags:
git show c659c38 | ./rename_rev.pl -nc \
-e 's/TLanPrivateInfo/struct tlan_priv/' \
-e 's/TLanList/struct tlan_list/' \
-ea '$_ = lc' -ea 's/_//g'

What I would like is if there was some way to ignore changes which just
introduced new lines, but didn't affect runtime behavior. I'm not sure
how to do that.

regards,
dan carpenter
#!/usr/bin/perl

use strict;
use File::Temp qw/ :mktemp /;

sub usage() {
print "usage: cat diff | $0 old new old new old new...\n";
print " or: cat diff | $0 -e 's/old/new/g'\n";
print " -e : execute on old lines\n";
print " -ea: execute on all lines\n";
print " -nc: strip one line comments\n";
exit(1);
}

my @subs;
my @cmds;
my $strip_comments;

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

my $old = 0;
if ($_ =~ /^-/) {
$old = 1;
}

# remove the first char
s/^[ +-]//;

if ($strip_comments) {
s/\/\*.*?\*\///g;
s/\/\/.*//;
}

foreach my $cmd (@cmds) {
if ($old || $cmd->[0] =~ /^-ea$/) {
eval $cmd->[1];
}
}

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

return $_;
}


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

while (my $param1 = shift()) {
if ($param1 =~ /^-nc$/) {
$strip_comments = 1;
next;
}

my $param2 = shift;
if ($param2 =~ /^$/) {
usage();
}

if ($param1 =~ /^-e(a|)$/) {
push @cmds, [$param1, $param2];
next;
}
push @subs, [$param1, $param2];
}

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

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 -uw $oldfile $newfile");

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