|
| 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 | +} |
0 commit comments