3 # Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
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.
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.
29 # Extended "svn diff" script for WebKit Open Source Project, used to make patches.
31 # Differences from standard "svn diff":
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.
41 # Handle copied and moved directories.
52 use lib $FindBin::Bin;
54 use POSIX qw(:errno_h);
59 sub diffOptionsForFile($);
61 sub findMimeType($;$);
62 sub findModificationType($);
63 sub findSourceFileAndRevision($);
65 sub generateFileList($\%);
66 sub hunkHeaderLineRegExForFile($);
67 sub isBinaryMimeType($);
68 sub manufacturePatchForAdditionWithHistory($);
70 sub outputBinaryContent($);
80 my $ignoreChangelogs = 0;
81 my $devNull = File::Spec->devnull();
83 my $result = GetOptions(
85 "ignore-changelogs" => \$ignoreChangelogs
87 if (!$result || $showHelp) {
88 print STDERR basename($0) . " [-h|--help] [--ignore-changelogs] [svndir1 [svndir2 ...]]\n";
92 # Sort the diffs for easier reviewing.
93 my %paths = processPaths(@ARGV);
95 # Generate a list of files requiring diffs.
97 for my $path (keys %paths) {
98 generateFileList($path, %diffFiles);
101 my $svnRoot = determineSVNRoot();
102 my $prefix = chdirReturningRelativePath($svnRoot);
106 # Generate the diffs, in a order chosen for easy reviewing.
107 for my $path (sort patchpathcmp values %diffFiles) {
108 $patchSize += generateDiff($path, $prefix);
111 if ($patchSize > 20480) {
112 print STDERR "WARNING: Patch's size is " . int($patchSize/1024) . " kbytes.\n";
113 print STDERR "Patches 20k or smaller are more likely to be reviewed. Larger patches may sit unreviewed for a long time.\n";
118 # Overall sort, considering multiple criteria.
123 # All binary files come after all non-binary files.
124 my $result = binarycmp($a, $b);
125 return $result if $result;
127 # All test files come after all non-test files.
128 $result = testfilecmp($a, $b);
129 return $result if $result;
131 # Final sort is a "smart" sort by directory and file name.
132 return pathcmp($a, $b);
135 # Sort so text files appear before binary files.
138 my ($fileDataA, $fileDataB) = @_;
139 return $fileDataA->{isBinary} <=> $fileDataB->{isBinary};
142 sub diffOptionsForFile($)
146 my $options = "uaNp";
148 if (my $hunkHeaderLineRegEx = hunkHeaderLineRegExForFile($file)) {
149 $options .= "F'$hunkHeaderLineRegEx'";
159 open INFO, "svn info '$infoPath' |" or die;
161 if (/^URL: (.+?)[\r\n]*$/) {
169 sub findMimeType($;$)
171 my ($file, $revision) = @_;
172 my $args = $revision ? "--revision $revision" : "";
173 open PROPGET, "svn propget svn:mime-type $args '$file' |" or die;
174 my $mimeType = <PROPGET>;
176 # svn may output a different EOL sequence than $/, so avoid chomp.
178 $mimeType =~ s/[\r\n]+$//g;
183 sub findModificationType($)
186 my $fileStat = substr($stat, 0, 1);
187 my $propertyStat = substr($stat, 1, 1);
188 if ($fileStat eq "A" || $fileStat eq "R") {
189 my $additionWithHistory = substr($stat, 3, 1);
190 return $additionWithHistory eq "+" ? "additionWithHistory" : "addition";
192 return "modification" if ($fileStat eq "M" || $propertyStat eq "M");
193 return "deletion" if ($fileStat eq "D");
197 sub findSourceFileAndRevision($)
200 my $baseUrl = findBaseUrl(".");
203 open INFO, "svn info '$file' |" or die;
205 if (/^Copied From URL: (.+?)[\r\n]*$/) {
206 $sourceFile = File::Spec->abs2rel($1, $baseUrl);
207 } elsif (/^Copied From Rev: ([0-9]+)/) {
208 $sourceRevision = $1;
212 return ($sourceFile, $sourceRevision);
217 my ($fileData, $prefix) = @_;
218 my $file = File::Spec->catdir($prefix, $fileData->{path});
220 if ($ignoreChangelogs && basename($file) eq "ChangeLog") {
225 if ($fileData->{modificationType} eq "additionWithHistory") {
226 manufacturePatchForAdditionWithHistory($fileData);
229 my $diffOptions = diffOptionsForFile($file);
230 open DIFF, "svn diff --diff-cmd diff -x -$diffOptions '$file' |" or die;
235 if (basename($file) eq "ChangeLog") {
236 my $changeLogHash = fixChangeLogPatch($patch);
237 $patch = $changeLogHash->{patch};
240 if ($fileData->{isBinary}) {
241 print "\n" if ($patch && $patch =~ m/\n\S+$/m);
242 outputBinaryContent($file);
244 return length($patch);
247 sub generateFileList($\%)
249 my ($statPath, $diffFiles) = @_;
250 my %testDirectories = map { $_ => 1 } qw(LayoutTests);
251 open STAT, "svn stat '$statPath' |" or die;
252 while (my $line = <STAT>) {
253 # svn may output a different EOL sequence than $/, so avoid chomp.
254 $line =~ s/[\r\n]+$//g;
257 if (isSVNVersion16OrNewer()) {
258 $stat = substr($line, 0, 8);
259 $path = substr($line, 8);
261 $stat = substr($line, 0, 7);
262 $path = substr($line, 7);
265 my $modificationType = findModificationType($stat);
266 if ($modificationType) {
267 $diffFiles->{$path}->{path} = $path;
268 $diffFiles->{$path}->{modificationType} = $modificationType;
269 $diffFiles->{$path}->{isBinary} = isBinaryMimeType($path);
270 $diffFiles->{$path}->{isTestFile} = exists $testDirectories{(File::Spec->splitdir($path))[0]} ? 1 : 0;
271 if ($modificationType eq "additionWithHistory") {
272 my ($sourceFile, $sourceRevision) = findSourceFileAndRevision($path);
273 $diffFiles->{$path}->{sourceFile} = $sourceFile;
274 $diffFiles->{$path}->{sourceRevision} = $sourceRevision;
277 print STDERR $line, "\n";
283 sub hunkHeaderLineRegExForFile($)
287 my $startOfObjCInterfaceRegEx = "@(implementation\\|interface\\|protocol)";
288 return "^[-+]\\|$startOfObjCInterfaceRegEx" if $file =~ /\.mm?$/;
289 return "^$startOfObjCInterfaceRegEx" if $file =~ /^(.*\/)?(mac|objc)\// && $file =~ /\.h$/;
292 sub isBinaryMimeType($)
295 my $mimeType = findMimeType($file);
296 return 0 if (!$mimeType || substr($mimeType, 0, 5) eq "text/");
300 sub manufacturePatchForAdditionWithHistory($)
303 my $file = $fileData->{path};
304 print "Index: ${file}\n";
305 print "=" x 67, "\n";
306 my $sourceFile = $fileData->{sourceFile};
307 my $sourceRevision = $fileData->{sourceRevision};
308 print "--- ${file}\t(revision ${sourceRevision})\t(from ${sourceFile}:${sourceRevision})\n";
309 print "+++ ${file}\t(working copy)\n";
310 if ($fileData->{isBinary}) {
311 print "\nCannot display: file marked as a binary type.\n";
312 my $mimeType = findMimeType($file, $sourceRevision);
313 print "svn:mime-type = ${mimeType}\n\n";
315 print `svn cat ${sourceFile} | diff -u $devNull - | tail -n +3`;
319 # Sort numeric parts of strings as numbers, other parts as strings.
320 # Makes 1.33 come after 1.3, which is cool.
325 my @a = split /(\d+)/, $aa;
326 my @b = split /(\d+)/, $bb;
328 # Compare one chunk at a time.
329 # Each chunk is either all numeric digits, or all not numeric digits.
334 # Use numeric comparison if chunks are non-equal numbers.
335 return $a <=> $b if $a =~ /^\d/ && $b =~ /^\d/ && $a != $b;
337 # Use string comparison if chunks are any other kind of non-equal string.
338 return $a cmp $b if $a ne $b;
341 # One of the two is now empty; compare lengths for result in this case.
345 sub outputBinaryContent($)
349 return if (! -e $path);
350 # Addition or Modification
352 open BINARY, $path or die;
353 while (read(BINARY, $buffer, 60*57)) {
354 print encode_base64($buffer);
360 # Sort first by directory, then by file, so all paths in one directory are grouped
361 # rather than being interspersed with items from subdirectories.
362 # Use numericcmp to sort directory and filenames to make order logical.
363 # Also include a special case for ChangeLog, which comes first in any directory.
366 my ($fileDataA, $fileDataB) = @_;
368 my ($dira, $namea) = splitpath($fileDataA->{path});
369 my ($dirb, $nameb) = splitpath($fileDataB->{path});
371 return numericcmp($dira, $dirb) if $dira ne $dirb;
372 return -1 if $namea eq "ChangeLog" && $nameb ne "ChangeLog";
373 return +1 if $namea ne "ChangeLog" && $nameb eq "ChangeLog";
374 return numericcmp($namea, $nameb);
380 return ("." => 1) if (!@{$paths});
384 for my $file (@{$paths}) {
385 die "can't handle absolute paths like \"$file\"\n" if File::Spec->file_name_is_absolute($file);
386 die "can't handle empty string path\n" if $file eq "";
387 die "can't handle path with single quote in the name like \"$file\"\n" if $file =~ /'/; # ' (keep Xcode syntax highlighting happy)
389 my $untouchedFile = $file;
391 $file = canonicalizePath($file);
393 die "can't handle paths with .. like \"$untouchedFile\"\n" if $file =~ m|/\.\./|;
398 return ("." => 1) if ($result{"."});
400 # Remove any paths that also have a parent listed.
401 for my $path (keys %result) {
402 for (my $parent = dirname($path); $parent ne '.'; $parent = dirname($parent)) {
403 if ($result{$parent}) {
404 delete $result{$path};
413 # Break up a path into the directory (with slash) and base name.
418 my $pathSeparator = "/";
419 my $dirname = dirname($path) . $pathSeparator;
420 $dirname = "" if $dirname eq "." . $pathSeparator;
422 return ($dirname, basename($path));
425 # Sort so source code files appear before test files.
428 my ($fileDataA, $fileDataB) = @_;
429 return $fileDataA->{isTestFile} <=> $fileDataB->{isTestFile};