All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dan Carpenter <dan.carpenter@oracle.com>
To: devel@driverdev.osuosl.org
Cc: linux-kernel@vger.kernel.org
Subject: move_rev.pl: tool for reviewing patches that move functions
Date: Thu, 28 Jun 2012 17:13:11 +0300	[thread overview]
Message-ID: <20120628141310.GL5333@mwanda> (raw)

[-- Attachment #1: Type: text/plain, Size: 706 bytes --]

We get a lot of patches in staging that move shift functions around
without changing the code.  For example:
http://marc.info/?l=linux-driver-devel&m=134075825327894&q=raw

I've written a perl script to help review them.  If you take that
example diff then it looks like this:

$ cat patch.txt | diffstat
 cb_pcidas.c |  494 ++++++++++++++++++++++++++----------------------------------
  1 file changed, 219 insertions(+), 275 deletions(-)
$ cat patch.txt | ./move_rev.pl | diffstat
 cb_pcidas.c |   63 ------------------------------------------------------------
  1 file changed, 63 deletions(-)
$

63 lines of deleted code is much quicker to review.  :)

The script is attached.

regards,
dan carpenter

[-- Attachment #2: move_rev.pl --]
[-- Type: text/x-perl, Size: 3498 bytes --]

#!/usr/bin/perl

use strict;
use File::Temp qw/ tempdir /;
use File::Path qw/ rmtree /;
use File::Compare qw/ compare /;

sub print_file($$)
{
    my $prefix = shift;
    my $file = shift;

    open FILE, "<", $file;
    while (<FILE>) {
        print $prefix . $_;
    }
    close FILE;
}

my $olddir = tempdir();
my $newdir = tempdir();

my @input;
while (<>) {
   push @input, $_;
}

my ($nr_old, $nr_new);

#
# Break out the blocks into separate files
#
my $block;
my ($old, $new);
foreach (@input) {
    my $line = $_;

    if ($line =~ /^(---|\+\+\+)/ ||
       !($line =~ /^[+-]/) ||
       ($old && $line =~ /^[+]/) ||
       ($new && $line =~ /^[-]/)) {
        $block = "";
        $old = 0;
        $new = 0;
        next;
    }

    if ($line =~ /^[-]{$/) {
        $block = "{";
        $old = 1;
        next;
    }
    if ($line =~ /^[+]{$/) {
        $block = "{";
        $new = 1;
        next;
    }

    $line =~ s/^[+-]//;

    $block = $block . $line;

    if ($line =~ /^}$/) {
        if ($old) {
            $nr_old++;
            open OUT, ">", "$olddir/$nr_old";
            print OUT "$block";
            close OUT;
        } else {
            $nr_new++;
            open OUT, ">", "$newdir/$nr_new";
            print OUT "$block";
            close OUT;
        }
        $block = "";
        $old = 0;
        $new = 0;
        next;
    }
}

#
# Find any blocks which are exactly the same
#
for (my $i = 1; $i <= $nr_old; $i++) {
    for (my $j = 1; $j <= $nr_new; $j++) {
        if (compare("$olddir/$i", "$newdir/$j") == 0) {
            open OUT, ">", "$olddir/$i";
            print OUT "- MOVED {$i}\n";
            close OUT;

            open OUT, ">", "$newdir/$j";
            print OUT "+ MOVED {$j}\n";
            close OUT;
        }
    }
}

#
# Find any blocks which are partly the same
#
for (my $i = 1; $i <= $nr_old; $i++) {
    for (my $j = 1; $j <= $nr_new; $j++) {
        my $lines = `cat $olddir/$i | wc -l`;
        my $after = `diff $olddir/$i $newdir/$j | grep '^<' | wc -l`;
        my $percent;

        $lines =~ s/\n//;
        $after =~ s/\n//;
        $percent = $after/$lines;

        if ($percent >= .7) {
            next;
        }

        my $diff = `diff -u $olddir/$i $newdir/$j | tail -n +3`;

        open OUT, ">", "$olddir/$i";
        print OUT "- MOVED {$i}\n";
        close OUT;

        open OUT, ">", "$newdir/$j";
        print OUT "+ MOVED {$j} <- begin\n";
        print OUT $diff;
        print OUT "+ MOVED {$j} -> end\n";
        close OUT;
    }
}

#
# Print out the diff...
#
$nr_old = $nr_new = 0;
$old = $new = 0;
foreach (@input) {
    my $line = $_;

    if ($line =~ /^(---|\+\+\+)/ ||
       !($line =~ /^[+-]/) ||
       ($old && $line =~ /^[+]/) ||
       ($new && $line =~ /^[-]/)) {
        print $block;
        print $line;
        $block = "";
        $old = 0;
        $new = 0;
        next;
    }

    if ($line =~ /^[-]{$/) {
        print $block;
        $block = $line;
        $old = 1;
        next;
    }
    if ($line =~ /^[+]{$/) {
        print $block;
        $block = $line;
        $new = 1;
        next;
    }

    $block = $block . $line;

    if ($line =~ /^[+-]}$/) {
        if ($old) {
            $nr_old++;
            print_file("-", "$olddir/$nr_old");
        } else {
            $nr_new++;
            print_file("+", "$newdir/$nr_new");
        }
        $block = "";
        $old = 0;
        $new = 0;
        next;
    }
}

rmtree($olddir);
rmtree($newdir);

                 reply	other threads:[~2012-06-28 14:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120628141310.GL5333@mwanda \
    --to=dan.carpenter@oracle.com \
    --cc=devel@driverdev.osuosl.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.