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

fix: Fix the support of the diff with checksum #1057

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ private static function getShortSummary(PharInfo $pharInfo, IO $io): string
{
$output = new BufferedOutput(
$io->getVerbosity(),
$io->isDecorated(),
$io->getOutput()->getFormatter(),
false,
clone $io->getOutput()->getFormatter(),
);

self::renderShortSummary(
Expand Down
8 changes: 8 additions & 0 deletions src/Console/OutputFormatterConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,13 @@ public static function configureFormatter(OutputFormatterInterface $outputFormat
'warning',
new OutputFormatterStyle('white', 'red'),
);
$outputFormatter->setStyle(
'diff-expected',
new OutputFormatterStyle('green'),
);
$outputFormatter->setStyle(
'diff-actual',
new OutputFormatterStyle('red'),
);
}
}
105 changes: 100 additions & 5 deletions src/Phar/PharDiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Process;
use UnexpectedValueException;
use ValueError;
use function array_diff;
use function array_key_exists;
use function array_map;
use function count;
use function hash_file;
use function implode;
use function iterator_to_array;
use function sprintf;
use function str_replace;
use const DIRECTORY_SEPARATOR;

Expand Down Expand Up @@ -76,9 +82,11 @@ public function diff(DiffMode $mode, string $checksumAlgorithm): null|string|arr
}

if (DiffMode::CHECKSUM === $mode) {
$this->listChecksums($checksumAlgorithm);

return null;
return self::getChecksumDiff(
$this->pharInfoA,
$this->pharInfoB,
$checksumAlgorithm,
);
}

return self::getDiff(
Expand Down Expand Up @@ -162,12 +170,71 @@ private static function getDiff(PharInfo $pharInfoA, PharInfo $pharInfoB, string
);
}

private static function getChecksumDiff(
PharInfo $pharInfoA,
PharInfo $pharInfoB,
string $checksumAlgorithm,
): ?string {
$pharInfoAFileHashes = self::getFileHashesByRelativePathname(
$pharInfoA,
$checksumAlgorithm,
);
$pharInfoBFileHashes = self::getFileHashesByRelativePathname(
$pharInfoB,
$checksumAlgorithm,
);
$output = [
'<diff-expected>--- PHAR A</diff-expected>',
'<diff-actual>+++ PHAR B</diff-actual>',
'@@ @@',
];

foreach ($pharInfoAFileHashes as $filePath => $fileAHash) {
if (!array_key_exists($filePath, $pharInfoBFileHashes)) {
$output[] = $filePath;
$output[] = sprintf(
"\t<diff-expected>- %s</diff-expected>",
$fileAHash,
);

continue;
}

$fileBHash = $pharInfoBFileHashes[$filePath];
unset($pharInfoBFileHashes[$filePath]);

if ($fileAHash === $fileBHash) {
continue;
}

$output[] = $filePath;
$output[] = sprintf(
"\t<diff-expected>- %s</diff-expected>",
$fileAHash,
);
$output[] = sprintf(
"\t<diff-actual>+ %s</diff-actual>",
$fileBHash,
);
}

foreach ($pharInfoBFileHashes as $filePath => $fileBHash) {
$output[] = $filePath;
$output[] = sprintf(
"\t<diff-actual>+ %s</diff-actual>",
$fileBHash,
);
}

return 3 === count($output) ? null : implode("\n", $output);
}

/**
* @return string[]
*/
private static function collectFiles(PharInfo $phar): array
private static function collectFiles(PharInfo $pharInfo): array
{
$basePath = $phar->getTmp().DIRECTORY_SEPARATOR;
$basePath = $pharInfo->getTmp().DIRECTORY_SEPARATOR;

return array_map(
static fn (SplFileInfo $fileInfo): string => str_replace($basePath, '', $fileInfo->getRealPath()),
Expand All @@ -180,4 +247,32 @@ private static function collectFiles(PharInfo $phar): array
),
);
}

/**
* @return array<string, string>
*/
private static function getFileHashesByRelativePathname(
PharInfo $pharInfo,
string $algorithm,
): array {
$hashFiles = [];

try {
foreach ($pharInfo->getFiles() as $file) {
$hashFiles[$file->getRelativePathname()] = hash_file(
$algorithm,
$file->getPathname(),
);
}
} catch (ValueError) {
throw new UnexpectedValueException(
sprintf(
'Unexpected algorithm "%s". Please pick a registered hashing algorithm (checksum `hash_algos()`).',
$algorithm,
),
);
}

return $hashFiles;
}
}
Loading