Skip to content

Commit

Permalink
Merge branch 'pr-227' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
stucki committed Aug 9, 2018
2 parents 04a534a + 1f12644 commit 5d43604
Showing 1 changed file with 55 additions and 32 deletions.
87 changes: 55 additions & 32 deletions src/Plugin/Patches.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>' . $comment . '</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.
Expand Down Expand Up @@ -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>' . $comment . '</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.
*
Expand Down

0 comments on commit 5d43604

Please sign in to comment.