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);
60 sub findMimeType($;$);
61 sub findModificationType($);
62 sub findSourceFileAndRevision($);
64 sub generateFileList($\%);
65 sub isBinaryMimeType($);
66 sub manufacturePatchForAdditionWithHistory($);
68 sub outputBinaryContent($);
78 my $ignoreChangelogs = 0;
79 my $devNull = File::Spec->devnull();
81 my $result = GetOptions(
83 "ignore-changelogs" => \$ignoreChangelogs
85 if (!$result || $showHelp) {
86 print STDERR basename($0) . " [-h|--help] [--ignore-changelogs] [svndir1 [svndir2 ...]]\n";
90 # Sort the diffs for easier reviewing.
91 my %paths = processPaths(@ARGV);
93 # Generate a list of files requiring diffs.
95 for my $path (keys %paths) {
96 generateFileList($path, %diffFiles);
99 my $svnRoot = determineSVNRoot();
100 my $prefix = chdirReturningRelativePath($svnRoot);
102 # Generate the diffs, in a order chosen for easy reviewing.
103 for my $path (sort patchpathcmp values %diffFiles) {
104 generateDiff($path, $prefix);
109 # Overall sort, considering multiple criteria.
114 # All binary files come after all non-binary files.
115 my $result = binarycmp($a, $b);
116 return $result if $result;
118 # All test files come after all non-test files.
119 $result = testfilecmp($a, $b);
120 return $result if $result;
122 # Final sort is a "smart" sort by directory and file name.
123 return pathcmp($a, $b);
126 # Sort so text files appear before binary files.
129 my ($fileDataA, $fileDataB) = @_;
130 return $fileDataA->{isBinary} <=> $fileDataB->{isBinary};
137 open INFO, "svn info '$infoPath' |" or die;
139 if (/^URL: (.+?)[\r\n]*$/) {
147 sub findMimeType($;$)
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>;
154 # svn may output a different EOL sequence than $/, so avoid chomp.
156 $mimeType =~ s/[\r\n]+$//g;
161 sub findModificationType($)
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";
170 return "modification" if ($fileStat eq "M" || $propertyStat eq "M");
171 return "deletion" if ($fileStat eq "D");
175 sub findSourceFileAndRevision($)
178 my $baseUrl = findBaseUrl(".");
181 open INFO, "svn info '$file' |" or die;
183 if (/^Copied From URL: (.+?)[\r\n]*$/) {
184 $sourceFile = File::Spec->abs2rel($1, $baseUrl);
185 } elsif (/^Copied From Rev: ([0-9]+)/) {
186 $sourceRevision = $1;
190 return ($sourceFile, $sourceRevision);
195 my ($fileData, $prefix) = @_;
196 my $file = File::Spec->catdir($prefix, $fileData->{path});
198 if ($ignoreChangelogs && basename($file) eq "ChangeLog") {
203 if ($fileData->{modificationType} eq "additionWithHistory") {
204 manufacturePatchForAdditionWithHistory($fileData);
206 open DIFF, "svn diff --diff-cmd diff -x -uaNp '$file' |" or die;
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);
219 sub generateFileList($\%)
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;
229 if (isSVNVersion16OrNewer()) {
230 $stat = substr($line, 0, 8);
231 $path = substr($line, 8);
233 $stat = substr($line, 0, 7);
234 $path = substr($line, 7);
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;
249 print STDERR $line, "\n";
255 sub isBinaryMimeType($)
258 my $mimeType = findMimeType($file);
259 return 0 if (!$mimeType || substr($mimeType, 0, 5) eq "text/");
263 sub manufacturePatchForAdditionWithHistory($)
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";
278 print `svn cat ${sourceFile} | diff -u $devNull - | tail -n +3`;
282 # Sort numeric parts of strings as numbers, other parts as strings.
283 # Makes 1.33 come after 1.3, which is cool.
288 my @a = split /(\d+)/, $aa;
289 my @b = split /(\d+)/, $bb;
291 # Compare one chunk at a time.
292 # Each chunk is either all numeric digits, or all not numeric digits.
297 # Use numeric comparison if chunks are non-equal numbers.
298 return $a <=> $b if $a =~ /^\d/ && $b =~ /^\d/ && $a != $b;
300 # Use string comparison if chunks are any other kind of non-equal string.
301 return $a cmp $b if $a ne $b;
304 # One of the two is now empty; compare lengths for result in this case.
308 sub outputBinaryContent($)
312 return if (! -e $path);
313 # Addition or Modification
315 open BINARY, $path or die;
316 while (read(BINARY, $buffer, 60*57)) {
317 print encode_base64($buffer);
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.
329 my ($fileDataA, $fileDataB) = @_;
331 my ($dira, $namea) = splitpath($fileDataA->{path});
332 my ($dirb, $nameb) = splitpath($fileDataB->{path});
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);
343 return ("." => 1) if (!@{$paths});
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)
352 my $untouchedFile = $file;
354 $file = canonicalizePath($file);
356 die "can't handle paths with .. like \"$untouchedFile\"\n" if $file =~ m|/\.\./|;
361 return ("." => 1) if ($result{"."});
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};
376 # Break up a path into the directory (with slash) and base name.
381 my $pathSeparator = "/";
382 my $dirname = dirname($path) . $pathSeparator;
383 $dirname = "" if $dirname eq "." . $pathSeparator;
385 return ($dirname, basename($path));
388 # Sort so source code files appear before test files.
391 my ($fileDataA, $fileDataB) = @_;
392 return $fileDataA->{isTestFile} <=> $fileDataB->{isTestFile};