Skip to content

Commit 3dafd72

Browse files
committed
Merge branch 'issue-174' into develop-new
2 parents b01a0ec + 1f12644 commit 3dafd72

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

src/Plugin/Patches.php

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -425,42 +425,13 @@ protected function getAndApplyPatch(RemoteFilesystem $downloader, $install_path,
425425
}
426426
}
427427

428-
// Modified from drush6:make.project.inc
429-
$patched = false;
430428
// The order here is intentional. p1 is most likely to apply with git apply.
431429
// p0 is next likely. p2 is extremely unlikely, but for some special cases,
432430
// it might be useful. p4 is useful for Magento 2 patches
433431
$patch_levels = $this->getConfig('patch-levels');
434-
foreach ($patch_levels as $patch_level) {
435-
if ($this->io->isVerbose()) {
436-
$comment = 'Testing ability to patch with git apply.';
437-
$comment .= ' This command may produce errors that can be safely ignored.';
438-
$this->io->write('<comment>' . $comment . '</comment>');
439-
}
440-
$checked = $this->executeCommand(
441-
'git -C %s apply --check -v %s %s',
442-
$install_path,
443-
$patch_level,
444-
$filename
445-
);
446-
$output = $this->executor->getErrorOutput();
447-
if (substr($output, 0, 7) === 'Skipped') {
448-
// Git will indicate success but silently skip patches in some scenarios.
449-
//
450-
// @see https://github.com/cweagans/composer-patches/pull/165
451-
$checked = false;
452-
}
453-
if ($checked) {
454-
// Apply the first successful style.
455-
$patched = $this->executeCommand(
456-
'git -C %s apply %s %s',
457-
$install_path,
458-
$patch_level,
459-
$filename
460-
);
461-
break;
462-
}
463-
}
432+
433+
// Attempt to apply with git apply
434+
$patched = $this->applyPatchWithGit($install_path, $patch_levels, $filename);
464435

465436
// In some rare cases, git will fail to apply a patch, fallback to using
466437
// the 'patch' command.
@@ -519,6 +490,58 @@ protected function isPatchingEnabled()
519490
return $enabled;
520491
}
521492

493+
/**
494+
* Attempts to apply a patch with git apply
495+
*
496+
* @param $install_path
497+
* @param $patch_levels
498+
* @param $filename
499+
*
500+
* @return bool
501+
* TRUE if patch was applied, FALSE otherwise.
502+
*/
503+
protected function applyPatchWithGit($install_path, $patch_levels, $filename)
504+
{
505+
// Do not use git apply unless the install path is itself a git repo
506+
// @see https://stackoverflow.com/a/27283285
507+
if (!is_dir($install_path . '/.git')) {
508+
return false;
509+
}
510+
511+
$patched = false;
512+
foreach ($patch_levels as $patch_level) {
513+
if ($this->io->isVerbose()) {
514+
$comment = 'Testing ability to patch with git apply.';
515+
$comment .= ' This command may produce errors that can be safely ignored.';
516+
$this->io->write('<comment>' . $comment . '</comment>');
517+
}
518+
$checked = $this->executeCommand(
519+
'git -C %s apply --check -v %s %s',
520+
$install_path,
521+
$patch_level,
522+
$filename
523+
);
524+
$output = $this->executor->getErrorOutput();
525+
if (substr($output, 0, 7) === 'Skipped') {
526+
// Git will indicate success but silently skip patches in some scenarios.
527+
//
528+
// @see https://github.com/cweagans/composer-patches/pull/165
529+
$checked = false;
530+
}
531+
if ($checked) {
532+
// Apply the first successful style.
533+
$patched = $this->executeCommand(
534+
'git -C %s apply %s %s',
535+
$install_path,
536+
$patch_level,
537+
$filename
538+
);
539+
break;
540+
}
541+
}
542+
return $patched;
543+
}
544+
522545
/**
523546
* Executes a shell command with escaping.
524547
*

0 commit comments

Comments
 (0)