| #!/usr/bin/perl -w |
| |
| # Copyright (C) 2005 Apple Computer, Inc. All rights reserved. |
| # |
| # Redistribution and use in source and binary forms, with or without |
| # modification, are permitted provided that the following conditions |
| # are met: |
| # |
| # 1. Redistributions of source code must retain the above copyright |
| # notice, this list of conditions and the following disclaimer. |
| # 2. Redistributions in binary form must reproduce the above copyright |
| # notice, this list of conditions and the following disclaimer in the |
| # documentation and/or other materials provided with the distribution. |
| # 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| # its contributors may be used to endorse or promote products derived |
| # from this software without specific prior written permission. |
| # |
| # THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| # DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| # "patch" script for Web Kit Open Source Project, used to apply patches. |
| |
| # Differences from invoking "patch -p0": |
| # |
| # Paths from Index: lines are used rather than the paths on the patch lines, which makes |
| # patches generated by "cvs diff" work. |
| # Handles added files (does a cvs add). |
| # Handles removed files (does a cvs rm). |
| # |
| # Missing features: |
| # |
| # Use CVS version numbers in the patch file and do a 3-way merge. |
| # Handle binary files. |
| # When reversing an addition, doesn't check that file matches what's being removed. |
| |
| use strict; |
| use Cwd; |
| |
| my $startDir = getcwd(); |
| |
| my $indexPath; |
| my $patch; |
| while (<>) { |
| s/\r//g; |
| chomp; |
| if (/^Index: (.*)/) { |
| $indexPath = $1; |
| if ($patch) { |
| patch($patch); |
| $patch = ""; |
| } |
| } |
| if ($indexPath) { |
| # Fix paths on diff, ---, and +++ lines to match preceding Index: line. |
| s/\S+$/$indexPath/ if /^diff/; |
| s/^--- \S+/--- $indexPath/; |
| if (s/^\+\+\+ \S+/+++ $indexPath/) { |
| $indexPath = ""; |
| } |
| } |
| $patch .= $_; |
| $patch .= "\n"; |
| } |
| patch($patch); |
| |
| sub patch |
| { |
| my ($patch) = @_; |
| return if !$patch; |
| if ($patch !~ /\ndiff -N/) { |
| # Standard patch, patch tool can handle this. |
| open PATCH, "| patch -p0 -R" or die; |
| print PATCH $patch; |
| close PATCH; |
| } else { |
| # Either a deletion or an addition. |
| |
| $patch =~ m|^Index: (([^/\n]*/)+)([^/\n]+)| or die; |
| my $prefix = $1 || "."; |
| my $base = $3; |
| |
| # Change directory down into the directory in question. |
| chdir $prefix or die; |
| |
| if ($patch =~ /\n@@ .* \+0,0 @@/) { |
| # Reverse a deletion. |
| system "cvs", "add", "$base"; |
| my $file = $patch; |
| if ($file !~ s/^(.*\n)*@@[^\n]+@@\n//) { |
| # Empty file. |
| $file = ""; |
| } else { |
| # Non-empty file: Remove leading - signs. |
| $file =~ s/^-//; |
| $file =~ s/\n-/\n/g; |
| } |
| open FILE, ">", $base or die; |
| print FILE $file; |
| close FILE; |
| } else { |
| # Reverse an addition. |
| system "cvs", "rm", "-f", $base; |
| } |
| |
| chdir $startDir or die; |
| } |
| } |