Skip to content

Commit

Permalink
WIP: I don't remember anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed May 29, 2023
1 parent 55646ee commit 5e413be
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 54 deletions.
72 changes: 59 additions & 13 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ final class Diff implements Command
private const FIRST_PHAR_ARG = 'pharA';
private const SECOND_PHAR_ARG = 'pharB';

// TODO: replace by DiffMode::X->value once bumping to PHP 8.2 as the min version.
private const LIST_FILES_DIFF_OPTION = 'list-diff';
private const GIT_DIFF_OPTION = 'git-diff';
private const GNU_DIFF_OPTION = 'gnu-diff';
Expand Down Expand Up @@ -122,10 +123,10 @@ public function execute(IO $io): int
return ExitCode::FAILURE;
}

$result1 = $this->compareArchives($diff, $io);
$this->showArchives($diff, $io);
$result2 = $this->compareContents($diff, $io);

return $result1 + $result2;
return $result2;
}

/**
Expand All @@ -146,19 +147,11 @@ private static function getPaths(IO $io): array
);
}

private function compareArchives(PharDiff $diff, IO $io): int
private function showArchives(PharDiff $diff, IO $io): void
{
$io->comment('<info>Comparing the two archives... (do not check the signatures)</info>');

$pharInfoA = $diff->getPharA();
$pharInfoB = $diff->getPharB();

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

return ExitCode::SUCCESS;
}

self::renderArchive(
$diff->getPharA()->getFileName(),
$pharInfoA,
Expand All @@ -172,11 +165,63 @@ private function compareArchives(PharDiff $diff, IO $io): int
$pharInfoB,
$io,
);
}

private function compareContents(PharDiff $diff, IO $io): int
{
$checkSumAlgorithm = $io->getOption(self::CHECK_OPTION)->asNullableNonEmptyString() ?? self::DEFAULT_CHECKSUM_ALGO;

if ($io->hasOption('-c') || $io->hasOption('--check')) {
return $diff->listChecksums($checkSumAlgorithm);
}

$diffResult = $diff->diff($io->getOption());
if ($io->getOption(self::GNU_DIFF_OPTION)->asBoolean()) {
$diffResult = $diff->gnuDiff();
} elseif ($io->getOption(self::GIT_DIFF_OPTION)->asBoolean()) {
$diffResult = $diff->gitDiff();
} else {
$diffResult = $diff->listDiff();
}

if (null === $diffResult || [[], []] === $diffResult) {
$io->success('The contents are identical');

return ExitCode::SUCCESS;
}

if (is_string($diffResult)) {
// Git or GNU diff: we don't have much control on the format
$io->writeln($diffResult);

return ExitCode::FAILURE;
}

$io->writeln(sprintf(
'--- Files present in "%s" but not in "%s"',
$diff->getPharA()->getFileName(),
$diff->getPharB()->getFileName(),
));
$io->writeln(sprintf(
'+++ Files present in "%s" but not in "%s"',
$diff->getPharB()->getFileName(),
$diff->getPharA()->getFileName(),
));

$io->newLine();

self::renderPaths('-', $diff->getPharA(), $diffResult[0], $io);
self::renderPaths('+', $diff->getPharB(), $diffResult[1], $io);

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

return ExitCode::FAILURE;
}

private function compareContents(PharDiff $diff, IO $io): int
private function compareContentssS(PharDiff $diff, IO $io): int
{
$io->comment('<info>Comparing the two archives contents...</info>');

Expand Down Expand Up @@ -275,8 +320,9 @@ private static function renderArchive(string $fileName, Pharaoh $pharInfo, IO $i
);

PharInfoRenderer::renderCompression($pharInfo, $io);
// Omit the signature
PharInfoRenderer::renderSignature($pharInfo, $io);
PharInfoRenderer::renderMetadata($pharInfo, $io);
PharInfoRenderer::renderContentsSummary($pharInfo, $io);
// TODO: checksum
}
}
12 changes: 12 additions & 0 deletions src/PharInfo/DiffMode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace KevinGH\Box\PharInfo;

enum DiffMode: string
{
case LIST = 'list-diff';
case GIT = 'git-diff';
case GNU = 'gnu-diff';
}
24 changes: 24 additions & 0 deletions src/PharInfo/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ public function getPharB(): Pharaoh
return $this->pharB;
}

/**
* @return string|string[][]
*/
public function diff(DiffMode $mode): string|array
{
if (DiffMode::LIST === $mode) {
return $this->listDiff();
}

return self::getDiff(
$this->pharA,
$this->pharB,
self::getModeCommand($mode),
);
}

private static function getModeCommand(DiffMode $mode): string
{
return match ($mode) {
DiffMode::GIT => 'git diff --no-index',
DiffMode::GNU => 'diff',
};
}

public function gitDiff(): ?string
{
return self::getDiff(
Expand Down
41 changes: 0 additions & 41 deletions src/Pharaoh/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,47 +93,6 @@ public function __construct(Pharaoh $pharA, Pharaoh $pharB)
$this->phars = [$pharA, $pharB];
}

/**
* Prints a git-formatted diff of the two PHARs.
*/
public function printGitDiff(): int
{
// Lazy way; requires git. Will replace with custom implementation later.

$argA = escapeshellarg($this->phars[0]->getTmp());
$argB = escapeshellarg($this->phars[1]->getTmp());
/** @var string $diff */
$diff = shell_exec("git diff --no-index {$argA} {$argB}");
echo $diff;
if (empty($diff) && $this->verbose) {
echo 'No differences encountered.', PHP_EOL;

return 0;
}

return 1;
}

/**
* Prints a GNU diff of the two PHARs.
*/
public function printGnuDiff(): int
{
// Lazy way. Will replace with custom implementation later.
$argA = escapeshellarg($this->phars[0]->getTmp());
$argB = escapeshellarg($this->phars[1]->getTmp());
/** @var string $diff */
$diff = shell_exec("diff {$argA} {$argB}");
echo $diff;
if (empty($diff) && $this->verbose) {
echo 'No differences encountered.', PHP_EOL;

return 0;
}

return 1;
}

/**
* Get hashes of all the files in the two arrays.
*
Expand Down

0 comments on commit 5e413be

Please sign in to comment.