1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the box project. |
7
|
|
|
* |
8
|
|
|
* (c) Kevin Herrera <[email protected]> |
9
|
|
|
* Théo Fidry <[email protected]> |
10
|
|
|
* |
11
|
|
|
* This source file is subject to the MIT license that is bundled |
12
|
|
|
* with this source code in the file LICENSE. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace KevinGH\Box\Console\Command; |
16
|
|
|
|
17
|
|
|
use Assert\Assertion; |
18
|
|
|
use function count; |
19
|
|
|
use function is_string; |
20
|
|
|
use KevinGH\Box\Console\PharInfoRenderer; |
21
|
|
|
use KevinGH\Box\PharInfo\PharDiff; |
22
|
|
|
use KevinGH\Box\PharInfo\PharInfo; |
23
|
|
|
use KevinGH\Box\PhpSettingsHandler; |
24
|
|
|
use function sprintf; |
25
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
27
|
|
|
use Symfony\Component\Console\Input\InputOption; |
28
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
29
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
30
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
31
|
|
|
use Throwable; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @private |
35
|
|
|
*/ |
36
|
|
|
final class Diff extends Command |
37
|
|
|
{ |
38
|
|
|
use CreateTemporaryPharFile; |
39
|
|
|
|
40
|
|
|
private const FIRST_PHAR_ARG = 'pharA'; |
41
|
|
|
private const SECOND_PHAR_ARG = 'pharB'; |
42
|
|
|
|
43
|
|
|
private const LIST_FILES_DIFF_OPTION = 'list-diff'; |
44
|
|
|
private const GIT_DIFF_OPTION = 'git-diff'; |
45
|
|
|
private const GNU_DIFF_OPTION = 'gnu-diff'; |
46
|
|
|
private const CHECK_OPTION = 'check'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
|
|
protected function configure(): void |
52
|
|
|
{ |
53
|
|
|
parent::configure(); |
54
|
|
|
|
55
|
|
|
$this->setName('diff'); |
56
|
|
|
$this->setDescription('🕵 Displays the differences between all of the files in two PHARs'); |
57
|
|
|
|
58
|
|
|
$this->addArgument( |
59
|
|
|
self::FIRST_PHAR_ARG, |
60
|
|
|
InputArgument::REQUIRED, |
61
|
|
|
'The first PHAR' |
62
|
|
|
); |
63
|
|
|
$this->addArgument( |
64
|
|
|
self::SECOND_PHAR_ARG, |
65
|
|
|
InputArgument::REQUIRED, |
66
|
|
|
'The second PHAR' |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->addOption( |
70
|
|
|
self::GNU_DIFF_OPTION, |
71
|
|
|
null, |
72
|
|
|
InputOption::VALUE_NONE, |
73
|
|
|
'Displays a GNU diff' |
74
|
|
|
); |
75
|
|
|
$this->addOption( |
76
|
|
|
self::GIT_DIFF_OPTION, |
77
|
|
|
null, |
78
|
|
|
InputOption::VALUE_NONE, |
79
|
|
|
'Displays a Git diff' |
80
|
|
|
); |
81
|
|
|
$this->addOption( |
82
|
|
|
self::LIST_FILES_DIFF_OPTION, |
83
|
|
|
null, |
84
|
|
|
InputOption::VALUE_NONE, |
85
|
|
|
'Displays a list of file names diff (default)' |
86
|
|
|
); |
87
|
|
|
$this->addOption( |
88
|
|
|
self::CHECK_OPTION, |
89
|
|
|
'c', |
90
|
|
|
InputOption::VALUE_OPTIONAL, |
91
|
|
|
'Verify the authenticity of the contents between the two PHARs with the given hash function.', |
92
|
|
|
'sha384' |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
100
|
|
|
{ |
101
|
|
|
$io = new SymfonyStyle($input, $output); |
102
|
|
|
|
103
|
|
|
(new PhpSettingsHandler(new ConsoleLogger($output)))->check(); |
104
|
|
|
|
105
|
|
|
$paths = [ |
106
|
|
|
$input->getArgument(self::FIRST_PHAR_ARG), |
107
|
|
|
$input->getArgument(self::SECOND_PHAR_ARG), |
108
|
|
|
]; |
109
|
|
|
|
110
|
|
|
Assertion::allFile($paths); |
|
|
|
|
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$diff = new PharDiff(...$paths); |
114
|
|
|
} catch (Throwable $throwable) { |
115
|
|
|
if ($output->isDebug()) { |
116
|
|
|
throw $throwable; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$io->writeln( |
120
|
|
|
sprintf( |
121
|
|
|
'<error>Could not check the PHARs: %s</error>', |
122
|
|
|
$throwable->getMessage() |
123
|
|
|
) |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return 1; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$result1 = $this->compareArchives($diff, $io); |
130
|
|
|
$result2 = $this->compareContents($input, $diff, $io); |
131
|
|
|
|
132
|
|
|
return $result1 + $result2; |
133
|
|
|
if ($input->hasParameterOption(['-c', '--check'])) { |
|
|
|
|
134
|
|
|
return $diff->listChecksums($input->getOption(self::CHECK_OPTION) ?? 'sha384'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($input->getOption(self::GNU_DIFF_OPTION)) { |
138
|
|
|
$diffResult = $diff->gnuDiff(); |
139
|
|
|
} elseif ($input->getOption(self::GIT_DIFF_OPTION)) { |
140
|
|
|
$diffResult = $diff->gitDiff(); |
141
|
|
|
} else { |
142
|
|
|
return $this->renderListDiff($diff, $io); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if (null === $diffResult) { |
146
|
|
|
$io->success('No differences encountered.'); |
147
|
|
|
|
148
|
|
|
return 0; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$io->writeln($diffResult); |
152
|
|
|
|
153
|
|
|
return 1; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function compareArchives(PharDiff $diff, SymfonyStyle $io): int |
157
|
|
|
{ |
158
|
|
|
$io->comment('<info>Comparing the two archives... (do not check the signatures)</info>'); |
159
|
|
|
|
160
|
|
|
$pharInfoA = new PharInfo($diff->getPharA()->phar->getPath()); |
161
|
|
|
$pharInfoB = new PharInfo($diff->getPharB()->phar->getPath()); |
162
|
|
|
|
163
|
|
|
if ($pharInfoA->equals($pharInfoB)) { |
164
|
|
|
$io->success('The two archives are identical'); |
165
|
|
|
|
166
|
|
|
return 0; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->renderArchive( |
170
|
|
|
$diff->getPharA()->getFileName(), |
171
|
|
|
$pharInfoA, |
172
|
|
|
$io |
173
|
|
|
); |
174
|
|
|
|
175
|
|
|
$io->newLine(); |
176
|
|
|
|
177
|
|
|
$this->renderArchive( |
178
|
|
|
$diff->getPharB()->getFileName(), |
179
|
|
|
$pharInfoA, |
180
|
|
|
$io |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
return 1; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
private function compareContents(InputInterface $input, PharDiff $diff, SymfonyStyle $io): int |
187
|
|
|
{ |
188
|
|
|
$io->comment('<info>Comparing the two archives contents...</info>'); |
189
|
|
|
|
190
|
|
|
if ($input->hasParameterOption(['-c', '--check'])) { |
191
|
|
|
return $diff->listChecksums($input->getOption(self::CHECK_OPTION) ?? 'sha384'); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
if ($input->getOption(self::GNU_DIFF_OPTION)) { |
195
|
|
|
$diffResult = $diff->gnuDiff(); |
196
|
|
|
} elseif ($input->getOption(self::GIT_DIFF_OPTION)) { |
197
|
|
|
$diffResult = $diff->gitDiff(); |
198
|
|
|
} else { |
199
|
|
|
$diffResult = $diff->listDiff($diff, $io); |
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (null === $diffResult || [[], []] === $diffResult) { |
203
|
|
|
$io->success('The contents are identical'); |
204
|
|
|
|
205
|
|
|
return 0; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if (is_string($diffResult)) { |
209
|
|
|
// Git or GNU diff: we don't have much control on the format |
210
|
|
|
$io->writeln($diffResult); |
211
|
|
|
|
212
|
|
|
return 1; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$io->writeln(sprintf( |
216
|
|
|
'--- Files present in "%s" but not in "%s"', |
217
|
|
|
$diff->getPharA()->getFileName(), |
218
|
|
|
$diff->getPharB()->getFileName() |
219
|
|
|
)); |
220
|
|
|
$io->writeln(sprintf( |
221
|
|
|
'+++ Files present in "%s" but not in "%s"', |
222
|
|
|
$diff->getPharB()->getFileName(), |
223
|
|
|
$diff->getPharA()->getFileName() |
224
|
|
|
)); |
225
|
|
|
|
226
|
|
|
$io->newLine(); |
227
|
|
|
|
228
|
|
|
$renderPaths = static function (string $symbol, array $paths, SymfonyStyle $io): void { |
229
|
|
|
foreach ($paths as $path) { |
230
|
|
|
$io->writeln(sprintf( |
231
|
|
|
'%s %s', |
232
|
|
|
$symbol, |
233
|
|
|
$path |
234
|
|
|
)); |
235
|
|
|
} |
236
|
|
|
}; |
237
|
|
|
|
238
|
|
|
$renderPaths('-', $diffResult[0], $io); |
239
|
|
|
$renderPaths('+', $diffResult[1], $io); |
240
|
|
|
|
241
|
|
|
$io->error(sprintf( |
242
|
|
|
'%d file(s) difference', |
243
|
|
|
count($diffResult[0]) + count($diffResult[1]) |
244
|
|
|
)); |
245
|
|
|
|
246
|
|
|
return 1; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
private function renderArchive(string $fileName, PharInfo $pharInfo, SymfonyStyle $io): void |
250
|
|
|
{ |
251
|
|
|
$io->writeln( |
252
|
|
|
sprintf( |
253
|
|
|
'<comment>Archive: </comment><fg=cyan;options=bold>%s</>', |
254
|
|
|
$fileName |
255
|
|
|
) |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
PharInfoRenderer::renderCompression($pharInfo, $io); |
259
|
|
|
// Omit the signature |
260
|
|
|
PharInfoRenderer::renderMetadata($pharInfo, $io); |
261
|
|
|
PharInfoRenderer::renderContentsSummary($pharInfo, $io); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|