|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Storeman\Cli\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Storeman\Index\Comparison\IndexObjectComparison; |
|
6
|
|
|
use Storeman\Index\Index; |
|
7
|
|
|
use Storeman\Index\IndexObject; |
|
8
|
|
|
use Storeman\Storeman; |
|
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The DiffCommand can be used to display the difference of two different states |
|
16
|
|
|
*/ |
|
17
|
|
|
class DiffCommand extends AbstractCommand |
|
18
|
|
|
{ |
|
19
|
|
|
protected function configure() |
|
20
|
|
|
{ |
|
21
|
|
|
parent::configure(); |
|
22
|
|
|
|
|
23
|
|
|
$this->setName('diff'); |
|
24
|
|
|
$this->setDescription('Displays the difference of a revision and another revision or the current local state.'); |
|
25
|
|
|
$this->addArgument('revision', InputArgument::OPTIONAL, 'Revision for comparison. Defaults to the last revision.'); |
|
26
|
|
|
$this->addArgument('compareTo', InputArgument::OPTIONAL, 'Optional other revision number for index identification to compare against. Defaults to none for comparison with the current local state.'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function executeConfigured(InputInterface $input, OutputInterface $output, Storeman $storeman): int |
|
30
|
|
|
{ |
|
31
|
|
|
$revision = intval($input->getArgument('revision')) ?: $storeman->getLastRevision(); |
|
32
|
|
|
|
|
33
|
|
|
if ($revision === null) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->consoleStyle->writeln("Could not find any past synchronizations to compare against."); |
|
36
|
|
|
|
|
37
|
|
|
return 0; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$vaults = $storeman->getVaultContainer(); |
|
41
|
|
|
$vault = $vaults->getPrioritizedVault($vaults->getVaultsHavingRevision($revision)); |
|
42
|
|
|
|
|
43
|
|
|
if ($vault === null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->consoleStyle->error("Could not find requested revision {$revision} in any vault."); |
|
46
|
|
|
|
|
47
|
|
|
return 1; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$index = $vault->loadRemoteIndex($revision); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$compareTo = intval($input->getArgument('compareTo')) ?: null; |
|
54
|
|
|
|
|
55
|
|
|
if ($compareTo === 0) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->consoleStyle->error("Invalid argument compareTo given."); |
|
58
|
|
|
|
|
59
|
|
|
return 1; |
|
60
|
|
|
} |
|
61
|
|
|
elseif ($compareTo === null) |
|
62
|
|
|
{ |
|
63
|
|
|
$compareToIndex = $storeman->getLocalIndex(); |
|
64
|
|
|
} |
|
65
|
|
|
else |
|
66
|
|
|
{ |
|
67
|
|
|
$vault = $vaults->getPrioritizedVault($vaults->getVaultsHavingRevision($compareTo)); |
|
68
|
|
|
|
|
69
|
|
|
if ($vault === null) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->consoleStyle->error("Could not find revision {$compareTo} in any vault."); |
|
72
|
|
|
|
|
73
|
|
|
return 1; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$compareToIndex = $vault->loadRemoteIndex($compareTo); |
|
77
|
|
|
|
|
78
|
|
|
assert($compareToIndex instanceof Index); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$diff = $index->getDifference($compareToIndex, IndexObject::CMP_IGNORE_BLOBID | IndexObject::CMP_IGNORE_INODE); |
|
83
|
|
|
|
|
84
|
|
|
if ($diff->count() > 0) |
|
85
|
|
|
{ |
|
86
|
|
|
$output->writeln(sprintf("Found %d difference(s):\n", $diff->count() ?: 'No')); |
|
87
|
|
|
|
|
88
|
|
|
$table = new Table($output); |
|
89
|
|
|
$table->setStyle('compact'); |
|
90
|
|
|
$table->setHeaders(['Path', "r{$revision}", $compareTo ? "r{$compareTo}" : "local"]); |
|
91
|
|
|
$table->addRows(array_map(function(IndexObjectComparison $difference) { |
|
92
|
|
|
|
|
93
|
|
|
return [ |
|
94
|
|
|
$difference->getRelativePath(), |
|
95
|
|
|
$this->renderIndexObjectColumn($difference->getIndexObjectA()), |
|
96
|
|
|
$this->renderIndexObjectColumn($difference->getIndexObjectB()), |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
}, iterator_to_array($diff->getIterator()))); |
|
100
|
|
|
|
|
101
|
|
|
$table->render(); |
|
102
|
|
|
|
|
103
|
|
|
$output->write("\n"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
else |
|
107
|
|
|
{ |
|
108
|
|
|
$output->writeln("No differences found."); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return 0; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
protected function renderIndexObjectColumn(?IndexObject $indexObject): string |
|
115
|
|
|
{ |
|
116
|
|
|
if ($indexObject === null) |
|
117
|
|
|
{ |
|
118
|
|
|
return '-'; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return "mtime: {$indexObject->getMtime()}, size: {$indexObject->getSize()}"; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|