Skip to content

Commit

Permalink
feat: Rework the diff command output (#1054)
Browse files Browse the repository at this point in the history
- Always display a summary of both archives upon equality failure.
- Provide a human-readable diff of the archives when possible.
- Make the message more explicit that if no diff was found for the
  archives content, this is true for the selected diff mode, i.e. a
difference may be found with another diff mode.
  • Loading branch information
theofidry committed Oct 11, 2023
1 parent d733d96 commit 3a8ca01
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 126 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"phpdocumentor/reflection-docblock": "^5.3",
"phpdocumentor/type-resolver": "^1.7",
"psr/log": "^3.0",
"sebastian/diff": "^4.0",
"seld/jsonlint": "^1.9",
"symfony/console": "^6.1.7",
"symfony/filesystem": "^6.1.5",
Expand Down
134 changes: 67 additions & 67 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 60 additions & 29 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
use KevinGH\Box\Phar\DiffMode;
use KevinGH\Box\Phar\PharDiff;
use KevinGH\Box\Phar\PharInfo;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\BufferedOutput;
Expand Down Expand Up @@ -119,17 +121,18 @@ public function execute(IO $io): int

$io->comment('<info>Comparing the two archives...</info>');

$pharInfoA = $diff->getPharInfoA();
$pharInfoB = $diff->getPharInfoB();

if ($pharInfoA->equals($pharInfoB)) {
if ($diff->equals()) {
$io->success('The two archives are identical.');

return ExitCode::SUCCESS;
}

$this->compareArchives($diff, $io);
$this->compareContents($diff, $diffMode, $io);
self::renderSummary($diff->getPharInfoA(), $io);
$io->newLine();
self::renderSummary($diff->getPharInfoB(), $io);

$this->renderArchivesDiff($diff, $io);
$this->renderContentsDiff($diff, $diffMode, $io);

return ExitCode::FAILURE;
}
Expand All @@ -152,24 +155,25 @@ private static function getPaths(IO $io): array
);
}

private function compareArchives(PharDiff $diff, IO $io): void
private function renderArchivesDiff(PharDiff $diff, IO $io): void
{
$pharInfoA = $diff->getPharInfoA();
$pharInfoB = $diff->getPharInfoB();

self::renderArchive(
$diff->getPharInfoA()->getFileName(),
$pharInfoA,
$io,
$differ = new Differ(
new UnifiedDiffOutputBuilder("\n--- PHAR A\n+++ PHAR B\n"),
);

$io->newLine();
$pharA = self::getShortSummary($diff->getPharInfoA(), $io);
$pharB = self::getShortSummary($diff->getPharInfoB(), $io);

self::renderArchive(
$diff->getPharInfoB()->getFileName(),
$pharInfoB,
$io,
if ($pharA === $pharB) {
return;
}

$result = $differ->diff(
$pharA,
$pharB,
);

$io->writeln($result);
}

private static function getDiffMode(IO $io): DiffMode
Expand Down Expand Up @@ -216,9 +220,14 @@ private static function getDiffMode(IO $io): DiffMode
return DiffMode::from($io->getOption(self::DIFF_OPTION)->asNonEmptyString());
}

private function compareContents(PharDiff $diff, DiffMode $diffMode, IO $io): void
private function renderContentsDiff(PharDiff $diff, DiffMode $diffMode, IO $io): void
{
$io->comment('<info>Comparing the two archives contents...</info>');
$io->comment(
sprintf(
'<info>Comparing the two archives contents (%s diff)...</info>',
$diffMode->value,
),
);

$checkSumAlgorithm = $io->getOption(self::CHECK_OPTION)->asNullableNonEmptyString() ?? self::DEFAULT_CHECKSUM_ALGO;

Expand All @@ -231,7 +240,7 @@ private function compareContents(PharDiff $diff, DiffMode $diffMode, IO $io): vo
$diffResult = $diff->diff($diffMode);

if (null === $diffResult || [[], []] === $diffResult) {
$io->success('The contents are identical');
$io->writeln('No difference could be observed with this mode.');

return;
}
Expand Down Expand Up @@ -259,10 +268,12 @@ private function compareContents(PharDiff $diff, DiffMode $diffMode, IO $io): vo
self::renderPaths('-', $diff->getPharInfoA(), $diffResult[0], $io);
self::renderPaths('+', $diff->getPharInfoB(), $diffResult[1], $io);

$io->error(sprintf(
'%d file(s) difference',
count($diffResult[0]) + count($diffResult[1]),
));
$io->error(
sprintf(
'%d file(s) difference',
count($diffResult[0]) + count($diffResult[1]),
),
);
}

/**
Expand Down Expand Up @@ -294,19 +305,39 @@ private static function renderPaths(string $symbol, PharInfo $pharInfo, array $p
$io->writeln($lines);
}

private static function renderArchive(string $fileName, PharInfo $pharInfo, IO $io): void
private static function renderSummary(PharInfo $pharInfo, IO $io): void
{
$io->writeln(
sprintf(
'<comment>Archive: </comment><fg=cyan;options=bold>%s</>',
$fileName,
$pharInfo->getFileName(),
),
);

self::renderShortSummary($pharInfo, $io);
}

private static function getShortSummary(PharInfo $pharInfo, IO $io): string
{
$output = new BufferedOutput(
$io->getVerbosity(),
$io->isDecorated(),
$io->getOutput()->getFormatter(),
);

self::renderShortSummary(
$pharInfo,
$io->withOutput($output),
);

return $output->fetch();
}

private static function renderShortSummary(PharInfo $pharInfo, IO $io): void
{
PharInfoRenderer::renderCompression($pharInfo, $io);
PharInfoRenderer::renderSignature($pharInfo, $io);
PharInfoRenderer::renderMetadata($pharInfo, $io);
PharInfoRenderer::renderContentsSummary($pharInfo, $io);
// TODO: checksum
}
}
5 changes: 5 additions & 0 deletions src/Phar/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function getPharInfoB(): PharInfo
return $this->pharInfoB;
}

public function equals(): bool
{
return $this->pharInfoA->equals($this->pharInfoB);
}

/**
* @return null|string|array{string[], string[]}
*/
Expand Down
Loading

0 comments on commit 3a8ca01

Please sign in to comment.