tizen beta release
[framework/web/webkit-efl.git] / Tools / Scripts / svn-unapply
1 #!/usr/bin/perl -w
2
3 # Copyright (C) 2005, 2006, 2007 Apple Inc.  All rights reserved.
4 # Copyright (C) 2009 Cameron McCormack <cam@mcc.id.au>
5 # Copyright (C) 2010 Chris Jerdonek (chris.jerdonek@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         # FIXME: Add support for Git binary files.
172         if ($isSvnBinary) {
173             # Reverse binary change
174             unlink($fullPath) if (-e $fullPath);
175             system "svn", "revert", $fullPath;
176         } elsif ($deletion) {
177             # Reverse deletion
178             rename($fullPath, "$fullPath.orig") if -e $fullPath;
179
180             unapplyPatch($patch, $fullPath);
181
182             # If we don't ask for the filehandle here, we always get a warning.
183             my ($fh, $tempPath) = tempfile(basename($fullPath) . "-XXXXXXXX",
184                                            DIR => dirname($fullPath), UNLINK => 1);
185             close($fh);
186
187             # Keep the version from the patch in case it's different from svn.
188             rename($fullPath, $tempPath);
189             system "svn", "revert", $fullPath;
190             rename($tempPath, $fullPath);
191
192             # This works around a bug in the svn client.
193             # [Issue 1960] file modifications get lost due to FAT 2s time resolution
194             # http://subversion.tigris.org/issues/show_bug.cgi?id=1960
195             system "touch", $fullPath;
196
197             # Remove $fullPath.orig if it is the same as $fullPath
198             unlink("$fullPath.orig") if -e "$fullPath.orig" && checksum($fullPath) eq checksum("$fullPath.orig");
199
200             # Show status if the file is modifed
201             system "svn", "stat", $fullPath;
202         } elsif ($addition) {
203             # Reverse addition
204             #
205             # FIXME: This should use the same logic as svn-apply's deletion
206             #        code.  In particular, svn-apply's scmRemove() subroutine
207             #        should be used here.
208             unapplyPatch($patch, $fullPath, ["--force"]) if $patch;
209             unlink($fullPath) if -z $fullPath;
210             system "svn", "revert", $fullPath;
211         }
212     }
213
214     scmToggleExecutableBit($fullPath, -1 * $diffHashRef->{executableBitDelta}) if defined($diffHashRef->{executableBitDelta});
215 }
216
217 sub revertDirectories()
218 {
219     chdir $repositoryRootPath;
220     my %checkedDirectories;
221     foreach my $path (reverse sort keys %directoriesToCheck) {
222         my @dirs = File::Spec->splitdir($path);
223         while (scalar @dirs) {
224             my $dir = File::Spec->catdir(@dirs);
225             pop(@dirs);
226             next if (exists $checkedDirectories{$dir});
227             if (-d $dir) {
228                 my $svnOutput = svnStatus($dir);
229                 if ($svnOutput && $svnOutput =~ m#A\s+$dir\n#) {
230                    system "svn", "revert", $dir;
231                    rmdir $dir;
232                 }
233                 elsif ($svnOutput && $svnOutput =~ m#D\s+$dir\n#) {
234                    system "svn", "revert", $dir;
235                 }
236                 else {
237                     # Modification
238                     print $svnOutput if $svnOutput;
239                 }
240                 $checkedDirectories{$dir} = 1;
241             }
242             else {
243                 die "'$dir' is not a directory";
244             }
245         }
246     }
247 }
248
249 # Args:
250 #   $patch: a patch string.
251 #   $pathRelativeToRoot: the path of the file to be patched, relative to the
252 #                        repository root. This should normally be the path
253 #                        found in the patch's "Index:" line.
254 #   $options: a reference to an array of options to pass to the patch command.
255 #             Do not include --reverse in this array.
256 sub unapplyPatch($$;$)
257 {
258     my ($patch, $pathRelativeToRoot, $options) = @_;
259
260     my $optionalArgs = {options => $options, ensureForce => $force, shouldReverse => 1};
261
262     my $exitStatus = runPatchCommand($patch, $repositoryRootPath, $pathRelativeToRoot, $optionalArgs);
263
264     if ($exitStatus) {
265         $globalExitStatus = $exitStatus;
266     }
267 }
268
269 sub unsetChangeLogDate($$)
270 {
271     my $fullPath = shift;
272     my $patch = shift;
273     my $newDate;
274     sysopen(CHANGELOG, $fullPath, O_RDONLY) or die "Failed to open $fullPath: $!";
275     sysseek(CHANGELOG, 0, SEEK_SET);
276     my $byteCount = sysread(CHANGELOG, $newDate, 10);
277     die "Failed reading $fullPath: $!" if !$byteCount || $byteCount != 10;
278     close(CHANGELOG);
279     $patch =~ s/(\n\+)\d{4}-[^-]{2}-[^-]{2}(  )/$1$newDate$2/;
280     return $patch;
281 }