Skip to content

Commit

Permalink
fix: Fix the git diff mode (#1062)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Oct 11, 2023
1 parent 2520c96 commit b7e493d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/Phar/Differ/DifferFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function create(
): Differ {
return match ($mode) {
DiffMode::FILE_NAME => new FilenameDiffer(),
DiffMode::GIT => new ProcessCommandBasedDiffer('git diff --no-index'),
DiffMode::GIT => new GitDiffer(),
DiffMode::GNU => new ProcessCommandBasedDiffer('diff --exclude='.Extract::PHAR_META_PATH),
DiffMode::CHECKSUM => new ChecksumDiffer($checksumAlgorithm),
};
Expand Down
65 changes: 65 additions & 0 deletions src/Phar/Differ/GitDiffer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Phar\Differ;

use Fidry\Console\Input\IO;
use KevinGH\Box\Console\Command\Extract;
use KevinGH\Box\Phar\PharInfo;
use function array_filter;
use function explode;
use function implode;
use function sprintf;
use function str_starts_with;

final class GitDiffer implements Differ
{
public function diff(PharInfo $pharInfoA, PharInfo $pharInfoB, IO $io): void
{
$gitDiff = ProcessCommandBasedDiffer::getDiff(
$pharInfoA,
$pharInfoB,
'git diff --no-index',
);

if (null === $gitDiff) {
$io->writeln(Differ::NO_DIFF_MESSAGE);

return;
}

$separator = 'diff --git ';

$diffLines = explode(
$separator,
$gitDiff,
);

$pharMetaLine = sprintf(
'a%2$s/%1$s b%3$s/%1$s',
Extract::PHAR_META_PATH,
$pharInfoA->getFileName(),
$pharInfoB->getFileName(),
);

$filteredLines = array_filter(
$diffLines,
static fn (string $line) => !str_starts_with($line, $pharMetaLine)
);

$filteredDiff = implode($separator, $filteredLines);

$io->writeln('' === $filteredDiff ? Differ::NO_DIFF_MESSAGE : $filteredDiff);
}
}
2 changes: 1 addition & 1 deletion src/Phar/Differ/ProcessCommandBasedDiffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function diff(PharInfo $pharInfoA, PharInfo $pharInfoB, IO $io): void
$io->writeln($result ?? Differ::NO_DIFF_MESSAGE);
}

private static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string $command): ?string
public static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string $command): ?string
{
$pharInfoATmp = $pharInfoA->getTmp();
$pharInfoBTmp = $pharInfoB->getTmp();
Expand Down
87 changes: 27 additions & 60 deletions tests/Console/Command/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace KevinGH\Box\Console\Command;

use Fidry\Console\Command\Command;
use Fidry\Console\DisplayNormalizer;
use Fidry\Console\ExitCode;
use InvalidArgumentException;
use KevinGH\Box\Phar\DiffMode;
Expand All @@ -25,8 +24,6 @@
use KevinGH\Box\Test\RequiresPharReadonlyOff;
use Symfony\Component\Console\Output\OutputInterface;
use function array_splice;
use function ob_get_clean;
use function Safe\ob_start;
use function Safe\realpath;

/**
Expand Down Expand Up @@ -63,10 +60,6 @@ public function test_it_can_display_the_diff_of_two_phar_files(
?string $expectedOutput,
int $expectedStatusCode,
): void {
if (DiffMode::GIT === $diffMode) {
self::markTestSkipped('TODO');
}

$command = [
'command' => 'diff',
'pharA' => realpath($pharAPath),
Expand Down Expand Up @@ -125,7 +118,6 @@ public function test_it_can_display_the_list_diff_of_two_phar_files(): void
*/
public function test_it_can_display_the_git_diff_of_two_phar_files(): void
{
self::markTestSkipped('TODO');
$pharPath = realpath(self::FIXTURES_DIR.'/simple-phar-foo.phar');

$this->commandTester->execute(
Expand All @@ -138,7 +130,7 @@ public function test_it_can_display_the_git_diff_of_two_phar_files(): void
);

$expectedOutput = <<<'OUTPUT'
⚠️ <warning>Using the option "list-diff" is deprecated. Use "--diff=file-name" instead.</warning>
⚠️ <warning>Using the option "git-diff" is deprecated. Use "--diff=git" instead.</warning>
// Comparing the two archives...
Expand Down Expand Up @@ -182,54 +174,6 @@ public function test_it_can_display_the_gnu_diff_of_two_phar_files(): void
);
}

public function test_it_can_check_the_sum_of_two_phar_files(): void
{
self::markTestSkipped('TODO');
(function (): void {
$pharPath = realpath(self::FIXTURES_DIR.'/simple-phar-foo.phar');

ob_start();
$this->commandTester->execute(
[
'command' => 'diff',
'pharA' => $pharPath,
'pharB' => $pharPath,
'--check' => null,
],
);
$actual = DisplayNormalizer::removeTrailingSpaces(ob_get_clean());

$expected = <<<'OUTPUT'
No differences encountered.

OUTPUT;

$this->assertSame($expected, $actual);
$this->assertSame(0, $this->commandTester->getStatusCode());
})();

(function (): void {
ob_start();
$this->commandTester->execute(
[
'command' => 'diff',
'pharA' => realpath(self::FIXTURES_DIR.'/simple-phar-foo.phar'),
'pharB' => realpath(self::FIXTURES_DIR.'/simple-phar-bar.phar'),
'--check' => null,
],
);
$actual = DisplayNormalizer::removeTrailingSpaces(ob_get_clean());

$expected = <<<'OUTPUT'
No differences encountered.

OUTPUT;

$this->assertSame($expected, $actual);
$this->assertSame(0, $this->commandTester->getStatusCode());
})();
}

public function test_it_cannot_compare_non_existent_files(): void
{
try {
Expand Down Expand Up @@ -538,15 +482,26 @@ public static function gitDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
// Comparing the two archives contents...
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
<diff-expected>-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57</diff-expected>
<diff-actual>+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-actual>
Metadata: None
Contents: 1 file (6.64KB)
// Comparing the two archives contents (git diff)...
diff --git asimple-phar-foo.phar/foo.php bsimple-phar-bar.phar/bar.php
similarity index 100%
rename from simple-phar-foo.phar/foo.php
rename to simple-phar-bar.phar/bar.php

OUTPUT,
3,
ExitCode::FAILURE,
];

yield 'same files different content' => [
Expand All @@ -572,7 +527,19 @@ public static function gitDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
// Comparing the two archives contents...
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5</diff-actual>
Metadata: None
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Contents: 1 file (6.61KB)</diff-actual>
// Comparing the two archives contents (git diff)...
diff --git asimple-phar-bar.phar/bar.php bsimple-phar-baz.phar/bar.php
index 290849f..8aac305 100644
Expand Down

0 comments on commit b7e493d

Please sign in to comment.