1 # Copyright (C) 2007, 2008, 2009 Apple Inc. All rights reserved.
2 # Copyright (C) 2009, 2010 Chris Jerdonek (chris.jerdonek@gmail.com)
3 # Copyright (C) Research In Motion Limited 2010. 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 # Module to share code to work with various version control systems.
35 use Cwd qw(); # "qw()" prevents warnings about redefining getcwd() with "use POSIX;"
36 use English; # for $POSTMATCH, etc.
43 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
47 &applyGitBinaryPatchDelta
50 &changeLogEmailAddress
52 &chdirReturningRelativePath
66 &isSVNVersion16OrNewer
71 &pathRelativeToSVNRepositoryRootForPath
76 &scmToggleExecutableBit
77 &setChangeLogDateAndReviewer
78 &svnRevisionForDirectory
95 # Project time zone for Cupertino, CA, US
96 my $changeLogTimeZone = "PST8PDT";
98 my $gitDiffStartRegEx = qr#^diff --git (\w/)?(.+) (\w/)?([^\r\n]+)#;
99 my $svnDiffStartRegEx = qr#^Index: ([^\r\n]+)#;
100 my $svnPropertiesStartRegEx = qr#^Property changes on: ([^\r\n]+)#; # $1 is normally the same as the index path.
101 my $svnPropertyStartRegEx = qr#^(Modified|Name|Added|Deleted): ([^\r\n]+)#; # $2 is the name of the property.
102 my $svnPropertyValueStartRegEx = qr#^ (\+|-|Merged|Reverse-merged) ([^\r\n]+)#; # $2 is the start of the property's value (which may span multiple lines).
104 # This method is for portability. Return the system-appropriate exit
105 # status of a child process.
107 # Args: pass the child error status returned by the last pipe close,
111 my ($returnvalue) = @_;
112 if ($^O eq "MSWin32") {
113 return $returnvalue >> 8;
115 return WEXITSTATUS($returnvalue);
118 # Call a function while suppressing STDERR, and return the return values
120 sub callSilently($@) {
121 my ($func, @args) = @_;
123 # The following pattern was taken from here:
124 # http://www.sdsc.edu/~moreland/courses/IntroPerl/docs/manual/pod/perlfunc/open.html
126 # Also see this Perl documentation (search for "open OLDERR"):
127 # http://perldoc.perl.org/functions/open.html
128 open(OLDERR, ">&STDERR");
130 my @returnValue = &$func(@args);
131 open(STDERR, ">&OLDERR");
137 sub toWindowsLineEndings
140 $text =~ s/\n/\r\n/g;
144 # Note, this method will not error if the file corresponding to the $source path does not exist.
145 sub scmMoveOrRenameFile
147 my ($source, $destination) = @_;
148 return if ! -e $source;
150 system("svn", "move", $source, $destination);
152 system("git", "mv", $source, $destination);
156 # Note, this method will not error if the file corresponding to the path does not exist.
157 sub scmToggleExecutableBit
159 my ($path, $executableBitDelta) = @_;
160 return if ! -e $path;
161 if ($executableBitDelta == 1) {
162 scmAddExecutableBit($path);
163 } elsif ($executableBitDelta == -1) {
164 scmRemoveExecutableBit($path);
168 sub scmAddExecutableBit($)
173 system("svn", "propset", "svn:executable", "on", $path) == 0 or die "Failed to run 'svn propset svn:executable on $path'.";
179 sub scmRemoveExecutableBit($)
184 system("svn", "propdel", "svn:executable", $path) == 0 or die "Failed to run 'svn propdel svn:executable $path'.";
190 sub isGitDirectory($)
193 return system("cd $dir && git rev-parse > " . File::Spec->devnull() . " 2>&1") == 0;
198 return $isGit if defined $isGit;
200 $isGit = isGitDirectory(".");
206 unless (defined $gitBranch) {
207 chomp($gitBranch = `git symbolic-ref -q HEAD`);
208 $gitBranch = "" if exitStatus($?);
209 $gitBranch =~ s#^refs/heads/##;
210 $gitBranch = "" if $gitBranch eq "master";
216 sub isGitBranchBuild()
218 my $branch = gitBranch();
219 chomp(my $override = `git config --bool branch.$branch.webKitBranchBuild`);
220 return 1 if $override eq "true";
221 return 0 if $override eq "false";
223 unless (defined $isGitBranchBuild) {
224 chomp(my $gitBranchBuild = `git config --bool core.webKitBranchBuild`);
225 $isGitBranchBuild = $gitBranchBuild eq "true";
228 return $isGitBranchBuild;
231 sub isSVNDirectory($)
235 return -d File::Spec->catdir($dir, ".svn");
240 return $isSVN if defined $isSVN;
242 $isSVN = isSVNDirectory(".");
248 return $svnVersion if defined $svnVersion;
253 chomp($svnVersion = `svn --version --quiet`);
258 sub isSVNVersion16OrNewer()
260 my $version = svnVersion();
261 return eval "v$version" ge v1.6;
264 sub chdirReturningRelativePath($)
266 my ($directory) = @_;
267 my $previousDirectory = Cwd::getcwd();
269 my $newDirectory = Cwd::getcwd();
270 return "." if $newDirectory eq $previousDirectory;
271 return File::Spec->abs2rel($previousDirectory, $newDirectory);
274 sub determineGitRoot()
276 chomp(my $gitDir = `git rev-parse --git-dir`);
277 return dirname($gitDir);
280 sub determineSVNRoot()
290 # Ignore error messages in case we've run past the root of the checkout.
291 open INFO, "svn info '$path' 2> " . File::Spec->devnull() . " |" or die;
293 if (/^Repository Root: (.+)/) {
296 if (/^Repository UUID: (.+)/) {
299 if ($thisRoot && $thisUUID) {
301 <INFO>; # Consume the rest of the input.
306 # It's possible (e.g. for developers of some ports) to have a WebKit
307 # checkout in a subdirectory of another checkout. So abort if the
308 # repository root or the repository UUID suddenly changes.
310 $repositoryUUID = $thisUUID if !$repositoryUUID;
311 last if $thisUUID ne $repositoryUUID;
314 $repositoryRoot = $thisRoot if !$repositoryRoot;
315 last if $thisRoot ne $repositoryRoot;
318 $path = File::Spec->catdir($parent, $path);
321 return File::Spec->rel2abs($last);
324 sub determineVCSRoot()
327 return determineGitRoot();
331 # Some users have a workflow where svn-create-patch, svn-apply and
332 # svn-unapply are used outside of multiple svn working directores,
333 # so warn the user and assume Subversion is being used in this case.
334 warn "Unable to determine VCS root; assuming Subversion";
338 return determineSVNRoot();
341 sub svnRevisionForDirectory($)
346 if (isSVNDirectory($dir)) {
347 my $svnInfo = `LC_ALL=C svn info $dir | grep Revision:`;
348 ($revision) = ($svnInfo =~ m/Revision: (\d+).*/g);
349 } elsif (isGitDirectory($dir)) {
350 my $gitLog = `cd $dir && LC_ALL=C git log --grep='git-svn-id: ' -n 1 | grep git-svn-id:`;
351 ($revision) = ($gitLog =~ m/ +git-svn-id: .+@(\d+) /g);
353 die "Unable to determine current SVN revision in $dir" unless (defined $revision);
357 sub pathRelativeToSVNRepositoryRootForPath($)
360 my $relativePath = File::Spec->abs2rel($file);
364 $svnInfo = `LC_ALL=C svn info $relativePath`;
366 $svnInfo = `LC_ALL=C git svn info $relativePath`;
369 $svnInfo =~ /.*^URL: (.*?)$/m;
372 $svnInfo =~ /.*^Repository Root: (.*?)$/m;
373 my $repositoryRoot = $1;
375 $svnURL =~ s/$repositoryRoot\///;
379 sub makeFilePathRelative($)
382 return $path unless isGit();
384 unless (defined $gitRoot) {
385 chomp($gitRoot = `git rev-parse --show-cdup`);
387 return $gitRoot . $path;
397 sub adjustPathForRecentRenamings($)
401 if ($fullPath =~ m|^WebCore/| || $fullPath =~ m|^JavaScriptCore/|) {
402 return "Source/$fullPath";
407 sub canonicalizePath($)
411 # Remove extra slashes and '.' directories in path
412 $file = File::Spec->canonpath($file);
414 # Remove '..' directories in path
416 foreach my $dir (File::Spec->splitdir($file)) {
417 if ($dir eq '..' && $#dirs >= 0 && $dirs[$#dirs] ne '..') {
423 return ($#dirs >= 0) ? File::Spec->catdir(@dirs) : ".";
429 return "" unless $line;
431 $line =~ s/[\r\n]+$//g;
439 open SVN, "svn status --non-interactive --non-recursive '$fullPath' |" or die;
441 # When running "svn stat" on a directory, we can't assume that only one
442 # status will be returned (since any files with a status below the
443 # directory will be returned), and we can't assume that the directory will
444 # be first (since any files with unknown status will be listed first).
445 my $normalizedFullPath = File::Spec->catdir(File::Spec->splitdir($fullPath));
447 # Input may use a different EOL sequence than $/, so avoid chomp.
449 my $normalizedStatPath = File::Spec->catdir(File::Spec->splitdir(substr($_, 7)));
450 if ($normalizedFullPath eq $normalizedStatPath) {
455 # Read the rest of the svn command output to avoid a broken pipe warning.
460 # Files will have only one status returned.
461 $svnStatus = removeEOL(<SVN>) . "\n";
467 # Return whether the given file mode is executable in the source control
468 # sense. We make this determination based on whether the executable bit
469 # is set for "others" rather than the stronger condition that it be set
470 # for the user, group, and others. This is sufficient for distinguishing
471 # the default behavior in Git and SVN.
474 # $fileMode: A number or string representing a file mode in octal notation.
477 my $fileMode = shift;
479 return $fileMode % 2;
482 # Parse the next Git diff header from the given file handle, and advance
483 # the handle so the last line read is the first line after the header.
485 # This subroutine dies if given leading junk.
488 # $fileHandle: advanced so the last line read from the handle is the first
489 # line of the header to parse. This should be a line
490 # beginning with "diff --git".
491 # $line: the line last read from $fileHandle
493 # Returns ($headerHashRef, $lastReadLine):
494 # $headerHashRef: a hash reference representing a diff header, as follows--
495 # copiedFromPath: the path from which the file was copied or moved if
496 # the diff is a copy or move.
497 # executableBitDelta: the value 1 or -1 if the executable bit was added or
498 # removed, respectively. New and deleted files have
499 # this value only if the file is executable, in which
500 # case the value is 1 and -1, respectively.
501 # indexPath: the path of the target file.
502 # isBinary: the value 1 if the diff is for a binary file.
503 # isDeletion: the value 1 if the diff is a file deletion.
504 # isCopyWithChanges: the value 1 if the file was copied or moved and
505 # the target file was changed in some way after being
506 # copied or moved (e.g. if its contents or executable
508 # isNew: the value 1 if the diff is for a new file.
509 # shouldDeleteSource: the value 1 if the file was copied or moved and
510 # the source file was deleted -- i.e. if the copy
511 # was actually a move.
512 # svnConvertedText: the header text with some lines converted to SVN
513 # format. Git-specific lines are preserved.
514 # $lastReadLine: the line last read from $fileHandle.
515 sub parseGitDiffHeader($$)
517 my ($fileHandle, $line) = @_;
522 if (/$gitDiffStartRegEx/) {
523 # The first and second paths can differ in the case of copies
524 # and renames. We use the second file path because it is the
526 $indexPath = adjustPathForRecentRenamings($4);
527 # Use $POSTMATCH to preserve the end-of-line character.
528 $_ = "Index: $indexPath$POSTMATCH"; # Convert to SVN format.
530 die("Could not parse leading \"diff --git\" line: \"$line\".");
534 my $foundHeaderEnding;
538 my $newExecutableBit = 0;
539 my $oldExecutableBit = 0;
540 my $shouldDeleteSource = 0;
541 my $similarityIndex = 0;
542 my $svnConvertedText;
544 # Temporarily strip off any end-of-line characters to simplify
545 # regex matching below.
549 if (/^(deleted file|old) mode (\d+)/) {
550 $oldExecutableBit = (isExecutable($2) ? 1 : 0);
551 $isDeletion = 1 if $1 eq "deleted file";
552 } elsif (/^new( file)? mode (\d+)/) {
553 $newExecutableBit = (isExecutable($2) ? 1 : 0);
555 } elsif (/^similarity index (\d+)%/) {
556 $similarityIndex = $1;
557 } elsif (/^copy from (\S+)/) {
558 $copiedFromPath = $1;
559 } elsif (/^rename from (\S+)/) {
560 # FIXME: Record this as a move rather than as a copy-and-delete.
561 # This will simplify adding rename support to svn-unapply.
562 # Otherwise, the hash for a deletion would have to know
563 # everything about the file being deleted in order to
564 # support undoing itself. Recording as a move will also
565 # permit us to use "svn move" and "git move".
566 $copiedFromPath = $1;
567 $shouldDeleteSource = 1;
568 } elsif (/^--- \S+/) {
569 $_ = "--- $indexPath"; # Convert to SVN format.
570 } elsif (/^\+\+\+ \S+/) {
571 $_ = "+++ $indexPath"; # Convert to SVN format.
572 $foundHeaderEnding = 1;
573 } elsif (/^GIT binary patch$/ ) {
575 $foundHeaderEnding = 1;
576 # The "git diff" command includes a line of the form "Binary files
577 # <path1> and <path2> differ" if the --binary flag is not used.
578 } elsif (/^Binary files / ) {
579 die("Error: the Git diff contains a binary file without the binary data in ".
580 "line: \"$_\". Be sure to use the --binary flag when invoking \"git diff\" ".
581 "with diffs containing binary files.");
584 $svnConvertedText .= "$_$eol"; # Also restore end-of-line characters.
586 $_ = <$fileHandle>; # Not defined if end-of-file reached.
588 last if (!defined($_) || /$gitDiffStartRegEx/ || $foundHeaderEnding);
591 my $executableBitDelta = $newExecutableBit - $oldExecutableBit;
595 $header{copiedFromPath} = $copiedFromPath if $copiedFromPath;
596 $header{executableBitDelta} = $executableBitDelta if $executableBitDelta;
597 $header{indexPath} = $indexPath;
598 $header{isBinary} = $isBinary if $isBinary;
599 $header{isCopyWithChanges} = 1 if ($copiedFromPath && ($similarityIndex != 100 || $executableBitDelta));
600 $header{isDeletion} = $isDeletion if $isDeletion;
601 $header{isNew} = $isNew if $isNew;
602 $header{shouldDeleteSource} = $shouldDeleteSource if $shouldDeleteSource;
603 $header{svnConvertedText} = $svnConvertedText;
605 return (\%header, $_);
608 # Parse the next SVN diff header from the given file handle, and advance
609 # the handle so the last line read is the first line after the header.
611 # This subroutine dies if given leading junk or if it could not detect
612 # the end of the header block.
615 # $fileHandle: advanced so the last line read from the handle is the first
616 # line of the header to parse. This should be a line
617 # beginning with "Index:".
618 # $line: the line last read from $fileHandle
620 # Returns ($headerHashRef, $lastReadLine):
621 # $headerHashRef: a hash reference representing a diff header, as follows--
622 # copiedFromPath: the path from which the file was copied if the diff
624 # indexPath: the path of the target file, which is the path found in
626 # isBinary: the value 1 if the diff is for a binary file.
627 # isNew: the value 1 if the diff is for a new file.
628 # sourceRevision: the revision number of the source, if it exists. This
629 # is the same as the revision number the file was copied
630 # from, in the case of a file copy.
631 # svnConvertedText: the header text converted to a header with the paths
632 # in some lines corrected.
633 # $lastReadLine: the line last read from $fileHandle.
634 sub parseSvnDiffHeader($$)
636 my ($fileHandle, $line) = @_;
641 if (/$svnDiffStartRegEx/) {
642 $indexPath = adjustPathForRecentRenamings($1);
644 die("First line of SVN diff does not begin with \"Index \": \"$_\"");
648 my $foundHeaderEnding;
652 my $svnConvertedText;
654 # Temporarily strip off any end-of-line characters to simplify
655 # regex matching below.
659 # Fix paths on ""---" and "+++" lines to match the leading
661 if (s/^--- \S+/--- $indexPath/) {
663 if (/^--- .+\(revision (\d+)\)/) {
664 $sourceRevision = $1;
665 $isNew = 1 if !$sourceRevision; # if revision 0.
666 if (/\(from (\S+):(\d+)\)$/) {
667 # The "from" clause is created by svn-create-patch, in
668 # which case there is always also a "revision" clause.
669 $copiedFromPath = $1;
670 die("Revision number \"$2\" in \"from\" clause does not match " .
671 "source revision number \"$sourceRevision\".") if ($2 != $sourceRevision);
674 } elsif (s/^\+\+\+ \S+/+++ $indexPath/) {
675 $foundHeaderEnding = 1;
676 } elsif (/^Cannot display: file marked as a binary type.$/) {
678 $foundHeaderEnding = 1;
681 $svnConvertedText .= "$_$eol"; # Also restore end-of-line characters.
683 $_ = <$fileHandle>; # Not defined if end-of-file reached.
685 last if (!defined($_) || /$svnDiffStartRegEx/ || $foundHeaderEnding);
688 if (!$foundHeaderEnding) {
689 die("Did not find end of header block corresponding to index path \"$indexPath\".");
694 $header{copiedFromPath} = $copiedFromPath if $copiedFromPath;
695 $header{indexPath} = $indexPath;
696 $header{isBinary} = $isBinary if $isBinary;
697 $header{isNew} = $isNew if $isNew;
698 $header{sourceRevision} = $sourceRevision if $sourceRevision;
699 $header{svnConvertedText} = $svnConvertedText;
701 return (\%header, $_);
704 # Parse the next diff header from the given file handle, and advance
705 # the handle so the last line read is the first line after the header.
707 # This subroutine dies if given leading junk or if it could not detect
708 # the end of the header block.
711 # $fileHandle: advanced so the last line read from the handle is the first
712 # line of the header to parse. For SVN-formatted diffs, this
713 # is a line beginning with "Index:". For Git, this is a line
714 # beginning with "diff --git".
715 # $line: the line last read from $fileHandle
717 # Returns ($headerHashRef, $lastReadLine):
718 # $headerHashRef: a hash reference representing a diff header
719 # copiedFromPath: the path from which the file was copied if the diff
721 # executableBitDelta: the value 1 or -1 if the executable bit was added or
722 # removed, respectively. New and deleted files have
723 # this value only if the file is executable, in which
724 # case the value is 1 and -1, respectively.
725 # indexPath: the path of the target file.
726 # isBinary: the value 1 if the diff is for a binary file.
727 # isGit: the value 1 if the diff is Git-formatted.
728 # isSvn: the value 1 if the diff is SVN-formatted.
729 # sourceRevision: the revision number of the source, if it exists. This
730 # is the same as the revision number the file was copied
731 # from, in the case of a file copy.
732 # svnConvertedText: the header text with some lines converted to SVN
733 # format. Git-specific lines are preserved.
734 # $lastReadLine: the line last read from $fileHandle.
735 sub parseDiffHeader($$)
737 my ($fileHandle, $line) = @_;
739 my $header; # This is a hash ref.
744 if ($line =~ $svnDiffStartRegEx) {
746 ($header, $lastReadLine) = parseSvnDiffHeader($fileHandle, $line);
747 } elsif ($line =~ $gitDiffStartRegEx) {
749 ($header, $lastReadLine) = parseGitDiffHeader($fileHandle, $line);
751 die("First line of diff does not begin with \"Index:\" or \"diff --git\": \"$line\"");
754 $header->{isGit} = $isGit if $isGit;
755 $header->{isSvn} = $isSvn if $isSvn;
757 return ($header, $lastReadLine);
760 # FIXME: The %diffHash "object" should not have an svnConvertedText property.
761 # Instead, the hash object should store its information in a
762 # structured way as properties. This should be done in a way so
763 # that, if necessary, the text of an SVN or Git patch can be
764 # reconstructed from the information in those hash properties.
766 # A %diffHash is a hash representing a source control diff of a single
767 # file operation (e.g. a file modification, copy, or delete).
769 # These hashes appear, for example, in the parseDiff(), parsePatch(),
770 # and prepareParsedPatch() subroutines of this package.
772 # The corresponding values are--
774 # copiedFromPath: the path from which the file was copied if the diff
776 # executableBitDelta: the value 1 or -1 if the executable bit was added or
777 # removed from the target file, respectively.
778 # indexPath: the path of the target file. For SVN-formatted diffs,
779 # this is the same as the path in the "Index:" line.
780 # isBinary: the value 1 if the diff is for a binary file.
781 # isDeletion: the value 1 if the diff is known from the header to be a deletion.
782 # isGit: the value 1 if the diff is Git-formatted.
783 # isNew: the value 1 if the dif is known from the header to be a new file.
784 # isSvn: the value 1 if the diff is SVN-formatted.
785 # sourceRevision: the revision number of the source, if it exists. This
786 # is the same as the revision number the file was copied
787 # from, in the case of a file copy.
788 # svnConvertedText: the diff with some lines converted to SVN format.
789 # Git-specific lines are preserved.
791 # Parse one diff from a patch file created by svn-create-patch, and
792 # advance the file handle so the last line read is the first line
793 # of the next header block.
795 # This subroutine preserves any leading junk encountered before the header.
797 # Composition of an SVN diff
799 # There are three parts to an SVN diff: the header, the property change, and
800 # the binary contents, in that order. Either the header or the property change
801 # may be ommitted, but not both. If there are binary changes, then you always
805 # $fileHandle: a file handle advanced to the first line of the next
806 # header block. Leading junk is okay.
807 # $line: the line last read from $fileHandle.
809 # Returns ($diffHashRefs, $lastReadLine):
810 # $diffHashRefs: A reference to an array of references to %diffHash hashes.
811 # See the %diffHash documentation above.
812 # $lastReadLine: the line last read from $fileHandle
815 # FIXME: Adjust this method so that it dies if the first line does not
816 # match the start of a diff. This will require a change to
817 # parsePatch() so that parsePatch() skips over leading junk.
818 my ($fileHandle, $line) = @_;
820 my $headerStartRegEx = $svnDiffStartRegEx; # SVN-style header for the default
822 my $headerHashRef; # Last header found, as returned by parseDiffHeader().
823 my $svnPropertiesHashRef; # Last SVN properties diff found, as returned by parseSvnDiffProperties().
825 while (defined($line)) {
826 if (!$headerHashRef && ($line =~ $gitDiffStartRegEx)) {
827 # Then assume all diffs in the patch are Git-formatted. This
828 # block was made to be enterable at most once since we assume
829 # all diffs in the patch are formatted the same (SVN or Git).
830 $headerStartRegEx = $gitDiffStartRegEx;
833 if ($line =~ $svnPropertiesStartRegEx) {
834 my $propertyPath = $1;
835 if ($svnPropertiesHashRef || $headerHashRef && ($propertyPath ne $headerHashRef->{indexPath})) {
836 # This is the start of the second diff in the while loop, which happens to
837 # be a property diff. If $svnPropertiesHasRef is defined, then this is the
838 # second consecutive property diff, otherwise it's the start of a property
839 # diff for a file that only has property changes.
842 ($svnPropertiesHashRef, $line) = parseSvnDiffProperties($fileHandle, $line);
845 if ($line !~ $headerStartRegEx) {
846 # Then we are in the body of the diff.
848 $line = <$fileHandle>;
850 } # Otherwise, we found a diff header.
852 if ($svnPropertiesHashRef || $headerHashRef) {
853 # Then either we just processed an SVN property change or this
854 # is the start of the second diff header of this while loop.
858 ($headerHashRef, $line) = parseDiffHeader($fileHandle, $line);
860 $svnText .= $headerHashRef->{svnConvertedText};
865 if ($headerHashRef->{shouldDeleteSource}) {
867 $deletionHash{indexPath} = $headerHashRef->{copiedFromPath};
868 $deletionHash{isDeletion} = 1;
869 push @diffHashRefs, \%deletionHash;
871 if ($headerHashRef->{copiedFromPath}) {
873 $copyHash{copiedFromPath} = $headerHashRef->{copiedFromPath};
874 $copyHash{indexPath} = $headerHashRef->{indexPath};
875 $copyHash{sourceRevision} = $headerHashRef->{sourceRevision} if $headerHashRef->{sourceRevision};
876 if ($headerHashRef->{isSvn}) {
877 $copyHash{executableBitDelta} = $svnPropertiesHashRef->{executableBitDelta} if $svnPropertiesHashRef->{executableBitDelta};
879 push @diffHashRefs, \%copyHash;
882 # Note, the order of evaluation for the following if conditional has been explicitly chosen so that
883 # it evaluates to false when there is no headerHashRef (e.g. a property change diff for a file that
884 # only has property changes).
885 if ($headerHashRef->{isCopyWithChanges} || (%$headerHashRef && !$headerHashRef->{copiedFromPath})) {
886 # Then add the usual file modification.
888 # FIXME: We should expand this code to support other properties. In the future,
889 # parseSvnDiffProperties may return a hash whose keys are the properties.
890 if ($headerHashRef->{isSvn}) {
891 # SVN records the change to the executable bit in a separate property change diff
892 # that follows the contents of the diff, except for binary diffs. For binary
893 # diffs, the property change diff follows the diff header.
894 $diffHash{executableBitDelta} = $svnPropertiesHashRef->{executableBitDelta} if $svnPropertiesHashRef->{executableBitDelta};
895 } elsif ($headerHashRef->{isGit}) {
896 # Git records the change to the executable bit in the header of a diff.
897 $diffHash{executableBitDelta} = $headerHashRef->{executableBitDelta} if $headerHashRef->{executableBitDelta};
899 $diffHash{indexPath} = $headerHashRef->{indexPath};
900 $diffHash{isBinary} = $headerHashRef->{isBinary} if $headerHashRef->{isBinary};
901 $diffHash{isDeletion} = $headerHashRef->{isDeletion} if $headerHashRef->{isDeletion};
902 $diffHash{isGit} = $headerHashRef->{isGit} if $headerHashRef->{isGit};
903 $diffHash{isNew} = $headerHashRef->{isNew} if $headerHashRef->{isNew};
904 $diffHash{isSvn} = $headerHashRef->{isSvn} if $headerHashRef->{isSvn};
905 if (!$headerHashRef->{copiedFromPath}) {
906 # If the file was copied, then we have already incorporated the
907 # sourceRevision information into the change.
908 $diffHash{sourceRevision} = $headerHashRef->{sourceRevision} if $headerHashRef->{sourceRevision};
910 # FIXME: Remove the need for svnConvertedText. See the %diffHash
911 # code comments above for more information.
913 # Note, we may not always have SVN converted text since we intend
914 # to deprecate it in the future. For example, a property change
915 # diff for a file that only has property changes will not return
916 # any SVN converted text.
917 $diffHash{svnConvertedText} = $svnText if $svnText;
918 push @diffHashRefs, \%diffHash;
921 if (!%$headerHashRef && $svnPropertiesHashRef) {
922 # A property change diff for a file that only has property changes.
923 my %propertyChangeHash;
924 $propertyChangeHash{executableBitDelta} = $svnPropertiesHashRef->{executableBitDelta} if $svnPropertiesHashRef->{executableBitDelta};
925 $propertyChangeHash{indexPath} = $svnPropertiesHashRef->{propertyPath};
926 $propertyChangeHash{isSvn} = 1;
927 push @diffHashRefs, \%propertyChangeHash;
930 return (\@diffHashRefs, $line);
933 # Parse an SVN property change diff from the given file handle, and advance
934 # the handle so the last line read is the first line after this diff.
936 # For the case of an SVN binary diff, the binary contents will follow the
937 # the property changes.
939 # This subroutine dies if the first line does not begin with "Property changes on"
940 # or if the separator line that follows this line is missing.
943 # $fileHandle: advanced so the last line read from the handle is the first
944 # line of the footer to parse. This line begins with
945 # "Property changes on".
946 # $line: the line last read from $fileHandle.
948 # Returns ($propertyHashRef, $lastReadLine):
949 # $propertyHashRef: a hash reference representing an SVN diff footer.
950 # propertyPath: the path of the target file.
951 # executableBitDelta: the value 1 or -1 if the executable bit was added or
952 # removed from the target file, respectively.
953 # $lastReadLine: the line last read from $fileHandle.
954 sub parseSvnDiffProperties($$)
956 my ($fileHandle, $line) = @_;
961 if (/$svnPropertiesStartRegEx/) {
962 $footer{propertyPath} = $1;
964 die("Failed to find start of SVN property change, \"Property changes on \": \"$_\"");
967 # We advance $fileHandle two lines so that the next line that
968 # we process is $svnPropertyStartRegEx in a well-formed footer.
969 # A well-formed footer has the form:
970 # Property changes on: FileA
971 # ___________________________________________________________________
972 # Added: svn:executable
974 $_ = <$fileHandle>; # Not defined if end-of-file reached.
975 my $separator = "_" x 67;
976 if (defined($_) && /^$separator[\r\n]+$/) {
979 die("Failed to find separator line: \"$_\".");
982 # FIXME: We should expand this to support other SVN properties
983 # (e.g. return a hash of property key-values that represents
986 # Notice, we keep processing until we hit end-of-file or some
987 # line that does not resemble $svnPropertyStartRegEx, such as
988 # the empty line that precedes the start of the binary contents
989 # of a patch, or the start of the next diff (e.g. "Index:").
991 while (defined($_) && /$svnPropertyStartRegEx/) {
992 ($propertyHashRef, $_) = parseSvnProperty($fileHandle, $_);
993 if ($propertyHashRef->{name} eq "svn:executable") {
994 # Notice, for SVN properties, propertyChangeDelta is always non-zero
995 # because a property can only be added or removed.
996 $footer{executableBitDelta} = $propertyHashRef->{propertyChangeDelta};
1000 return(\%footer, $_);
1003 # Parse the next SVN property from the given file handle, and advance the handle so the last
1004 # line read is the first line after the property.
1006 # This subroutine dies if the first line is not a valid start of an SVN property,
1007 # or the property is missing a value, or the property change type (e.g. "Added")
1008 # does not correspond to the property value type (e.g. "+").
1011 # $fileHandle: advanced so the last line read from the handle is the first
1012 # line of the property to parse. This should be a line
1013 # that matches $svnPropertyStartRegEx.
1014 # $line: the line last read from $fileHandle.
1016 # Returns ($propertyHashRef, $lastReadLine):
1017 # $propertyHashRef: a hash reference representing a SVN property.
1018 # name: the name of the property.
1019 # value: the last property value. For instance, suppose the property is "Modified".
1020 # Then it has both a '-' and '+' property value in that order. Therefore,
1021 # the value of this key is the value of the '+' property by ordering (since
1022 # it is the last value).
1023 # propertyChangeDelta: the value 1 or -1 if the property was added or
1024 # removed, respectively.
1025 # $lastReadLine: the line last read from $fileHandle.
1026 sub parseSvnProperty($$)
1028 my ($fileHandle, $line) = @_;
1033 my $propertyChangeType;
1034 if (/$svnPropertyStartRegEx/) {
1035 $propertyChangeType = $1;
1038 die("Failed to find SVN property: \"$_\".");
1041 $_ = <$fileHandle>; # Not defined if end-of-file reached.
1043 # The "svn diff" command neither inserts newline characters between property values
1044 # nor between successive properties.
1046 # FIXME: We do not support property values that contain tailing newline characters
1047 # as it is difficult to disambiguate these trailing newlines from the empty
1048 # line that precedes the contents of a binary patch.
1050 my $propertyValueType;
1051 while (defined($_) && /$svnPropertyValueStartRegEx/) {
1052 # Note, a '-' property may be followed by a '+' property in the case of a "Modified"
1053 # or "Name" property. We only care about the ending value (i.e. the '+' property)
1054 # in such circumstances. So, we take the property value for the property to be its
1055 # last parsed property value.
1057 # FIXME: We may want to consider strictly enforcing a '-', '+' property ordering or
1058 # add error checking to prevent '+', '+', ..., '+' and other invalid combinations.
1059 $propertyValueType = $1;
1060 ($propertyValue, $_) = parseSvnPropertyValue($fileHandle, $_);
1063 if (!$propertyValue) {
1064 die("Failed to find the property value for the SVN property \"$propertyName\": \"$_\".");
1067 my $propertyChangeDelta;
1068 if ($propertyValueType eq "+" || $propertyValueType eq "Merged") {
1069 $propertyChangeDelta = 1;
1070 } elsif ($propertyValueType eq "-" || $propertyValueType eq "Reverse-merged") {
1071 $propertyChangeDelta = -1;
1073 die("Not reached.");
1076 # We perform a simple validation that an "Added" or "Deleted" property
1077 # change type corresponds with a "+" and "-" value type, respectively.
1078 my $expectedChangeDelta;
1079 if ($propertyChangeType eq "Added") {
1080 $expectedChangeDelta = 1;
1081 } elsif ($propertyChangeType eq "Deleted") {
1082 $expectedChangeDelta = -1;
1085 if ($expectedChangeDelta && $propertyChangeDelta != $expectedChangeDelta) {
1086 die("The final property value type found \"$propertyValueType\" does not " .
1087 "correspond to the property change type found \"$propertyChangeType\".");
1091 $propertyHash{name} = $propertyName;
1092 $propertyHash{propertyChangeDelta} = $propertyChangeDelta;
1093 $propertyHash{value} = $propertyValue;
1094 return (\%propertyHash, $_);
1097 # Parse the value of an SVN property from the given file handle, and advance
1098 # the handle so the last line read is the first line after the property value.
1100 # This subroutine dies if the first line is an invalid SVN property value line
1101 # (i.e. a line that does not begin with " +" or " -").
1104 # $fileHandle: advanced so the last line read from the handle is the first
1105 # line of the property value to parse. This should be a line
1106 # beginning with " +" or " -".
1107 # $line: the line last read from $fileHandle.
1109 # Returns ($propertyValue, $lastReadLine):
1110 # $propertyValue: the value of the property.
1111 # $lastReadLine: the line last read from $fileHandle.
1112 sub parseSvnPropertyValue($$)
1114 my ($fileHandle, $line) = @_;
1120 if (/$svnPropertyValueStartRegEx/) {
1121 $propertyValue = $2; # Does not include the end-of-line character(s).
1124 die("Failed to find property value beginning with '+', '-', 'Merged', or 'Reverse-merged': \"$_\".");
1127 while (<$fileHandle>) {
1128 if (/^[\r\n]+$/ || /$svnPropertyValueStartRegEx/ || /$svnPropertyStartRegEx/) {
1129 # Note, we may encounter an empty line before the contents of a binary patch.
1130 # Also, we check for $svnPropertyValueStartRegEx because a '-' property may be
1131 # followed by a '+' property in the case of a "Modified" or "Name" property.
1132 # We check for $svnPropertyStartRegEx because it indicates the start of the
1133 # next property to parse.
1137 # Temporarily strip off any end-of-line characters. We add the end-of-line characters
1138 # from the previously processed line to the start of this line so that the last line
1139 # of the property value does not end in end-of-line characters.
1141 $propertyValue .= "$eol$_";
1145 return ($propertyValue, $_);
1148 # Parse a patch file created by svn-create-patch.
1151 # $fileHandle: A file handle to the patch file that has not yet been
1155 # @diffHashRefs: an array of diff hash references.
1156 # See the %diffHash documentation above.
1159 my ($fileHandle) = @_;
1161 my $newDiffHashRefs;
1162 my @diffHashRefs; # return value
1164 my $line = <$fileHandle>;
1166 while (defined($line)) { # Otherwise, at EOF.
1168 ($newDiffHashRefs, $line) = parseDiff($fileHandle, $line);
1170 push @diffHashRefs, @$newDiffHashRefs;
1173 return @diffHashRefs;
1176 # Prepare the results of parsePatch() for use in svn-apply and svn-unapply.
1179 # $shouldForce: Whether to continue processing if an unexpected
1181 # @diffHashRefs: An array of references to %diffHashes.
1182 # See the %diffHash documentation above.
1184 # Returns $preparedPatchHashRef:
1185 # copyDiffHashRefs: A reference to an array of the $diffHashRefs in
1186 # @diffHashRefs that represent file copies. The original
1187 # ordering is preserved.
1188 # nonCopyDiffHashRefs: A reference to an array of the $diffHashRefs in
1189 # @diffHashRefs that do not represent file copies.
1190 # The original ordering is preserved.
1191 # sourceRevisionHash: A reference to a hash of source path to source
1193 sub prepareParsedPatch($@)
1195 my ($shouldForce, @diffHashRefs) = @_;
1200 my @copyDiffHashRefs = ();
1201 my @nonCopyDiffHashRefs = ();
1202 my %sourceRevisionHash = ();
1203 for my $diffHashRef (@diffHashRefs) {
1204 my $copiedFromPath = $diffHashRef->{copiedFromPath};
1205 my $indexPath = $diffHashRef->{indexPath};
1206 my $sourceRevision = $diffHashRef->{sourceRevision};
1209 if (defined($copiedFromPath)) {
1210 # Then the diff is a copy operation.
1211 $sourcePath = $copiedFromPath;
1213 # FIXME: Consider printing a warning or exiting if
1214 # exists($copiedFiles{$indexPath}) is true -- i.e. if
1215 # $indexPath appears twice as a copy target.
1216 $copiedFiles{$indexPath} = $sourcePath;
1218 push @copyDiffHashRefs, $diffHashRef;
1220 # Then the diff is not a copy operation.
1221 $sourcePath = $indexPath;
1223 push @nonCopyDiffHashRefs, $diffHashRef;
1226 if (defined($sourceRevision)) {
1227 if (exists($sourceRevisionHash{$sourcePath}) &&
1228 ($sourceRevisionHash{$sourcePath} != $sourceRevision)) {
1229 if (!$shouldForce) {
1230 die "Two revisions of the same file required as a source:\n".
1231 " $sourcePath:$sourceRevisionHash{$sourcePath}\n".
1232 " $sourcePath:$sourceRevision";
1235 $sourceRevisionHash{$sourcePath} = $sourceRevision;
1239 my %preparedPatchHash;
1241 $preparedPatchHash{copyDiffHashRefs} = \@copyDiffHashRefs;
1242 $preparedPatchHash{nonCopyDiffHashRefs} = \@nonCopyDiffHashRefs;
1243 $preparedPatchHash{sourceRevisionHash} = \%sourceRevisionHash;
1245 return \%preparedPatchHash;
1248 # Return localtime() for the project's time zone, given an integer time as
1249 # returned by Perl's time() function.
1250 sub localTimeInProjectTimeZone($)
1252 my $epochTime = shift;
1254 # Change the time zone temporarily for the localtime() call.
1255 my $savedTimeZone = $ENV{'TZ'};
1256 $ENV{'TZ'} = $changeLogTimeZone;
1257 my @localTime = localtime($epochTime);
1258 if (defined $savedTimeZone) {
1259 $ENV{'TZ'} = $savedTimeZone;
1267 # Set the reviewer and date in a ChangeLog patch, and return the new patch.
1270 # $patch: a ChangeLog patch as a string.
1271 # $reviewer: the name of the reviewer, or undef if the reviewer should not be set.
1272 # $epochTime: an integer time as returned by Perl's time() function.
1273 sub setChangeLogDateAndReviewer($$$)
1275 my ($patch, $reviewer, $epochTime) = @_;
1277 my @localTime = localTimeInProjectTimeZone($epochTime);
1278 my $newDate = strftime("%Y-%m-%d", @localTime);
1280 my $firstChangeLogLineRegEx = qr#(\n\+)\d{4}-[^-]{2}-[^-]{2}( )#;
1281 $patch =~ s/$firstChangeLogLineRegEx/$1$newDate$2/;
1283 if (defined($reviewer)) {
1284 # We include a leading plus ("+") in the regular expression to make
1285 # the regular expression less likely to match text in the leading junk
1286 # for the patch, if the patch has leading junk.
1287 $patch =~ s/(\n\+.*)NOBODY \(OOPS!\)/$1$reviewer/;
1293 # If possible, returns a ChangeLog patch equivalent to the given one,
1294 # but with the newest ChangeLog entry inserted at the top of the
1295 # file -- i.e. no leading context and all lines starting with "+".
1297 # If given a patch string not representable as a patch with the above
1298 # properties, it returns the input back unchanged.
1300 # WARNING: This subroutine can return an inequivalent patch string if
1301 # both the beginning of the new ChangeLog file matches the beginning
1302 # of the source ChangeLog, and the source beginning was modified.
1303 # Otherwise, it is guaranteed to return an equivalent patch string,
1306 # Applying this subroutine to ChangeLog patches allows svn-apply to
1307 # insert new ChangeLog entries at the top of the ChangeLog file.
1308 # svn-apply uses patch with --fuzz=3 to do this. We need to apply
1309 # this subroutine because the diff(1) command is greedy when matching
1310 # lines. A new ChangeLog entry with the same date and author as the
1311 # previous will match and cause the diff to have lines of starting
1314 # This subroutine has unit tests in VCSUtils_unittest.pl.
1316 # Returns $changeLogHashRef:
1317 # $changeLogHashRef: a hash reference representing a change log patch.
1318 # patch: a ChangeLog patch equivalent to the given one, but with the
1319 # newest ChangeLog entry inserted at the top of the file, if possible.
1320 sub fixChangeLogPatch($)
1322 my $patch = shift; # $patch will only contain patch fragments for ChangeLog.
1324 $patch =~ /(\r?\n)/;
1325 my $lineEnding = $1;
1326 my @lines = split(/$lineEnding/, $patch);
1328 my $i = 0; # We reuse the same index throughout.
1330 # Skip to beginning of first chunk.
1331 for (; $i < @lines; ++$i) {
1332 if (substr($lines[$i], 0, 1) eq "@") {
1336 my $chunkStartIndex = ++$i;
1337 my %changeLogHashRef;
1339 # Optimization: do not process if new lines already begin the chunk.
1340 if (substr($lines[$i], 0, 1) eq "+") {
1341 $changeLogHashRef{patch} = $patch;
1342 return \%changeLogHashRef;
1345 # Skip to first line of newly added ChangeLog entry.
1346 # For example, +2009-06-03 Eric Seidel <eric@webkit.org>
1347 my $dateStartRegEx = '^\+(\d{4}-\d{2}-\d{2})' # leading "+" and date
1348 . '\s+(.+)\s+' # name
1349 . '<([^<>]+)>$'; # e-mail address
1351 for (; $i < @lines; ++$i) {
1352 my $line = $lines[$i];
1353 my $firstChar = substr($line, 0, 1);
1354 if ($line =~ /$dateStartRegEx/) {
1356 } elsif ($firstChar eq " " or $firstChar eq "+") {
1359 $changeLogHashRef{patch} = $patch; # Do not change if, for example, "-" or "@" found.
1360 return \%changeLogHashRef;
1363 $changeLogHashRef{patch} = $patch; # Do not change if date not found.
1364 return \%changeLogHashRef;
1366 my $dateStartIndex = $i;
1368 # Rewrite overlapping lines to lead with " ".
1369 my @overlappingLines = (); # These will include a leading "+".
1370 for (; $i < @lines; ++$i) {
1371 my $line = $lines[$i];
1372 if (substr($line, 0, 1) ne "+") {
1375 push(@overlappingLines, $line);
1376 $lines[$i] = " " . substr($line, 1);
1379 # Remove excess ending context, if necessary.
1380 my $shouldTrimContext = 1;
1381 for (; $i < @lines; ++$i) {
1382 my $firstChar = substr($lines[$i], 0, 1);
1383 if ($firstChar eq " ") {
1385 } elsif ($firstChar eq "@") {
1388 $shouldTrimContext = 0; # For example, if "+" or "-" encountered.
1391 my $deletedLineCount = 0;
1392 if ($shouldTrimContext) { # Also occurs if end of file reached.
1393 splice(@lines, $i - @overlappingLines, @overlappingLines);
1394 $deletedLineCount = @overlappingLines;
1397 # Work backwards, shifting overlapping lines towards front
1398 # while checking that patch stays equivalent.
1399 for ($i = $dateStartIndex - 1; @overlappingLines && $i >= $chunkStartIndex; --$i) {
1400 my $line = $lines[$i];
1401 if (substr($line, 0, 1) ne " ") {
1404 my $text = substr($line, 1);
1405 my $newLine = pop(@overlappingLines);
1406 if ($text ne substr($newLine, 1)) {
1407 $changeLogHashRef{patch} = $patch; # Unexpected difference.
1408 return \%changeLogHashRef;
1410 $lines[$i] = "+$text";
1413 # If @overlappingLines > 0, this is where we make use of the
1414 # assumption that the beginning of the source file was not modified.
1415 splice(@lines, $chunkStartIndex, 0, @overlappingLines);
1417 # Update the date start index as it may have changed after shifting
1418 # the overlapping lines towards the front.
1419 for ($i = $chunkStartIndex; $i < $dateStartIndex; ++$i) {
1420 $dateStartIndex = $i if $lines[$i] =~ /$dateStartRegEx/;
1422 splice(@lines, $chunkStartIndex, $dateStartIndex - $chunkStartIndex); # Remove context of later entry.
1423 $deletedLineCount += $dateStartIndex - $chunkStartIndex;
1425 # Update the initial chunk range.
1426 my $chunkRangeRegEx = '^\@\@ -(\d+),(\d+) \+\d+,(\d+) \@\@$'; # e.g. @@ -2,6 +2,18 @@
1427 if ($lines[$chunkStartIndex - 1] !~ /$chunkRangeRegEx/) {
1428 # FIXME: Handle errors differently from ChangeLog files that
1429 # are okay but should not be altered. That way we can find out
1430 # if improvements to the script ever become necessary.
1431 $changeLogHashRef{patch} = $patch; # Error: unexpected patch string format.
1432 return \%changeLogHashRef;
1434 my $oldSourceLineCount = $2;
1435 my $oldTargetLineCount = $3;
1437 my $sourceLineCount = $oldSourceLineCount + @overlappingLines - $deletedLineCount;
1438 my $targetLineCount = $oldTargetLineCount + @overlappingLines - $deletedLineCount;
1439 $lines[$chunkStartIndex - 1] = "@@ -1,$sourceLineCount +1,$targetLineCount @@";
1441 $changeLogHashRef{patch} = join($lineEnding, @lines) . "\n"; # patch(1) expects an extra trailing newline.
1442 return \%changeLogHashRef;
1445 # This is a supporting method for runPatchCommand.
1447 # Arg: the optional $args parameter passed to runPatchCommand (can be undefined).
1449 # Returns ($patchCommand, $isForcing).
1451 # This subroutine has unit tests in VCSUtils_unittest.pl.
1452 sub generatePatchCommand($)
1454 my ($passedArgsHashRef) = @_;
1456 my $argsHashRef = { # Defaults
1462 # Merges hash references. It's okay here if passed hash reference is undefined.
1463 @{$argsHashRef}{keys %{$passedArgsHashRef}} = values %{$passedArgsHashRef};
1465 my $ensureForce = $argsHashRef->{ensureForce};
1466 my $shouldReverse = $argsHashRef->{shouldReverse};
1467 my $options = $argsHashRef->{options};
1472 $options = [@{$options}]; # Copy to avoid side effects.
1476 if (grep /^--force$/, @{$options}) {
1478 } elsif ($ensureForce) {
1479 push @{$options}, "--force";
1483 if ($shouldReverse) { # No check: --reverse should never be passed explicitly.
1484 push @{$options}, "--reverse";
1487 @{$options} = sort(@{$options}); # For easier testing.
1489 my $patchCommand = join(" ", "patch -p0", @{$options});
1491 return ($patchCommand, $isForcing);
1494 # Apply the given patch using the patch(1) command.
1496 # On success, return the resulting exit status. Otherwise, exit with the
1497 # exit status. If "--force" is passed as an option, however, then never
1498 # exit and always return the exit status.
1501 # $patch: a patch string.
1502 # $repositoryRootPath: an absolute path to the repository root.
1503 # $pathRelativeToRoot: the path of the file to be patched, relative to the
1504 # repository root. This should normally be the path
1505 # found in the patch's "Index:" line. It is passed
1506 # explicitly rather than reparsed from the patch
1507 # string for optimization purposes.
1508 # This is used only for error reporting. The
1509 # patch command gleans the actual file to patch
1510 # from the patch string.
1511 # $args: a reference to a hash of optional arguments. The possible
1513 # ensureForce: whether to ensure --force is passed (defaults to 0).
1514 # shouldReverse: whether to pass --reverse (defaults to 0).
1515 # options: a reference to an array of options to pass to the
1516 # patch command. The subroutine passes the -p0 option
1517 # no matter what. This should not include --reverse.
1519 # This subroutine has unit tests in VCSUtils_unittest.pl.
1520 sub runPatchCommand($$$;$)
1522 my ($patch, $repositoryRootPath, $pathRelativeToRoot, $args) = @_;
1524 my ($patchCommand, $isForcing) = generatePatchCommand($args);
1526 # Temporarily change the working directory since the path found
1527 # in the patch's "Index:" line is relative to the repository root
1528 # (i.e. the same as $pathRelativeToRoot).
1529 my $cwd = Cwd::getcwd();
1530 chdir $repositoryRootPath;
1532 open PATCH, "| $patchCommand" or die "Could not call \"$patchCommand\" for file \"$pathRelativeToRoot\": $!";
1535 my $exitStatus = exitStatus($?);
1539 if ($exitStatus && !$isForcing) {
1540 print "Calling \"$patchCommand\" for file \"$pathRelativeToRoot\" returned " .
1541 "status $exitStatus. Pass --force to ignore patch failures.\n";
1548 # Merge ChangeLog patches using a three-file approach.
1550 # This is used by resolve-ChangeLogs when it's operated as a merge driver
1551 # and when it's used to merge conflicts after a patch is applied or after
1554 # It's also used for traditional rejected patches.
1557 # $fileMine: The merged version of the file. Also known in git as the
1558 # other branch's version (%B) or "ours".
1559 # For traditional patch rejects, this is the *.rej file.
1560 # $fileOlder: The base version of the file. Also known in git as the
1561 # ancestor version (%O) or "base".
1562 # For traditional patch rejects, this is the *.orig file.
1563 # $fileNewer: The current version of the file. Also known in git as the
1564 # current version (%A) or "theirs".
1565 # For traditional patch rejects, this is the original-named
1568 # Returns 1 if merge was successful, else 0.
1569 sub mergeChangeLogs($$$)
1571 my ($fileMine, $fileOlder, $fileNewer) = @_;
1573 my $traditionalReject = $fileMine =~ /\.rej$/ ? 1 : 0;
1578 if ($traditionalReject) {
1579 open(DIFF, "<", $fileMine) or die $!;
1582 rename($fileMine, "$fileMine.save");
1583 rename($fileOlder, "$fileOlder.save");
1585 open(DIFF, "-|", qw(diff -u -a --binary), $fileOlder, $fileMine) or die $!;
1590 unlink("${fileNewer}.orig");
1591 unlink("${fileNewer}.rej");
1593 open(PATCH, "| patch --force --fuzz=3 --binary $fileNewer > " . File::Spec->devnull()) or die $!;
1594 if ($traditionalReject) {
1597 my $changeLogHash = fixChangeLogPatch($patch);
1598 print PATCH $changeLogHash->{patch};
1602 my $result = !exitStatus($?);
1604 # Refuse to merge the patch if it did not apply cleanly
1605 if (-e "${fileNewer}.rej") {
1606 unlink("${fileNewer}.rej");
1607 if (-f "${fileNewer}.orig") {
1609 rename("${fileNewer}.orig", $fileNewer);
1612 unlink("${fileNewer}.orig");
1615 if ($traditionalReject) {
1616 rename("$fileMine.save", $fileMine);
1617 rename("$fileOlder.save", $fileOlder);
1625 return unless $isGit;
1629 my $result = `git config $config`;
1631 $result = `git repo-config $config`;
1637 sub changeLogNameError($)
1640 print STDERR "$message\nEither:\n";
1641 print STDERR " set CHANGE_LOG_NAME in your environment\n";
1642 print STDERR " OR pass --name= on the command line\n";
1643 print STDERR " OR set REAL_NAME in your environment";
1644 print STDERR " OR git users can set 'git config user.name'\n";
1650 my $name = $ENV{CHANGE_LOG_NAME} || $ENV{REAL_NAME} || gitConfig("user.name") || (split /\s*,\s*/, (getpwuid $<)[6])[0];
1652 changeLogNameError("Failed to determine ChangeLog name.") unless $name;
1653 # getpwuid seems to always succeed on windows, returning the username instead of the full name. This check will catch that case.
1654 changeLogNameError("'$name' does not contain a space! ChangeLogs should contain your full name.") unless ($name =~ /\w \w/);
1659 sub changeLogEmailAddressError($)
1662 print STDERR "$message\nEither:\n";
1663 print STDERR " set CHANGE_LOG_EMAIL_ADDRESS in your environment\n";
1664 print STDERR " OR pass --email= on the command line\n";
1665 print STDERR " OR set EMAIL_ADDRESS in your environment\n";
1666 print STDERR " OR git users can set 'git config user.email'\n";
1670 sub changeLogEmailAddress()
1672 my $emailAddress = $ENV{CHANGE_LOG_EMAIL_ADDRESS} || $ENV{EMAIL_ADDRESS} || gitConfig("user.email");
1674 changeLogEmailAddressError("Failed to determine email address for ChangeLog.") unless $emailAddress;
1675 changeLogEmailAddressError("Email address '$emailAddress' does not contain '\@' and is likely invalid.") unless ($emailAddress =~ /\@/);
1677 return $emailAddress;
1680 # http://tools.ietf.org/html/rfc1924
1685 my @characters = ('0'..'9', 'A'..'Z', 'a'..'z', '!', '#', '$', '%', '&', '(', ')', '*', '+', '-', ';', '<', '=', '>', '?', '@', '^', '_', '`', '{', '|', '}', '~');
1686 for (my $i = 0; $i < 85; $i++) {
1687 $table{$characters[$i]} = $i;
1691 my @encodedChars = $encoded =~ /./g;
1693 for (my $encodedIter = 0; defined($encodedChars[$encodedIter]);) {
1695 for (my $i = 0; $i < 5; $i++) {
1697 my $char = $encodedChars[$encodedIter];
1698 $digit += $table{$char};
1702 for (my $i = 0; $i < 4; $i++) {
1703 $decoded .= chr(($digit >> (3 - $i) * 8) & 255);
1710 sub decodeGitBinaryChunk($$)
1712 my ($contents, $fullPath) = @_;
1714 # Load this module lazily in case the user don't have this module
1715 # and won't handle git binary patches.
1716 require Compress::Zlib;
1719 my $compressedSize = 0;
1720 while ($contents =~ /^([A-Za-z])(.*)$/gm) {
1722 next if $line eq "";
1723 die "$fullPath: unexpected size of a line: $&" if length($2) % 5 != 0;
1724 my $actualSize = length($2) / 5 * 4;
1725 my $encodedExpectedSize = ord($1);
1726 my $expectedSize = $encodedExpectedSize <= ord("Z") ? $encodedExpectedSize - ord("A") + 1 : $encodedExpectedSize - ord("a") + 27;
1728 die "$fullPath: unexpected size of a line: $&" if int(($expectedSize + 3) / 4) * 4 != $actualSize;
1729 $compressedSize += $expectedSize;
1733 my $compressed = decodeBase85($encoded);
1734 $compressed = substr($compressed, 0, $compressedSize);
1735 return Compress::Zlib::uncompress($compressed);
1738 sub decodeGitBinaryPatch($$)
1740 my ($contents, $fullPath) = @_;
1742 # Git binary patch has two chunks. One is for the normal patching
1743 # and another is for the reverse patching.
1745 # Each chunk a line which starts from either "literal" or "delta",
1746 # followed by a number which specifies decoded size of the chunk.
1748 # Then, content of the chunk comes. To decode the content, we
1749 # need decode it with base85 first, and then zlib.
1750 my $gitPatchRegExp = '(literal|delta) ([0-9]+)\n([A-Za-z0-9!#$%&()*+-;<=>?@^_`{|}~\\n]*?)\n\n';
1751 if ($contents !~ m"\nGIT binary patch\n$gitPatchRegExp$gitPatchRegExp\Z") {
1752 die "$fullPath: unknown git binary patch format"
1755 my $binaryChunkType = $1;
1756 my $binaryChunkExpectedSize = $2;
1757 my $encodedChunk = $3;
1758 my $reverseBinaryChunkType = $4;
1759 my $reverseBinaryChunkExpectedSize = $5;
1760 my $encodedReverseChunk = $6;
1762 my $binaryChunk = decodeGitBinaryChunk($encodedChunk, $fullPath);
1763 my $binaryChunkActualSize = length($binaryChunk);
1764 my $reverseBinaryChunk = decodeGitBinaryChunk($encodedReverseChunk, $fullPath);
1765 my $reverseBinaryChunkActualSize = length($reverseBinaryChunk);
1767 die "$fullPath: unexpected size of the first chunk (expected $binaryChunkExpectedSize but was $binaryChunkActualSize" if ($binaryChunkType eq "literal" and $binaryChunkExpectedSize != $binaryChunkActualSize);
1768 die "$fullPath: unexpected size of the second chunk (expected $reverseBinaryChunkExpectedSize but was $reverseBinaryChunkActualSize" if ($reverseBinaryChunkType eq "literal" and $reverseBinaryChunkExpectedSize != $reverseBinaryChunkActualSize);
1770 return ($binaryChunkType, $binaryChunk, $reverseBinaryChunkType, $reverseBinaryChunk);
1775 my ($data, $location) = @_;
1777 # Return the byte at $location in $data as a numeric value.
1778 return ord(substr($data, $location, 1));
1781 # The git binary delta format is undocumented, except in code:
1782 # - https://github.com/git/git/blob/master/delta.h:get_delta_hdr_size is the source
1783 # of the algorithm in decodeGitBinaryPatchDeltaSize.
1784 # - https://github.com/git/git/blob/master/patch-delta.c:patch_delta is the source
1785 # of the algorithm in applyGitBinaryPatchDelta.
1786 sub decodeGitBinaryPatchDeltaSize($)
1788 my ($binaryChunk) = @_;
1790 # Source and destination buffer sizes are stored in 7-bit chunks at the
1791 # start of the binary delta patch data. The highest bit in each byte
1792 # except the last is set; the remaining 7 bits provide the next
1793 # chunk of the size. The chunks are stored in ascending significance
1798 for (my $i = 0; $i < length($binaryChunk);) {
1799 $cmd = readByte($binaryChunk, $i++);
1800 $size |= ($cmd & 0x7f) << $shift;
1802 if (!($cmd & 0x80)) {
1808 sub applyGitBinaryPatchDelta($$)
1810 my ($binaryChunk, $originalContents) = @_;
1812 # Git delta format consists of two headers indicating source buffer size
1813 # and result size, then a series of commands. Each command is either
1814 # a copy-from-old-version (the 0x80 bit is set) or a copy-from-delta
1815 # command. Commands are applied sequentially to generate the result.
1817 # A copy-from-old-version command encodes an offset and size to copy
1818 # from in subsequent bits, while a copy-from-delta command consists only
1819 # of the number of bytes to copy from the delta.
1821 # We don't use these values, but we need to know how big they are so that
1822 # we can skip to the diff data.
1823 my ($size, $bytesUsed) = decodeGitBinaryPatchDeltaSize($binaryChunk);
1824 $binaryChunk = substr($binaryChunk, $bytesUsed);
1825 ($size, $bytesUsed) = decodeGitBinaryPatchDeltaSize($binaryChunk);
1826 $binaryChunk = substr($binaryChunk, $bytesUsed);
1829 for (my $i = 0; $i < length($binaryChunk); ) {
1830 my $cmd = ord(substr($binaryChunk, $i++, 1));
1832 # Extract an offset and size from the delta data, then copy
1833 # $size bytes from $offset in the original data into the output.
1836 if ($cmd & 0x01) { $offset = readByte($binaryChunk, $i++); }
1837 if ($cmd & 0x02) { $offset |= readByte($binaryChunk, $i++) << 8; }
1838 if ($cmd & 0x04) { $offset |= readByte($binaryChunk, $i++) << 16; }
1839 if ($cmd & 0x08) { $offset |= readByte($binaryChunk, $i++) << 24; }
1840 if ($cmd & 0x10) { $size = readByte($binaryChunk, $i++); }
1841 if ($cmd & 0x20) { $size |= readByte($binaryChunk, $i++) << 8; }
1842 if ($cmd & 0x40) { $size |= readByte($binaryChunk, $i++) << 16; }
1843 if ($size == 0) { $size = 0x10000; }
1844 $out .= substr($originalContents, $offset, $size);
1846 # Copy $cmd bytes from the delta data into the output.
1847 $out .= substr($binaryChunk, $i, $cmd);
1850 die "unexpected delta opcode 0";