Skip to content

Commit e5b4e7b

Browse files
committed
Nice PHAR prefix diff
1 parent a82e010 commit e5b4e7b

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

.github/scripts/diffPrefixes.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php declare(strict_types = 1);
2+
3+
use Nette\Utils\Strings;
4+
use SebastianBergmann\Diff\Differ;
5+
6+
require_once __DIR__ . '/../../vendor/autoload.php';
7+
8+
$diffFile = $argv[1] ?? null;
9+
$oldDir = $argv[2] ?? null;
10+
$newDir = $argv[3] ?? null;
11+
if ($diffFile === null || !is_file($diffFile)) {
12+
exit(1);
13+
}
14+
15+
if ($oldDir === null || !is_dir($oldDir)) {
16+
exit(1);
17+
}
18+
19+
if ($newDir === null || !is_dir($newDir)) {
20+
exit(1);
21+
}
22+
23+
$diffLines = explode("\n", file_get_contents($diffFile));
24+
$differ = new Differ(new \SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder('', true));
25+
$isDifferent = false;
26+
foreach ($diffLines as $diffLine) {
27+
$operation = $diffLine[0];
28+
if ($operation === ' ') {
29+
continue;
30+
}
31+
32+
$pathWithLine = substr($diffLine, 1);
33+
$pathParts = explode(':', $pathWithLine);
34+
$path = $pathParts[0];
35+
$lineNumber = $pathParts[1] ?? null;
36+
if ($lineNumber === null) {
37+
continue;
38+
}
39+
$oldFilePath = $oldDir . '/' . $path;
40+
if (!is_file($oldFilePath)) {
41+
continue;
42+
}
43+
44+
$newFilePath = $newDir . '/' . $path;
45+
if (!is_file($newFilePath)) {
46+
continue;
47+
}
48+
49+
$stringDiff = $differ->diff(file_get_contents($oldFilePath), file_get_contents($newFilePath));
50+
if ($stringDiff === '') {
51+
continue;
52+
}
53+
54+
$isDifferent = true;
55+
56+
echo "$path:\n";
57+
$startLine = 1;
58+
$startContext = 1;
59+
foreach (explode("\n", $stringDiff) as $i => $line) {
60+
$matches = Strings::match($line, '/^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/');
61+
if ($matches !== null) {
62+
$startLine = (int) $matches[1];
63+
$startContext = (int) $matches[2];
64+
continue;
65+
}
66+
67+
if ($lineNumber < $startLine || $lineNumber > ($startLine + $startContext)) {
68+
continue;
69+
}
70+
71+
if (str_starts_with($line, '+')) {
72+
echo "\033[32m$line\033[0m\n";
73+
} elseif (str_starts_with($line, '-')) {
74+
echo "\033[31m$line\033[0m\n";
75+
} else {
76+
echo "$line\n";
77+
}
78+
}
79+
80+
echo "\n";
81+
}
82+
83+
if ($isDifferent) {
84+
exit(1);
85+
}

.github/workflows/phar.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,10 @@ jobs:
268268
run: "php .github/scripts/listPrefix.php ${{ github.workspace }}/phar-new > phar-new.txt"
269269

270270
- name: "Diff locations"
271-
run: "diff -u phar-old.txt phar-new.txt"
271+
run: "diff -u phar-old.txt phar-new.txt > diff.txt || true"
272+
273+
- name: "Diff files where prefix changed"
274+
run: "php .github/scripts/diffPrefixes.php ${{ github.workspace }}/diff.txt ${{ github.workspace }}/phar-old ${{ github.workspace }}/phar-new"
272275

273276
commit:
274277
name: "Commit PHAR"

0 commit comments

Comments
 (0)