Reviewed by Darin.
        Created by Eric.
        Tweaked and tested by me.

        New scripts to work with Subversion when the switch happens.
        These will replace cvs-apply, cvs-unapply, and cvs-create-patch.

        * Scripts/svn-apply: Added.
        * Scripts/svn-create-patch: Added.
        * Scripts/svn-unapply: Added.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@11875 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/WebKitTools/Scripts/svn-apply b/WebKitTools/Scripts/svn-apply
new file mode 100755
index 0000000..d42630e
--- /dev/null
+++ b/WebKitTools/Scripts/svn-apply
@@ -0,0 +1,160 @@
+#!/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 WebKit Open Source Project, used to apply patches.
+
+# Differences from invoking "patch -p0":
+#
+#   Handles added files (does a svn add).
+#   Handles removed files (does a svn rm).
+#   Has mode where it will roll back to svn version numbers in the patch file so svn can do a 3-way merge.
+#
+# Missing features:
+#
+#   Handle binary files.
+#   When doing a removal, doesn't check that old file matches what's being removed.
+#   Notice a patch that's being applied at the "wrong level" and make it work anyway.
+
+use strict;
+use Cwd;
+use Getopt::Long;
+
+my $merge = 0;
+GetOptions("merge" => \$merge); 
+
+my $startDir = getcwd();
+
+my @patches;
+my %versions;
+
+my $indexPath;
+my $patch;
+while (<>) {
+    s/\r//g;
+    chomp;
+    if (/^Index: (.+)/) {
+        $indexPath = $1;
+        if ($patch) {
+            push @patches, $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 = "";
+        }
+    }
+    if (/^--- .+\(revision (\d+)\)$/) {
+        $versions{$indexPath} = $1 if( $1 != 0 );
+    }
+    $patch .= $_;
+    $patch .= "\n";
+}
+
+push @patches, $patch if $patch;
+
+if ($merge) {
+    for my $file (sort keys %versions) {
+        print "Getting version $versions{$file} of $file\n";
+        $file =~ m|^(([^/\n]*/)*)([^/\n]+)$| or die;
+        my ($prefix, $base) = ($1, $3);
+        if ($prefix) {
+            chdir $prefix or die;
+        }
+        system "svn update -r $versions{$file} $base";
+        chdir $startDir;
+    }
+}
+
+for $patch (@patches) {
+    patch($patch);
+}
+
+sub patch
+{
+    my ($patch) = @_;
+    return if !$patch;
+
+    $patch =~ m|^Index: ((([^/\n]*/)*)([^/\n]+))| or die "Failed to find Index: in \"$patch\"\n";
+    my ($fullpath, $prefix, $base) = ($1, $2, $4);
+
+    my $previousRevision = 0;
+	if($patch =~ /\n--- .+\(revision (\d+)\)\n/) { 
+	    $previousRevision = $1;
+	}
+
+    if ($previousRevision != 0 || $patch !~ /\ndiff -N/) {
+        # Standard patch, patch tool can handle this.
+        open PATCH, "| patch -p0" or die "Failed to patch $fullpath\n";
+        print PATCH $patch;
+        close PATCH;
+    } else {
+        # Either a deletion or an addition.
+
+        # Change directory down into the directory in question.
+        chdirAddingDirectoriesIfNeeded($prefix);
+
+        if ($patch =~ /\n@@ .* \+0,0 @@/) {
+            # Deletion.
+            system "svn", "rm", $base;
+        } else {
+            # Addition.
+            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;
+            system "svn", "add", "$base";
+        }
+
+        chdir $startDir if $prefix;
+    }
+}
+
+sub chdirAddingDirectoriesIfNeeded
+{
+    my $path = shift;
+    my @dirs = split('/', $path);
+    while (my $dir = shift @dirs) {
+        if (!-x $dir) {
+            mkdir $dir or die "Failed create required directory: $dir for path: $path\n";
+            system "svn", "add", "$dir";
+        }
+        chdir $dir or die "Failed to chdir to $dir\n";
+    }
+}
\ No newline at end of file