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 my $escapedInfoPath = escapeSubversionPath($infoPath);
160 open INFO, "svn info '$escapedInfoPath' |" or die;
162 if (/^URL: (.+?)[\r\n]*$/) {
170 sub findMimeType($;$)
172 my ($file, $revision) = @_;
173 my $args = $revision ? "--revision $revision" : "";
174 my $escapedFile = escapeSubversionPath($file);
175 open PROPGET, "svn propget svn:mime-type $args '$escapedFile' |" or die;
176 my $mimeType = <PROPGET>;
178 # svn may output a different EOL sequence than $/, so avoid chomp.
180 $mimeType =~ s/[\r\n]+$//g;
185 sub findModificationType($)
188 my $fileStat = substr($stat, 0, 1);
189 my $propertyStat = substr($stat, 1, 1);
190 if ($fileStat eq "A" || $fileStat eq "R") {
191 my $additionWithHistory = substr($stat, 3, 1);
192 return $additionWithHistory eq "+" ? "additionWithHistory" : "addition";
194 return "modification" if ($fileStat eq "M" || $propertyStat eq "M");
195 return "deletion" if ($fileStat eq "D");
199 sub findSourceFileAndRevision($)
202 my $baseUrl = findBaseUrl(".");
205 my $escapedFile = escapeSubversionPath($file);
206 open INFO, "svn info '$escapedFile' |" or die;
208 if (/^Copied From URL: (.+?)[\r\n]*$/) {
209 $sourceFile = File::Spec->abs2rel($1, $baseUrl);
210 } elsif (/^Copied From Rev: ([0-9]+)/) {
211 $sourceRevision = $1;
215 return ($sourceFile, $sourceRevision);
220 my ($fileData, $prefix) = @_;
221 my $file = File::Spec->catdir($prefix, $fileData->{path});
223 if ($ignoreChangelogs && basename($file) eq "ChangeLog") {
228 if ($fileData->{modificationType} eq "additionWithHistory") {
229 manufacturePatchForAdditionWithHistory($fileData);
232 my $diffOptions = diffOptionsForFile($file);
233 my $escapedFile = escapeSubversionPath($file);
234 open DIFF, "svn diff --diff-cmd diff -x -$diffOptions '$escapedFile' |" or die;
239 if (basename($file) eq "ChangeLog") {
240 my $changeLogHash = fixChangeLogPatch($patch);
241 $patch = $changeLogHash->{patch};
244 if ($fileData->{isBinary}) {
245 print "\n" if ($patch && $patch =~ m/\n\S+$/m);
246 outputBinaryContent($file);
248 return length($patch);
251 sub generateFileList($\%)
253 my ($statPath, $diffFiles) = @_;
254 my %testDirectories = map { $_ => 1 } qw(LayoutTests);
255 my $escapedStatPath = escapeSubversionPath($statPath);
256 open STAT, "svn stat '$escapedStatPath' |" or die;
257 while (my $line = <STAT>) {
258 # svn may output a different EOL sequence than $/, so avoid chomp.
259 $line =~ s/[\r\n]+$//g;
262 if (isSVNVersion16OrNewer()) {
263 $stat = substr($line, 0, 8);
264 $path = substr($line, 8);
266 $stat = substr($line, 0, 7);
267 $path = substr($line, 7);
270 my $modificationType = findModificationType($stat);
271 if ($modificationType) {
272 $diffFiles->{$path}->{path} = $path;
273 $diffFiles->{$path}->{modificationType} = $modificationType;
274 $diffFiles->{$path}->{isBinary} = isBinaryMimeType($path);
275 $diffFiles->{$path}->{isTestFile} = exists $testDirectories{(File::Spec->splitdir($path))[0]} ? 1 : 0;
276 if ($modificationType eq "additionWithHistory") {
277 my ($sourceFile, $sourceRevision) = findSourceFileAndRevision($path);
278 $diffFiles->{$path}->{sourceFile} = $sourceFile;
279 $diffFiles->{$path}->{sourceRevision} = $sourceRevision;
282 print STDERR $line, "\n";
288 sub hunkHeaderLineRegExForFile($)
292 my $startOfObjCInterfaceRegEx = "@(implementation\\|interface\\|protocol)";
293 return "^[-+]\\|$startOfObjCInterfaceRegEx" if $file =~ /\.mm?$/;
294 return "^$startOfObjCInterfaceRegEx" if $file =~ /^(.*\/)?(mac|objc)\// && $file =~ /\.h$/;
297 sub isBinaryMimeType($)
300 my $mimeType = findMimeType($file);
301 return 0 if (!$mimeType || substr($mimeType, 0, 5) eq "text/");
305 sub manufacturePatchForAdditionWithHistory($)
308 my $file = $fileData->{path};
309 print "Index: ${file}\n";
310 print "=" x 67, "\n";
311 my $sourceFile = $fileData->{sourceFile};
312 my $sourceRevision = $fileData->{sourceRevision};
313 print "--- ${file}\t(revision ${sourceRevision})\t(from ${sourceFile}:${sourceRevision})\n";
314 print "+++ ${file}\t(working copy)\n";
315 if ($fileData->{isBinary}) {
316 print "\nCannot display: file marked as a binary type.\n";
317 my $mimeType = findMimeType($file, $sourceRevision);
318 print "svn:mime-type = ${mimeType}\n\n";
320 my $escapedSourceFile = escapeSubversionPath($sourceFile);
321 print `svn cat ${escapedSourceFile} | diff -u $devNull - | tail -n +3`;
325 # Sort numeric parts of strings as numbers, other parts as strings.
326 # Makes 1.33 come after 1.3, which is cool.
331 my @a = split /(\d+)/, $aa;
332 my @b = split /(\d+)/, $bb;
334 # Compare one chunk at a time.
335 # Each chunk is either all numeric digits, or all not numeric digits.
340 # Use numeric comparison if chunks are non-equal numbers.
341 return $a <=> $b if $a =~ /^\d/ && $b =~ /^\d/ && $a != $b;
343 # Use string comparison if chunks are any other kind of non-equal string.
344 return $a cmp $b if $a ne $b;
347 # One of the two is now empty; compare lengths for result in this case.
351 sub outputBinaryContent($)
355 return if (! -e $path);
356 # Addition or Modification
358 open BINARY, $path or die;
359 while (read(BINARY, $buffer, 60*57)) {
360 print encode_base64($buffer);
366 # Sort first by directory, then by file, so all paths in one directory are grouped
367 # rather than being interspersed with items from subdirectories.
368 # Use numericcmp to sort directory and filenames to make order logical.
369 # Also include a special case for ChangeLog, which comes first in any directory.
372 my ($fileDataA, $fileDataB) = @_;
374 my ($dira, $namea) = splitpath($fileDataA->{path});
375 my ($dirb, $nameb) = splitpath($fileDataB->{path});
377 return numericcmp($dira, $dirb) if $dira ne $dirb;
378 return -1 if $namea eq "ChangeLog" && $nameb ne "ChangeLog";
379 return +1 if $namea ne "ChangeLog" && $nameb eq "ChangeLog";
380 return numericcmp($namea, $nameb);
386 return ("." => 1) if (!@{$paths});
390 for my $file (@{$paths}) {
391 die "can't handle absolute paths like \"$file\"\n" if File::Spec->file_name_is_absolute($file);
392 die "can't handle empty string path\n" if $file eq "";
393 die "can't handle path with single quote in the name like \"$file\"\n" if $file =~ /'/; # ' (keep Xcode syntax highlighting happy)
395 my $untouchedFile = $file;
397 $file = canonicalizePath($file);
399 die "can't handle paths with .. like \"$untouchedFile\"\n" if $file =~ m|/\.\./|;
404 return ("." => 1) if ($result{"."});
406 # Remove any paths that also have a parent listed.
407 for my $path (keys %result) {
408 for (my $parent = dirname($path); $parent ne '.'; $parent = dirname($parent)) {
409 if ($result{$parent}) {
410 delete $result{$path};
419 # Break up a path into the directory (with slash) and base name.
424 my $pathSeparator = "/";
425 my $dirname = dirname($path) . $pathSeparator;
426 $dirname = "" if $dirname eq "." . $pathSeparator;
428 return ($dirname, basename($path));
431 # Sort so source code files appear before test files.
434 my ($fileDataA, $fileDataB) = @_;
435 return $fileDataA->{isTestFile} <=> $fileDataB->{isTestFile};