1 #!/usr/bin/perl -w 2 3 # Copyright (C) 2005, 2006, 2007 Apple Inc. All rights reserved. 4 # Copyright (C) 2009 Cameron McCormack <cam (at] mcc.id.au> 5 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek (at] gmail.com) 6 # 7 # Redistribution and use in source and binary forms, with or without 8 # modification, are permitted provided that the following conditions 9 # are met: 10 # 11 # 1. Redistributions of source code must retain the above copyright 12 # notice, this list of conditions and the following disclaimer. 13 # 2. Redistributions in binary form must reproduce the above copyright 14 # notice, this list of conditions and the following disclaimer in the 15 # documentation and/or other materials provided with the distribution. 16 # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 17 # its contributors may be used to endorse or promote products derived 18 # from this software without specific prior written permission. 19 # 20 # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 21 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 24 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 # "unpatch" script for WebKit Open Source Project, used to remove patches. 32 33 # Differences from invoking "patch -p0 -R": 34 # 35 # Handles added files (does a svn revert with additional logic to handle local changes). 36 # Handles added directories (does a svn revert and a rmdir). 37 # Handles removed files (does a svn revert with additional logic to handle local changes). 38 # Handles removed directories (does a svn revert). 39 # Paths from Index: lines are used rather than the paths on the patch lines, which 40 # makes patches generated by "cvs diff" work (increasingly unimportant since we 41 # use Subversion now). 42 # ChangeLog patches use --fuzz=3 to prevent rejects, and the entry date is reset in 43 # the patch before it is applied (svn-apply sets it when applying a patch). 44 # Handles binary files (requires patches made by svn-create-patch). 45 # Handles copied and moved files (requires patches made by svn-create-patch). 46 # Handles git-diff patches (without binary changes) created at the top-level directory 47 # 48 # Missing features: 49 # 50 # Handle property changes. 51 # Handle copied and moved directories (would require patches made by svn-create-patch). 52 # Use version numbers in the patch file and do a 3-way merge. 53 # When reversing an addition, check that the file matches what's being removed. 54 # Notice a patch that's being unapplied at the "wrong level" and make it work anyway. 55 # Do a dry run on the whole patch and don't do anything if part of the patch is 56 # going to fail (probably too strict unless we exclude ChangeLog). 57 # Handle git-diff patches with binary changes 58 59 use strict; 60 use warnings; 61 62 use Cwd; 63 use Digest::MD5; 64 use Fcntl qw(:DEFAULT :seek); 65 use File::Basename; 66 use File::Spec; 67 use File::Temp qw(tempfile); 68 use Getopt::Long; 69 70 use FindBin; 71 use lib $FindBin::Bin; 72 use VCSUtils; 73 74 sub checksum($); 75 sub patch($); 76 sub revertDirectories(); 77 sub unapplyPatch($$;$); 78 sub unsetChangeLogDate($$); 79 80 my $force = 0; 81 my $showHelp = 0; 82 83 my $optionParseSuccess = GetOptions( 84 "force!" => \$force, 85 "help!" => \$showHelp 86 ); 87 88 if (!$optionParseSuccess || $showHelp) { 89 print STDERR basename($0) . " [-h|--help] [--force] patch1 [patch2 ...]\n"; 90 exit 1; 91 } 92 93 my $globalExitStatus = 0; 94 95 my $repositoryRootPath = determineVCSRoot(); 96 97 my @copiedFiles; 98 my %directoriesToCheck; 99 100 # Need to use a typeglob to pass the file handle as a parameter, 101 # otherwise get a bareword error. 102 my @diffHashRefs = parsePatch(*ARGV); 103 104 print "Parsed " . @diffHashRefs . " diffs from patch file(s).\n"; 105 106 my $preparedPatchHash = prepareParsedPatch($force, @diffHashRefs); 107 108 my @copyDiffHashRefs = @{$preparedPatchHash->{copyDiffHashRefs}}; 109 my @nonCopyDiffHashRefs = @{$preparedPatchHash->{nonCopyDiffHashRefs}}; 110 111 for my $diffHashRef (@nonCopyDiffHashRefs) { 112 patch($diffHashRef); 113 } 114 115 # Handle copied and moved files last since they may have had post-copy changes that have now been unapplied 116 for my $diffHashRef (@copyDiffHashRefs) { 117 patch($diffHashRef); 118 } 119 120 if (isSVN()) { 121 revertDirectories(); 122 } 123 124 exit $globalExitStatus; 125 126 sub checksum($) 127 { 128 my $file = shift; 129 open(FILE, $file) or die "Can't open '$file': $!"; 130 binmode(FILE); 131 my $checksum = Digest::MD5->new->addfile(*FILE)->hexdigest(); 132 close(FILE); 133 return $checksum; 134 } 135 136 # Args: 137 # $diffHashRef: a diff hash reference of the type returned by parsePatch(). 138 sub patch($) 139 { 140 my ($diffHashRef) = @_; 141 142 # Make sure $patch is initialized to some value. There is no 143 # svnConvertedText when reversing an svn copy/move. 144 my $patch = $diffHashRef->{svnConvertedText} || ""; 145 146 my $fullPath = $diffHashRef->{indexPath}; 147 my $isSvnBinary = $diffHashRef->{isBinary} && $diffHashRef->{isSvn}; 148 my $hasTextChunks = $patch && $diffHashRef->{numTextChunks}; 149 150 $directoriesToCheck{dirname($fullPath)} = 1; 151 152 my $deletion = 0; 153 my $addition = 0; 154 155 $addition = 1 if ($diffHashRef->{isNew} || $diffHashRef->{copiedFromPath} || $patch =~ /\n@@ -0,0 .* @@/); 156 $deletion = 1 if ($diffHashRef->{isDeletion} || $patch =~ /\n@@ .* \+0,0 @@/); 157 158 if (!$addition && !$deletion && !$isSvnBinary && $hasTextChunks) { 159 # Standard patch, patch tool can handle this. 160 if (basename($fullPath) eq "ChangeLog") { 161 my $changeLogDotOrigExisted = -f "${fullPath}.orig"; 162 my $changeLogHash = fixChangeLogPatch($patch); 163 unapplyPatch(unsetChangeLogDate($fullPath, $changeLogHash->{patch}), $fullPath, ["--fuzz=3"]); 164 unlink("${fullPath}.orig") if (! $changeLogDotOrigExisted); 165 } else { 166 unapplyPatch($patch, $fullPath); 167 } 168 } else { 169 # Either a deletion, an addition or a binary change. 170 171 my $escapedFullPath = escapeSubversionPath($fullPath); 172 # FIXME: Add support for Git binary files. 173 if ($isSvnBinary) { 174 # Reverse binary change 175 unlink($fullPath) if (-e $fullPath); 176 system "svn", "revert", $escapedFullPath; 177 } elsif ($deletion) { 178 # Reverse deletion 179 rename($fullPath, "$fullPath.orig") if -e $fullPath; 180 181 unapplyPatch($patch, $fullPath); 182 183 # If we don't ask for the filehandle here, we always get a warning. 184 my ($fh, $tempPath) = tempfile(basename($fullPath) . "-XXXXXXXX", 185 DIR => dirname($fullPath), UNLINK => 1); 186 close($fh); 187 188 # Keep the version from the patch in case it's different from svn. 189 rename($fullPath, $tempPath); 190 system "svn", "revert", $escapedFullPath; 191 rename($tempPath, $fullPath); 192 193 # This works around a bug in the svn client. 194 # [Issue 1960] file modifications get lost due to FAT 2s time resolution 195 # http://subversion.tigris.org/issues/show_bug.cgi?id=1960 196 system "touch", $fullPath; 197 198 # Remove $fullPath.orig if it is the same as $fullPath 199 unlink("$fullPath.orig") if -e "$fullPath.orig" && checksum($fullPath) eq checksum("$fullPath.orig"); 200 201 # Show status if the file is modifed 202 system "svn", "stat", $escapedFullPath; 203 } elsif ($addition) { 204 # Reverse addition 205 # 206 # FIXME: This should use the same logic as svn-apply's deletion 207 # code. In particular, svn-apply's scmRemove() subroutine 208 # should be used here. 209 unapplyPatch($patch, $fullPath, ["--force"]) if $patch; 210 unlink($fullPath) if -z $fullPath; 211 system "svn", "revert", $escapedFullPath; 212 } 213 } 214 215 scmToggleExecutableBit($fullPath, -1 * $diffHashRef->{executableBitDelta}) if defined($diffHashRef->{executableBitDelta}); 216 } 217 218 sub revertDirectories() 219 { 220 chdir $repositoryRootPath; 221 my %checkedDirectories; 222 foreach my $path (reverse sort keys %directoriesToCheck) { 223 my @dirs = File::Spec->splitdir($path); 224 while (scalar @dirs) { 225 my $dir = File::Spec->catdir(@dirs); 226 pop(@dirs); 227 next if (exists $checkedDirectories{$dir}); 228 if (-d $dir) { 229 my $svnOutput = svnStatus($dir); 230 my $escapedDir = escapeSubversionPath($dir); 231 if ($svnOutput && $svnOutput =~ m#A\s+$dir\n#) { 232 system "svn", "revert", $escapedDir; 233 rmdir $dir; 234 } 235 elsif ($svnOutput && $svnOutput =~ m#D\s+$dir\n#) { 236 system "svn", "revert", $escapedDir; 237 } 238 else { 239 # Modification 240 print $svnOutput if $svnOutput; 241 } 242 $checkedDirectories{$dir} = 1; 243 } 244 else { 245 die "'$dir' is not a directory"; 246 } 247 } 248 } 249 } 250 251 # Args: 252 # $patch: a patch string. 253 # $pathRelativeToRoot: the path of the file to be patched, relative to the 254 # repository root. This should normally be the path 255 # found in the patch's "Index:" line. 256 # $options: a reference to an array of options to pass to the patch command. 257 # Do not include --reverse in this array. 258 sub unapplyPatch($$;$) 259 { 260 my ($patch, $pathRelativeToRoot, $options) = @_; 261 262 my $optionalArgs = {options => $options, ensureForce => $force, shouldReverse => 1}; 263 264 my $exitStatus = runPatchCommand($patch, $repositoryRootPath, $pathRelativeToRoot, $optionalArgs); 265 266 if ($exitStatus) { 267 $globalExitStatus = $exitStatus; 268 } 269 } 270 271 sub unsetChangeLogDate($$) 272 { 273 my $fullPath = shift; 274 my $patch = shift; 275 my $newDate; 276 sysopen(CHANGELOG, $fullPath, O_RDONLY) or die "Failed to open $fullPath: $!"; 277 sysseek(CHANGELOG, 0, SEEK_SET); 278 my $byteCount = sysread(CHANGELOG, $newDate, 10); 279 die "Failed reading $fullPath: $!" if !$byteCount || $byteCount != 10; 280 close(CHANGELOG); 281 $patch =~ s/(\n\+)\d{4}-[^-]{2}-[^-]{2}( )/$1$newDate$2/; 282 return $patch; 283 } 284