Skip to content

Commit

Permalink
feat: Colorize the archive diff
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Oct 11, 2023
1 parent 0590de7 commit 23a3d32
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 58 deletions.
51 changes: 41 additions & 10 deletions src/Console/Command/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@
use Symfony\Component\Filesystem\Path;
use Webmozart\Assert\Assert;
use function array_map;
use function explode;
use function implode;
use function sprintf;
use function str_starts_with;

/**
* @private
Expand Down Expand Up @@ -164,23 +166,52 @@ private static function getPaths(IO $io): array

private function renderArchivesDiff(PharDiff $diff, IO $io): void
{
$differ = new Differ(
new UnifiedDiffOutputBuilder("\n--- PHAR A\n+++ PHAR B\n"),
);

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

if ($pharA === $pharB) {
if ($pharASummary === $pharBSummary) {
return;
}

$io->writeln(
self::createColorizedDiff(
$pharASummary,
$pharBSummary,
),
);
}

private static function createColorizedDiff(string $pharASummary, string $pharBSummary): string
{
$differ = new Differ(
new UnifiedDiffOutputBuilder(
"\n<diff-expected>--- PHAR A</diff-expected>\n<diff-actual>+++ PHAR B</diff-actual>\n",
),
);

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

$lines = explode("\n", $result);

$colorizedLines = array_map(
static fn (string $line) => match (true) {
str_starts_with($line, '+') => sprintf(
'<diff-actual>%s</diff-actual>',
$line,
),
str_starts_with($line, '-') => sprintf(
'<diff-expected>%s</diff-expected>',
$line,
),
default => $line,
},
$lines,
);

$io->writeln($result);
return implode("\n", $colorizedLines);
}

private static function getDiffMode(IO $io): DiffMode
Expand Down
96 changes: 48 additions & 48 deletions tests/Console/Command/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,18 @@ private static function commonDiffPharsProvider(DiffMode $diffMode): iterable
Metadata: None
Contents: 1 file (6.65KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
-Files Compression: None
+Files Compression: GZ
<diff-expected>-Files Compression: None</diff-expected>
<diff-actual>+Files Compression: GZ</diff-actual>
Signature: SHA-1
-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+Signature Hash: 3A388D86C91C36659A043D52C2DEB64E8848DD1A
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 3A388D86C91C36659A043D52C2DEB64E8848DD1A</diff-actual>
Metadata: None
-Contents: 1 file (6.64KB)
+Contents: 1 file (6.65KB)
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Contents: 1 file (6.65KB)</diff-actual>
// Comparing the two archives contents (%s diff)...
Expand Down Expand Up @@ -441,14 +441,14 @@ private static function fileNameDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
<diff-expected>-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57</diff-expected>
<diff-actual>+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-actual>
Metadata: None
Contents: 1 file (6.64KB)
Expand Down Expand Up @@ -490,17 +490,17 @@ private static function fileNameDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5</diff-actual>
Metadata: None
-Contents: 1 file (6.64KB)
+Contents: 1 file (6.61KB)
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Contents: 1 file (6.61KB)</diff-actual>
// Comparing the two archives contents (file-name diff)...
Expand Down Expand Up @@ -616,14 +616,14 @@ public static function GNUDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
<diff-expected>-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57</diff-expected>
<diff-actual>+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-actual>
Metadata: None
Contents: 1 file (6.64KB)
Expand Down Expand Up @@ -660,17 +660,17 @@ public static function GNUDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5</diff-actual>
Metadata: None
-Contents: 1 file (6.64KB)
+Contents: 1 file (6.61KB)
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Contents: 1 file (6.61KB)</diff-actual>
// Comparing the two archives contents (gnu diff)...
Expand Down Expand Up @@ -701,17 +701,17 @@ public static function GNUDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5</diff-actual>
Metadata: None
-Contents: 1 file (6.64KB)
+Contents: 1 file (6.61KB)
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Contents: 1 file (6.61KB)</diff-actual>
// Comparing the two archives contents (gnu diff)...
Expand Down Expand Up @@ -763,14 +763,14 @@ public static function checksumDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
<diff-expected>-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57</diff-expected>
<diff-actual>+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-actual>
Metadata: None
Contents: 1 file (6.64KB)
Expand Down Expand Up @@ -812,14 +812,14 @@ public static function checksumDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
<diff-expected>-Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57</diff-expected>
<diff-actual>+Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-actual>
Metadata: None
Contents: 1 file (6.64KB)
Expand Down Expand Up @@ -861,17 +861,17 @@ public static function checksumDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
--- PHAR A
+++ PHAR B
<diff-expected>--- PHAR A</diff-expected>
<diff-actual>+++ PHAR B</diff-actual>
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
<diff-expected>-Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51</diff-expected>
<diff-actual>+Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5</diff-actual>
Metadata: None
-Contents: 1 file (6.64KB)
+Contents: 1 file (6.61KB)
<diff-expected>-Contents: 1 file (6.64KB)</diff-expected>
<diff-actual>+Contents: 1 file (6.61KB)</diff-actual>
// Comparing the two archives contents (checksum diff)...
Expand Down

0 comments on commit 23a3d32

Please sign in to comment.