Home | History | Annotate | Download | only in Scripts
      1 #!/usr/bin/perl -w
      2 
      3 # Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
      4 #
      5 # Redistribution and use in source and binary forms, with or without
      6 # modification, are permitted provided that the following conditions
      7 # are met:
      8 #
      9 # 1.  Redistributions of source code must retain the above copyright
     10 #     notice, this list of conditions and the following disclaimer. 
     11 # 2.  Redistributions in binary form must reproduce the above copyright
     12 #     notice, this list of conditions and the following disclaimer in the
     13 #     documentation and/or other materials provided with the distribution. 
     14 # 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     15 #     its contributors may be used to endorse or promote products derived
     16 #     from this software without specific prior written permission. 
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 # Extended "svn diff" script for WebKit Open Source Project, used to make patches.
     30 
     31 # Differences from standard "svn diff":
     32 #
     33 #   Uses the real diff, not svn's built-in diff.
     34 #   Always passes "-p" to diff so it will try to include function names.
     35 #   Handles binary files (encoded as a base64 chunk of text).
     36 #   Sorts the diffs alphabetically by text files, then binary files.
     37 #   Handles copied and moved files.
     38 #
     39 # Missing features:
     40 #
     41 #   Handle copied and moved directories.
     42 
     43 use strict;
     44 use warnings;
     45 
     46 use Config;
     47 use File::Basename;
     48 use File::Spec;
     49 use File::stat;
     50 use FindBin;
     51 use Getopt::Long;
     52 use lib $FindBin::Bin;
     53 use MIME::Base64;
     54 use POSIX qw(:errno_h);
     55 use Time::gmtime;
     56 use VCSUtils;
     57 
     58 sub binarycmp($$);
     59 sub findBaseUrl($);
     60 sub findMimeType($;$);
     61 sub findModificationType($);
     62 sub findSourceFileAndRevision($);
     63 sub generateDiff($$);
     64 sub generateFileList($\%);
     65 sub isBinaryMimeType($);
     66 sub manufacturePatchForAdditionWithHistory($);
     67 sub numericcmp($$);
     68 sub outputBinaryContent($);
     69 sub patchpathcmp($$);
     70 sub pathcmp($$);
     71 sub processPaths(\@);
     72 sub splitpath($);
     73 sub testfilecmp($$);
     74 
     75 $ENV{'LC_ALL'} = 'C';
     76 
     77 my $showHelp;
     78 my $ignoreChangelogs = 0;
     79 my $devNull = File::Spec->devnull();
     80 
     81 my $result = GetOptions(
     82     "help"       => \$showHelp,
     83     "ignore-changelogs"    => \$ignoreChangelogs
     84 );
     85 if (!$result || $showHelp) {
     86     print STDERR basename($0) . " [-h|--help] [--ignore-changelogs] [svndir1 [svndir2 ...]]\n";
     87     exit 1;
     88 }
     89 
     90 # Sort the diffs for easier reviewing.
     91 my %paths = processPaths(@ARGV);
     92 
     93 # Generate a list of files requiring diffs.
     94 my %diffFiles;
     95 for my $path (keys %paths) {
     96     generateFileList($path, %diffFiles);
     97 }
     98 
     99 my $svnRoot = determineSVNRoot();
    100 my $prefix = chdirReturningRelativePath($svnRoot);
    101 
    102 # Generate the diffs, in a order chosen for easy reviewing.
    103 for my $path (sort patchpathcmp values %diffFiles) {
    104     generateDiff($path, $prefix);
    105 }
    106 
    107 exit 0;
    108 
    109 # Overall sort, considering multiple criteria.
    110 sub patchpathcmp($$)
    111 {
    112     my ($a, $b) = @_;
    113 
    114     # All binary files come after all non-binary files.
    115     my $result = binarycmp($a, $b);
    116     return $result if $result;
    117 
    118     # All test files come after all non-test files.
    119     $result = testfilecmp($a, $b);
    120     return $result if $result;
    121 
    122     # Final sort is a "smart" sort by directory and file name.
    123     return pathcmp($a, $b);
    124 }
    125 
    126 # Sort so text files appear before binary files.
    127 sub binarycmp($$)
    128 {
    129     my ($fileDataA, $fileDataB) = @_;
    130     return $fileDataA->{isBinary} <=> $fileDataB->{isBinary};
    131 }
    132 
    133 sub findBaseUrl($)
    134 {
    135     my ($infoPath) = @_;
    136     my $baseUrl;
    137     open INFO, "svn info '$infoPath' |" or die;
    138     while (<INFO>) {
    139         if (/^URL: (.+?)[\r\n]*$/) {
    140             $baseUrl = $1;
    141         }
    142     }
    143     close INFO;
    144     return $baseUrl;
    145 }
    146 
    147 sub findMimeType($;$)
    148 {
    149     my ($file, $revision) = @_;
    150     my $args = $revision ? "--revision $revision" : "";
    151     open PROPGET, "svn propget svn:mime-type $args '$file' |" or die;
    152     my $mimeType = <PROPGET>;
    153     close PROPGET;
    154     # svn may output a different EOL sequence than $/, so avoid chomp.
    155     if ($mimeType) {
    156         $mimeType =~ s/[\r\n]+$//g;
    157     }
    158     return $mimeType;
    159 }
    160 
    161 sub findModificationType($)
    162 {
    163     my ($stat) = @_;
    164     my $fileStat = substr($stat, 0, 1);
    165     my $propertyStat = substr($stat, 1, 1);
    166     if ($fileStat eq "A" || $fileStat eq "R") {
    167         my $additionWithHistory = substr($stat, 3, 1);
    168         return $additionWithHistory eq "+" ? "additionWithHistory" : "addition";
    169     }
    170     return "modification" if ($fileStat eq "M" || $propertyStat eq "M");
    171     return "deletion" if ($fileStat eq "D");
    172     return undef;
    173 }
    174 
    175 sub findSourceFileAndRevision($)
    176 {
    177     my ($file) = @_;
    178     my $baseUrl = findBaseUrl(".");
    179     my $sourceFile;
    180     my $sourceRevision;
    181     open INFO, "svn info '$file' |" or die;
    182     while (<INFO>) {
    183         if (/^Copied From URL: (.+?)[\r\n]*$/) {
    184             $sourceFile = File::Spec->abs2rel($1, $baseUrl);
    185         } elsif (/^Copied From Rev: ([0-9]+)/) {
    186             $sourceRevision = $1;
    187         }
    188     }
    189     close INFO;
    190     return ($sourceFile, $sourceRevision);
    191 }
    192 
    193 sub generateDiff($$)
    194 {
    195     my ($fileData, $prefix) = @_;
    196     my $file = File::Spec->catdir($prefix, $fileData->{path});
    197     
    198     if ($ignoreChangelogs && basename($file) eq "ChangeLog") {
    199         return;
    200     }
    201     
    202     my $patch;
    203     if ($fileData->{modificationType} eq "additionWithHistory") {
    204         manufacturePatchForAdditionWithHistory($fileData);
    205     }
    206     open DIFF, "svn diff --diff-cmd diff -x -uaNp '$file' |" or die;
    207     while (<DIFF>) {
    208         $patch .= $_;
    209     }
    210     close DIFF;
    211     $patch = fixChangeLogPatch($patch) if basename($file) eq "ChangeLog";
    212     print $patch if $patch;
    213     if ($fileData->{isBinary}) {
    214         print "\n" if ($patch && $patch =~ m/\n\S+$/m);
    215         outputBinaryContent($file);
    216     }
    217 }
    218 
    219 sub generateFileList($\%)
    220 {
    221     my ($statPath, $diffFiles) = @_;
    222     my %testDirectories = map { $_ => 1 } qw(LayoutTests);
    223     open STAT, "svn stat '$statPath' |" or die;
    224     while (my $line = <STAT>) {
    225         # svn may output a different EOL sequence than $/, so avoid chomp.
    226         $line =~ s/[\r\n]+$//g;
    227         my $stat;
    228         my $path;
    229         if (isSVNVersion16OrNewer()) {
    230             $stat = substr($line, 0, 8);
    231             $path = substr($line, 8);
    232         } else {
    233             $stat = substr($line, 0, 7);
    234             $path = substr($line, 7);
    235         }
    236         next if -d $path;
    237         my $modificationType = findModificationType($stat);
    238         if ($modificationType) {
    239             $diffFiles->{$path}->{path} = $path;
    240             $diffFiles->{$path}->{modificationType} = $modificationType;
    241             $diffFiles->{$path}->{isBinary} = isBinaryMimeType($path);
    242             $diffFiles->{$path}->{isTestFile} = exists $testDirectories{(File::Spec->splitdir($path))[0]} ? 1 : 0;
    243             if ($modificationType eq "additionWithHistory") {
    244                 my ($sourceFile, $sourceRevision) = findSourceFileAndRevision($path);
    245                 $diffFiles->{$path}->{sourceFile} = $sourceFile;
    246                 $diffFiles->{$path}->{sourceRevision} = $sourceRevision;
    247             }
    248         } else {
    249             print STDERR $line, "\n";
    250         }
    251     }
    252     close STAT;
    253 }
    254 
    255 sub isBinaryMimeType($)
    256 {
    257     my ($file) = @_;
    258     my $mimeType = findMimeType($file);
    259     return 0 if (!$mimeType || substr($mimeType, 0, 5) eq "text/");
    260     return 1;
    261 }
    262 
    263 sub manufacturePatchForAdditionWithHistory($)
    264 {
    265     my ($fileData) = @_;
    266     my $file = $fileData->{path};
    267     print "Index: ${file}\n";
    268     print "=" x 67, "\n";
    269     my $sourceFile = $fileData->{sourceFile};
    270     my $sourceRevision = $fileData->{sourceRevision};
    271     print "--- ${file}\t(revision ${sourceRevision})\t(from ${sourceFile}:${sourceRevision})\n";
    272     print "+++ ${file}\t(working copy)\n";
    273     if ($fileData->{isBinary}) {
    274         print "\nCannot display: file marked as a binary type.\n";
    275         my $mimeType = findMimeType($file, $sourceRevision);
    276         print "svn:mime-type = ${mimeType}\n\n";
    277     } else {
    278         print `svn cat ${sourceFile} | diff -u $devNull - | tail -n +3`;
    279     }
    280 }
    281 
    282 # Sort numeric parts of strings as numbers, other parts as strings.
    283 # Makes 1.33 come after 1.3, which is cool.
    284 sub numericcmp($$)
    285 {
    286     my ($aa, $bb) = @_;
    287 
    288     my @a = split /(\d+)/, $aa;
    289     my @b = split /(\d+)/, $bb;
    290 
    291     # Compare one chunk at a time.
    292     # Each chunk is either all numeric digits, or all not numeric digits.
    293     while (@a && @b) {
    294         my $a = shift @a;
    295         my $b = shift @b;
    296         
    297         # Use numeric comparison if chunks are non-equal numbers.
    298         return $a <=> $b if $a =~ /^\d/ && $b =~ /^\d/ && $a != $b;
    299 
    300         # Use string comparison if chunks are any other kind of non-equal string.
    301         return $a cmp $b if $a ne $b;
    302     }
    303     
    304     # One of the two is now empty; compare lengths for result in this case.
    305     return @a <=> @b;
    306 }
    307 
    308 sub outputBinaryContent($)
    309 {
    310     my ($path) = @_;
    311     # Deletion
    312     return if (! -e $path);
    313     # Addition or Modification
    314     my $buffer;
    315     open BINARY, $path  or die;
    316     while (read(BINARY, $buffer, 60*57)) {
    317         print encode_base64($buffer);
    318     }
    319     close BINARY;
    320     print "\n";
    321 }
    322 
    323 # Sort first by directory, then by file, so all paths in one directory are grouped
    324 # rather than being interspersed with items from subdirectories.
    325 # Use numericcmp to sort directory and filenames to make order logical.
    326 # Also include a special case for ChangeLog, which comes first in any directory.
    327 sub pathcmp($$)
    328 {
    329     my ($fileDataA, $fileDataB) = @_;
    330 
    331     my ($dira, $namea) = splitpath($fileDataA->{path});
    332     my ($dirb, $nameb) = splitpath($fileDataB->{path});
    333 
    334     return numericcmp($dira, $dirb) if $dira ne $dirb;
    335     return -1 if $namea eq "ChangeLog" && $nameb ne "ChangeLog";
    336     return +1 if $namea ne "ChangeLog" && $nameb eq "ChangeLog";
    337     return numericcmp($namea, $nameb);
    338 }
    339 
    340 sub processPaths(\@)
    341 {
    342     my ($paths) = @_;
    343     return ("." => 1) if (!@{$paths});
    344 
    345     my %result = ();
    346 
    347     for my $file (@{$paths}) {
    348         die "can't handle absolute paths like \"$file\"\n" if File::Spec->file_name_is_absolute($file);
    349         die "can't handle empty string path\n" if $file eq "";
    350         die "can't handle path with single quote in the name like \"$file\"\n" if $file =~ /'/; # ' (keep Xcode syntax highlighting happy)
    351 
    352         my $untouchedFile = $file;
    353 
    354         $file = canonicalizePath($file);
    355 
    356         die "can't handle paths with .. like \"$untouchedFile\"\n" if $file =~ m|/\.\./|;
    357 
    358         $result{$file} = 1;
    359     }
    360 
    361     return ("." => 1) if ($result{"."});
    362 
    363     # Remove any paths that also have a parent listed.
    364     for my $path (keys %result) {
    365         for (my $parent = dirname($path); $parent ne '.'; $parent = dirname($parent)) {
    366             if ($result{$parent}) {
    367                 delete $result{$path};
    368                 last;
    369             }
    370         }
    371     }
    372 
    373     return %result;
    374 }
    375 
    376 # Break up a path into the directory (with slash) and base name.
    377 sub splitpath($)
    378 {
    379     my ($path) = @_;
    380 
    381     my $pathSeparator = "/";
    382     my $dirname = dirname($path) . $pathSeparator;
    383     $dirname = "" if $dirname eq "." . $pathSeparator;
    384 
    385     return ($dirname, basename($path));
    386 }
    387 
    388 # Sort so source code files appear before test files.
    389 sub testfilecmp($$)
    390 {
    391     my ($fileDataA, $fileDataB) = @_;
    392     return $fileDataA->{isTestFile} <=> $fileDataB->{isTestFile};
    393 }
    394 
    395