Skip to content

Commit 5ea435e

Browse files
authored
MC-40529: 'Git apply' command silently fails to apply the patch in some environments (#17)
1 parent 6e48b53 commit 5ea435e

File tree

6 files changed

+64
-4
lines changed

6 files changed

+64
-4
lines changed

src/Command/Apply.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@ protected function configure()
8383
*/
8484
public function execute(InputInterface $input, OutputInterface $output)
8585
{
86-
$this->logger->notice($this->magentoVersion->get());
86+
$this->logger->info($this->magentoVersion->get());
8787

8888
try {
8989
$this->applyOptional->run($input, $output);
9090
} catch (RuntimeException $e) {
9191
$output->writeln('<error>' . $e->getMessage() . '</error>');
92+
$this->logger->info($this->magentoVersion->get());
9293
$this->logger->error($e->getMessage());
9394

9495
return self::RETURN_FAILURE;

src/Command/Ece/Apply.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ protected function configure()
9191
*/
9292
public function execute(InputInterface $input, OutputInterface $output)
9393
{
94-
$this->logger->notice($this->magentoVersion->get());
94+
$this->logger->info($this->magentoVersion->get());
9595

9696
try {
9797
$this->applyRequired->run($input, $output);
9898
$this->applyOptional->run($input, $output);
9999
$this->applyLocal->run($input, $output);
100100
} catch (RuntimeException $e) {
101101
$output->writeln('<error>' . $e->getMessage() . '</error>');
102+
$this->logger->info($this->magentoVersion->get());
102103
$this->logger->error($e->getMessage());
103104

104105
return self::RETURN_FAILURE;

src/Command/Ece/Revert.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,13 @@ protected function configure()
7373
*/
7474
public function execute(InputInterface $input, OutputInterface $output)
7575
{
76-
$this->logger->notice($this->magentoVersion->get());
76+
$this->logger->info($this->magentoVersion->get());
7777

7878
try {
7979
$this->revert->run($input, $output);
8080
} catch (RuntimeException $e) {
8181
$output->writeln('<error>' . $e->getMessage() . '</error>');
82+
$this->logger->info($this->magentoVersion->get());
8283
$this->logger->error($e->getMessage());
8384

8485
return self::RETURN_FAILURE;

src/Command/Revert.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,13 @@ protected function configure()
9696
*/
9797
public function execute(InputInterface $input, OutputInterface $output)
9898
{
99-
$this->logger->notice($this->magentoVersion->get());
99+
$this->logger->info($this->magentoVersion->get());
100100

101101
try {
102102
$this->revert->run($input, $output);
103103
} catch (RuntimeException $e) {
104104
$output->writeln('<error>' . $e->getMessage() . '</error>');
105+
$this->logger->info($this->magentoVersion->get());
105106
$this->logger->error($e->getMessage());
106107

107108
return self::RETURN_FAILURE;

src/Patch/Applier.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ private function prepareContent(string $content): string
175175
$content = $this->gitConverter->convert($content);
176176
}
177177

178+
$content = $this->replaceDiffGit($content);
179+
178180
return $content;
179181
}
182+
183+
/**
184+
* Replace `diff --git`
185+
*
186+
* In some environments 'git apply' command skips applying a patch
187+
* while execution inside the git working tree. To prevent this issue
188+
* we need to replace `diff --git` with `diff -Nuar` in the patch source.
189+
*
190+
* @param string $content
191+
* @return string
192+
*/
193+
private function replaceDiffGit(string $content): string
194+
{
195+
return preg_replace(['/^diff --git/', '/\ndiff --git/'], ['diff -Nuar', "\ndiff -Nuar"], $content);
196+
}
180197
}

src/Test/Unit/Patch/ApplierTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,43 @@ public function testStatusApplied()
278278

279279
$this->assertSame(StatusPool::APPLIED, $this->applier->status($patchContent));
280280
}
281+
282+
/**
283+
* Tests checkApply operation.
284+
*
285+
* Verifies that 'diff --git' is replaced with 'diff -Nuar'
286+
*/
287+
public function testCheckApply()
288+
{
289+
$patchContent = 'diff --git a/vendor/module-deploy/Queue.php b/vendor/module-deploy/Queue.php
290+
--- a/vendor/magento/module-deploy/Process/Queue.php
291+
+++ b/vendor/magento/module-deploy/Process/Queue.php
292+
diff --git a/vendor/magento/module-email/Model/Transport.php b/vendor/magento/module-email/Model/Transport.php
293+
--- a/vendor/magento/module-email/Model/Transport.php
294+
+++ b/vendor/magento/module-email/Model/Transport.php
295+
- echo "diff --git";
296+
+ echo "diff --Nuar";
297+
diff -Nuar a/vendor/magento/module-email/Model/Transport.php b/vendor/magento/module-email/Model/Transport.php
298+
';
299+
$expectedPatchContent = 'diff -Nuar a/vendor/module-deploy/Queue.php b/vendor/module-deploy/Queue.php
300+
--- a/vendor/magento/module-deploy/Process/Queue.php
301+
+++ b/vendor/magento/module-deploy/Process/Queue.php
302+
diff -Nuar a/vendor/magento/module-email/Model/Transport.php b/vendor/magento/module-email/Model/Transport.php
303+
--- a/vendor/magento/module-email/Model/Transport.php
304+
+++ b/vendor/magento/module-email/Model/Transport.php
305+
- echo "diff --git";
306+
+ echo "diff --Nuar";
307+
diff -Nuar a/vendor/magento/module-email/Model/Transport.php b/vendor/magento/module-email/Model/Transport.php
308+
';
309+
310+
$this->magentoVersion->expects($this->once())
311+
->method('isGitBased')
312+
->willReturn(false);
313+
314+
$this->patchCommand->expects($this->once())
315+
->method('applyCheck')
316+
->with($expectedPatchContent);
317+
318+
$this->assertTrue($this->applier->checkApply($patchContent));
319+
}
281320
}

0 commit comments

Comments
 (0)