diff --git a/src/Plugin/Patches.php b/src/Plugin/Patches.php index f636403e..2daba2c5 100644 --- a/src/Plugin/Patches.php +++ b/src/Plugin/Patches.php @@ -413,42 +413,13 @@ protected function getAndApplyPatch(RemoteFilesystem $downloader, $install_path, $downloader->copy($hostname, $patch_url, $filename, false); } - // Modified from drush6:make.project.inc - $patched = false; // The order here is intentional. p1 is most likely to apply with git apply. // p0 is next likely. p2 is extremely unlikely, but for some special cases, // it might be useful. p4 is useful for Magento 2 patches $patch_levels = $this->getConfig('patch-levels'); - foreach ($patch_levels as $patch_level) { - if ($this->io->isVerbose()) { - $comment = 'Testing ability to patch with git apply.'; - $comment .= ' This command may produce errors that can be safely ignored.'; - $this->io->write('' . $comment . ''); - } - $checked = $this->executeCommand( - 'git -C %s apply --check -v %s %s', - $install_path, - $patch_level, - $filename - ); - $output = $this->executor->getErrorOutput(); - if (substr($output, 0, 7) == 'Skipped') { - // Git will indicate success but silently skip patches in some scenarios. - // - // @see https://github.com/cweagans/composer-patches/pull/165 - $checked = false; - } - if ($checked) { - // Apply the first successful style. - $patched = $this->executeCommand( - 'git -C %s apply %s %s', - $install_path, - $patch_level, - $filename - ); - break; - } - } + + // Attempt to apply with git apply + $patched = $this->applyPatchWithGit($install_path, $patch_levels, $filename); // In some rare cases, git will fail to apply a patch, fallback to using // the 'patch' command. @@ -500,6 +471,58 @@ protected function isPatchingEnabled() return $enabled; } + /** + * Attempts to apply a patch with git apply + * + * @param $install_path + * @param $patch_levels + * @param $filename + * + * @return bool + * TRUE if patch was applied, FALSE otherwise. + */ + protected function applyPatchWithGit($install_path, $patch_levels, $filename) + { + // Do not use git apply unless the install path is itself a git repo + // @see https://stackoverflow.com/a/27283285 + if (!is_dir($install_path . '/.git')) { + return false; + } + + $patched = false; + foreach ($patch_levels as $patch_level) { + if ($this->io->isVerbose()) { + $comment = 'Testing ability to patch with git apply.'; + $comment .= ' This command may produce errors that can be safely ignored.'; + $this->io->write('' . $comment . ''); + } + $checked = $this->executeCommand( + 'git -C %s apply --check -v %s %s', + $install_path, + $patch_level, + $filename + ); + $output = $this->executor->getErrorOutput(); + if (substr($output, 0, 7) == 'Skipped') { + // Git will indicate success but silently skip patches in some scenarios. + // + // @see https://github.com/cweagans/composer-patches/pull/165 + $checked = false; + } + if ($checked) { + // Apply the first successful style. + $patched = $this->executeCommand( + 'git -C %s apply %s %s', + $install_path, + $patch_level, + $filename + ); + break; + } + } + return $patched; + } + /** * Executes a shell command with escaping. *