From 2520c968e1d88f9dad8fcc224f1bba86aefa08a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20FIDRY?=
<5175937+theofidry@users.noreply.github.com>
Date: Wed, 11 Oct 2023 20:04:20 +0200
Subject: [PATCH] feat: Colorize the archive diff (#1061)
---
src/Console/Command/Diff.php | 51 ++++++++++++----
tests/Console/Command/DiffTest.php | 96 +++++++++++++++---------------
2 files changed, 89 insertions(+), 58 deletions(-)
diff --git a/src/Console/Command/Diff.php b/src/Console/Command/Diff.php
index d44ea9f44..192de1854 100644
--- a/src/Console/Command/Diff.php
+++ b/src/Console/Command/Diff.php
@@ -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
@@ -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--- PHAR A\n+++ PHAR B\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(
+ '%s',
+ $line,
+ ),
+ str_starts_with($line, '-') => sprintf(
+ '%s',
+ $line,
+ ),
+ default => $line,
+ },
+ $lines,
);
- $io->writeln($result);
+ return implode("\n", $colorizedLines);
}
private static function getDiffMode(IO $io): DiffMode
diff --git a/tests/Console/Command/DiffTest.php b/tests/Console/Command/DiffTest.php
index 8bf5062bf..6c72b572c 100644
--- a/tests/Console/Command/DiffTest.php
+++ b/tests/Console/Command/DiffTest.php
@@ -390,18 +390,18 @@ private static function commonDiffPharsProvider(DiffMode $diffMode): iterable
Metadata: None
Contents: 1 file (6.65KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
- -Files Compression: None
- +Files Compression: GZ
+ -Files Compression: None
+ +Files Compression: GZ
Signature: SHA-1
- -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
- +Signature Hash: 3A388D86C91C36659A043D52C2DEB64E8848DD1A
+ -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ +Signature Hash: 3A388D86C91C36659A043D52C2DEB64E8848DD1A
Metadata: None
- -Contents: 1 file (6.64KB)
- +Contents: 1 file (6.65KB)
+ -Contents: 1 file (6.64KB)
+ +Contents: 1 file (6.65KB)
// Comparing the two archives contents (%s diff)...
@@ -441,14 +441,14 @@ private static function fileNameDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
- +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+ +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
Metadata: None
Contents: 1 file (6.64KB)
@@ -490,17 +490,17 @@ private static function fileNameDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
- +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
+ -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
Metadata: None
- -Contents: 1 file (6.64KB)
- +Contents: 1 file (6.61KB)
+ -Contents: 1 file (6.64KB)
+ +Contents: 1 file (6.61KB)
// Comparing the two archives contents (file-name diff)...
@@ -616,14 +616,14 @@ public static function GNUDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
- +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+ +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
Metadata: None
Contents: 1 file (6.64KB)
@@ -660,17 +660,17 @@ public static function GNUDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
- +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
+ -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
Metadata: None
- -Contents: 1 file (6.64KB)
- +Contents: 1 file (6.61KB)
+ -Contents: 1 file (6.64KB)
+ +Contents: 1 file (6.61KB)
// Comparing the two archives contents (gnu diff)...
@@ -701,17 +701,17 @@ public static function GNUDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
- +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
+ -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
Metadata: None
- -Contents: 1 file (6.64KB)
- +Contents: 1 file (6.61KB)
+ -Contents: 1 file (6.64KB)
+ +Contents: 1 file (6.61KB)
// Comparing the two archives contents (gnu diff)...
@@ -763,14 +763,14 @@ public static function checksumDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
- +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+ +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
Metadata: None
Contents: 1 file (6.64KB)
@@ -812,14 +812,14 @@ public static function checksumDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.64KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
- +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ -Signature Hash: 311080EF8E479CE18D866B744B7D467880BFBF57
+ +Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
Metadata: None
Contents: 1 file (6.64KB)
@@ -861,17 +861,17 @@ public static function checksumDiffPharsProvider(): iterable
Metadata: None
Contents: 1 file (6.61KB)
- --- PHAR A
- +++ PHAR B
+ --- PHAR A
+ +++ PHAR B
@@ @@
Archive Compression: None
Files Compression: None
Signature: SHA-1
- -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
- +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
+ -Signature Hash: 9ADC09F73909EDF14F8A4ABF9758B6FFAD1BBC51
+ +Signature Hash: 122A636B8BB0348C9514833D70281EF6306A5BF5
Metadata: None
- -Contents: 1 file (6.64KB)
- +Contents: 1 file (6.61KB)
+ -Contents: 1 file (6.64KB)
+ +Contents: 1 file (6.61KB)
// Comparing the two archives contents (checksum diff)...