Skip to content

Commit 462e650

Browse files
authored
Merge pull request #175 from danepowell/issue-174
Fixes #174: Compatibility with older Git versions.
2 parents 730f0f6 + c14b6c3 commit 462e650

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

src/Patches.php

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -361,32 +361,13 @@ protected function getAndApplyPatch(RemoteFilesystem $downloader, $install_path,
361361
$downloader->copy($hostname, $patch_url, $filename, FALSE);
362362
}
363363

364-
// Modified from drush6:make.project.inc
365-
$patched = FALSE;
366364
// The order here is intentional. p1 is most likely to apply with git apply.
367365
// p0 is next likely. p2 is extremely unlikely, but for some special cases,
368366
// it might be useful. p4 is useful for Magento 2 patches
369367
$patch_levels = array('-p1', '-p0', '-p2', '-p4');
370-
foreach ($patch_levels as $patch_level) {
371-
if ($this->io->isVerbose()) {
372-
$comment = 'Testing ability to patch with git apply.';
373-
$comment .= ' This command may produce errors that can be safely ignored.';
374-
$this->io->write('<comment>' . $comment . '</comment>');
375-
}
376-
$checked = $this->executeCommand('git -C %s apply --check -v %s %s', $install_path, $patch_level, $filename);
377-
$output = $this->executor->getErrorOutput();
378-
if (substr($output, 0, 7) == 'Skipped') {
379-
// Git will indicate success but silently skip patches in some scenarios.
380-
//
381-
// @see https://github.com/cweagans/composer-patches/pull/165
382-
$checked = false;
383-
}
384-
if ($checked) {
385-
// Apply the first successful style.
386-
$patched = $this->executeCommand('git -C %s apply %s %s', $install_path, $patch_level, $filename);
387-
break;
388-
}
389-
}
368+
369+
// Attempt to apply with git apply.
370+
$patched = $this->applyPatchWithGit($install_path, $patch_levels, $filename);
390371

391372
// In some rare cases, git will fail to apply a patch, fallback to using
392373
// the 'patch' command.
@@ -510,4 +491,45 @@ protected function arrayMergeRecursiveDistinct(array $array1, array $array2) {
510491
return $merged;
511492
}
512493

494+
/**
495+
* Attempts to apply a patch with git apply.
496+
*
497+
* @param $install_path
498+
* @param $patch_levels
499+
* @param $filename
500+
*
501+
* @return bool
502+
* TRUE if patch was applied, FALSE otherwise.
503+
*/
504+
protected function applyPatchWithGit($install_path, $patch_levels, $filename) {
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('git -C %s apply --check -v %s %s', $install_path, $patch_level, $filename);
519+
$output = $this->executor->getErrorOutput();
520+
if (substr($output, 0, 7) == 'Skipped') {
521+
// Git will indicate success but silently skip patches in some scenarios.
522+
//
523+
// @see https://github.com/cweagans/composer-patches/pull/165
524+
$checked = FALSE;
525+
}
526+
if ($checked) {
527+
// Apply the first successful style.
528+
$patched = $this->executeCommand('git -C %s apply %s %s', $install_path, $patch_level, $filename);
529+
break;
530+
}
531+
}
532+
return $patched;
533+
}
534+
513535
}

0 commit comments

Comments
 (0)