From 8fec601b6a209a44f8981c5f6ff2046ae1b6c2c7 Mon Sep 17 00:00:00 2001 From: Michael Stucki Date: Fri, 13 Jul 2018 12:59:15 +0200 Subject: [PATCH 1/3] Refactor: extract git apply functionality --- src/Plugin/Patches.php | 78 ++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 30 deletions(-) diff --git a/src/Plugin/Patches.php b/src/Plugin/Patches.php index 389431ee..de98a99c 100644 --- a/src/Plugin/Patches.php +++ b/src/Plugin/Patches.php @@ -391,36 +391,9 @@ protected function getAndApplyPatch(RemoteFilesystem $downloader, $install_path, // 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. @@ -472,6 +445,51 @@ 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) + { + 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. * From eecde1ab980a8435def8fc92f9f8b40b9f013dc0 Mon Sep 17 00:00:00 2001 From: Michael Stucki Date: Fri, 13 Jul 2018 13:01:45 +0200 Subject: [PATCH 2/3] Fixes #174: Compatibility with older Git versions --- src/Plugin/Patches.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Plugin/Patches.php b/src/Plugin/Patches.php index de98a99c..249ec7e0 100644 --- a/src/Plugin/Patches.php +++ b/src/Plugin/Patches.php @@ -457,6 +457,12 @@ protected function isPatchingEnabled() */ 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; + } + foreach ($patch_levels as $patch_level) { if ($this->io->isVerbose()) { $comment = 'Testing ability to patch with git apply.'; From 1f126447f3a10308000195eebf3b9271d8a58eb3 Mon Sep 17 00:00:00 2001 From: Michael Stucki Date: Thu, 9 Aug 2018 15:32:05 +0200 Subject: [PATCH 3/3] Fix undefined variable --- src/Plugin/Patches.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Plugin/Patches.php b/src/Plugin/Patches.php index 249ec7e0..ed467e74 100644 --- a/src/Plugin/Patches.php +++ b/src/Plugin/Patches.php @@ -385,8 +385,6 @@ 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 @@ -463,6 +461,7 @@ protected function applyPatchWithGit($install_path, $patch_levels, $filename) return false; } + $patched = false; foreach ($patch_levels as $patch_level) { if ($this->io->isVerbose()) { $comment = 'Testing ability to patch with git apply.';