Skip to content

Commit

Permalink
Merge pull request cweagans#175 from danepowell/issue-174
Browse files Browse the repository at this point in the history
Fixes cweagans#174: Compatibility with older Git versions.

Modifying source to promet/composer-patches, adding a var_dump(); die
  • Loading branch information
cweagans authored and Lisa Ridley committed Apr 6, 2018
2 parents 730f0f6 + c14b6c3 commit 35b561b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 34 deletions.
14 changes: 9 additions & 5 deletions composer.json
@@ -1,16 +1,20 @@
{
"name": "cweagans/composer-patches",
"description": "Provides a way to patch Composer packages.",
"name": "promet/composer-patches",
"description": "Provides a newer way to patch Composer packages.",
"minimum-stability": "dev",
"license": "BSD-2-Clause",
"type": "composer-plugin",
"extra": {
"class": "cweagans\\Composer\\Patches"
"class": "promet\\Composer\\Patches"
},
"authors": [
{
"name": "Cameron Eagans",
"email": "me@cweagans.net"
},
{
"name": "Lisa Ridley",
"email": "lisa@prometsource.com"
}
],
"require": {
Expand All @@ -22,9 +26,9 @@
"phpunit/phpunit": "~4.6"
},
"autoload": {
"psr-4": {"cweagans\\Composer\\": "src"}
"psr-4": {"promet\\Composer\\": "src"}
},
"autoload-dev": {
"psr-4": {"cweagans\\Composer\\Tests\\": "tests"}
"psr-4": {"promet\\Composer\\Tests\\": "tests"}
}
}
2 changes: 1 addition & 1 deletion src/PatchEvent.php
Expand Up @@ -5,7 +5,7 @@
* Dispatch events when patches are applied.
*/

namespace cweagans\Composer;
namespace promet\Composer;

use Composer\EventDispatcher\Event;
use Composer\Package\PackageInterface;
Expand Down
6 changes: 3 additions & 3 deletions src/PatchEvents.php
Expand Up @@ -5,14 +5,14 @@
* Dispatch events when patches are applied.
*/

namespace cweagans\Composer;
namespace promet\Composer;

class PatchEvents {

/**
* The PRE_PATCH_APPLY event occurs before a patch is applied.
*
* The event listener method receives a cweagans\Composer\PatchEvent instance.
* The event listener method receives a promet\Composer\PatchEvent instance.
*
* @var string
*/
Expand All @@ -21,7 +21,7 @@ class PatchEvents {
/**
* The POST_PATCH_APPLY event occurs after a patch is applied.
*
* The event listener method receives a cweagans\Composer\PatchEvent instance.
* The event listener method receives a promet\Composer\PatchEvent instance.
*
* @var string
*/
Expand Down
72 changes: 47 additions & 25 deletions src/Patches.php
Expand Up @@ -5,7 +5,7 @@
* Provides a way to patch Composer packages after installation.
*/

namespace cweagans\Composer;
namespace promet\Composer;

use Composer\Composer;
use Composer\DependencyResolver\Operation\InstallOperation;
Expand Down Expand Up @@ -97,7 +97,7 @@ public function checkPatches(Event $event) {
$localRepository = $repositoryManager->getLocalRepository();
$installationManager = $this->composer->getInstallationManager();
$packages = $localRepository->getPackages();

var_dump(array('packages' => $packages));
$tmp_patches = $this->grabPatches();
foreach ($packages as $package) {
$extra = $package->getExtra();
Expand Down Expand Up @@ -361,32 +361,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 = array('-p1', '-p0', '-p2', '-p4');
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 @@ -437,7 +418,7 @@ protected function isPatchingEnabled() {
* @param string $directory
*/
protected function writePatchReport($patches, $directory) {
$output = "This file was automatically generated by Composer Patches (https://github.com/cweagans/composer-patches)\n";
$output = "This file was automatically generated by Composer Patches (https://github.com/promet/composer-patches)\n";
$output .= "Patches applied to this directory:\n\n";
foreach ($patches as $description => $url) {
$output .= $description . "\n";
Expand Down Expand Up @@ -510,4 +491,45 @@ protected function arrayMergeRecursiveDistinct(array $array1, array $array2) {
return $merged;
}

/**
* 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;
}

}

0 comments on commit 35b561b

Please sign in to comment.