Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mc 37324: Add fallback to 'patch' command when 'git' command is not available #2

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
<service id="Magento\CloudPatches\Patch\Status\StatusResolverException" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchIntegrityException" autowire="false"/>
<service id="Magento\CloudPatches\Patch\Pool\PatchNotFoundException" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommandNotFound" autowire="false"/>
<service id="Magento\CloudPatches\Patch\PatchCommandException" autowire="false"/>
<service id="Magento\CloudPatches\Patch\ApplierException" autowire="false"/>
<service id="Magento\CloudPatches\Shell\PackageNotFoundException" autowire="false"/>
<service id="Magento\CloudPatches\Patch\Data\Patch" autowire="false"/>
Expand Down Expand Up @@ -76,5 +78,14 @@
<argument key="$actionPool" type="service" id="ApplyOptionalActionPool"/>
</service>
<service id="Magento\CloudPatches\Patch\PatchBuilder" shared="false"/>
<service id="patchCommand" class="Magento\CloudPatches\Patch\PatchCommand">
<argument key="$drivers" type="collection">
<argument type="service" id="Magento\CloudPatches\Shell\Command\GitDriver"/>
<argument type="service" id="Magento\CloudPatches\Shell\Command\PatchDriver"/>
</argument>
</service>
<service id="Magento\CloudPatches\Patch\Applier">
<argument key="$patchCommand" type="service" id="patchCommand"/>
</service>
</services>
</container>
47 changes: 19 additions & 28 deletions src/Patch/Applier.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,16 @@
use Magento\CloudPatches\Composer\MagentoVersion;
use Magento\CloudPatches\Filesystem\Filesystem;
use Magento\CloudPatches\Patch\Status\StatusPool;
use Magento\CloudPatches\Shell\ProcessFactory;
use Symfony\Component\Process\Exception\ProcessFailedException;

/**
* Applies and reverts patches.
*/
class Applier
{
/**
* @var ProcessFactory
* @var PatchCommandInterface
*/
private $processFactory;
private $patchCommand;

/**
* @var GitConverter
Expand All @@ -39,18 +37,18 @@ class Applier
private $filesystem;

/**
* @param ProcessFactory $processFactory
* @param PatchCommandInterface $patchCommand
* @param GitConverter $gitConverter
* @param MagentoVersion $magentoVersion
* @param Filesystem $filesystem
*/
public function __construct(
ProcessFactory $processFactory,
PatchCommandInterface $patchCommand,
GitConverter $gitConverter,
MagentoVersion $magentoVersion,
Filesystem $filesystem
) {
$this->processFactory = $processFactory;
$this->patchCommand = $patchCommand;
$this->gitConverter = $gitConverter;
$this->magentoVersion = $magentoVersion;
$this->filesystem = $filesystem;
Expand All @@ -69,13 +67,11 @@ public function apply(string $path, string $id): string
{
$content = $this->readContent($path);
try {
$this->processFactory->create(['git', 'apply'], $content)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->apply($content);
} catch (PatchCommandException $exception) {
try {
$this->processFactory->create(['git', 'apply', '--check', '--reverse'], $content)
->mustRun();
} catch (ProcessFailedException $reverseException) {
$this->patchCommand->revertCheck($content);
} catch (PatchCommandException $reverseException) {
throw new ApplierException($exception->getMessage(), $exception->getCode());
}

Expand All @@ -98,13 +94,11 @@ public function revert(string $path, string $id): string
{
$content = $this->readContent($path);
try {
$this->processFactory->create(['git', 'apply', '--reverse'], $content)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->revert($content);
} catch (PatchCommandException $exception) {
try {
$this->processFactory->create(['git', 'apply', '--check'], $content)
->mustRun();
} catch (ProcessFailedException $applyException) {
$this->patchCommand->applyCheck($content);
} catch (PatchCommandException $applyException) {
throw new ApplierException($exception->getMessage(), $exception->getCode());
}

Expand All @@ -124,13 +118,11 @@ public function status(string $patchContent): string
{
$patchContent = $this->prepareContent($patchContent);
try {
$this->processFactory->create(['git', 'apply', '--check'], $patchContent)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->applyCheck($patchContent);
} catch (PatchCommandException $exception) {
try {
$this->processFactory->create(['git', 'apply', '--check', '--reverse'], $patchContent)
->mustRun();
} catch (ProcessFailedException $reverseException) {
$this->patchCommand->revertCheck($patchContent);
} catch (PatchCommandException $reverseException) {
return StatusPool::NA;
}

Expand All @@ -150,9 +142,8 @@ public function checkApply(string $patchContent): bool
{
$patchContent = $this->prepareContent($patchContent);
try {
$this->processFactory->create(['git', 'apply', '--check'], $patchContent)
->mustRun();
} catch (ProcessFailedException $exception) {
$this->patchCommand->applyCheck($patchContent);
} catch (PatchCommandException $exception) {
return false;
}

Expand Down
89 changes: 89 additions & 0 deletions src/Patch/PatchCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

use Magento\CloudPatches\Shell\Command\DriverInterface;

/**
* Patch command selector
*/
class PatchCommand implements PatchCommandInterface
{
/**
* @var DriverInterface[]
*/
private $drivers;

/**
* @var DriverInterface
*/
private $driver;

/**
* @param DriverInterface[] $drivers
*/
public function __construct(
array $drivers
) {
$this->drivers = $drivers;
}

/**
* @inheritDoc
*/
public function apply(string $patch)
{
$this->getDriver()->apply($patch);
}

/**
* @inheritDoc
*/
public function revert(string $patch)
{
$this->getDriver()->revert($patch);
}

/**
* @inheritDoc
*/
public function applyCheck(string $patch)
{
$this->getDriver()->applyCheck($patch);
}

/**
* @inheritDoc
*/
public function revertCheck(string $patch)
{
$this->getDriver()->revertCheck($patch);
}

/**
* Returns first available driver
*
* @return DriverInterface
* @throws PatchCommandNotFound
*/
private function getDriver(): DriverInterface
{
if ($this->driver === null) {
foreach ($this->drivers as $driver) {
if ($driver->isInstalled()) {
$this->driver = $driver;
break;
}
}
if ($this->driver === null) {
throw new PatchCommandNotFound();
}
}
return $this->driver;
}
}
17 changes: 17 additions & 0 deletions src/Patch/PatchCommandException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

use Magento\CloudPatches\App\GenericException;

/**
* Generic patch command exception
*/
class PatchCommandException extends GenericException
{
}
50 changes: 50 additions & 0 deletions src/Patch/PatchCommandInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

/**
* Patch command interface
*/
interface PatchCommandInterface
{
/**
* Applies patch
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function apply(string $patch);

/**
* Reverts patch
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function revert(string $patch);

/**
* Checks if patch can be applied.
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function applyCheck(string $patch);

/**
* Checks if patch can be reverted
*
* @param string $patch
* @return void
* @throws PatchCommandException
*/
public function revertCheck(string $patch);
}
21 changes: 21 additions & 0 deletions src/Patch/PatchCommandNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Patch;

use Magento\CloudPatches\App\GenericException;

/**
* Exception thrown if none of defined patch drivers is available
*/
class PatchCommandNotFound extends GenericException
{
public function __construct()
{
parent::__construct('GIT or PATCH is required to perform this operation.');
}
}
23 changes: 23 additions & 0 deletions src/Shell/Command/DriverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CloudPatches\Shell\Command;

use Magento\CloudPatches\Patch\PatchCommandInterface;

/**
* Patch command driver interface
*/
interface DriverInterface extends PatchCommandInterface
{
/**
* Checks if the driver is installed
*
* @return bool
*/
public function isInstalled(): bool;
}
Loading