-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDiffCommand.php
More file actions
109 lines (94 loc) · 3.49 KB
/
DiffCommand.php
File metadata and controls
109 lines (94 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace GitScan\Command;
use GitScan\CheckoutDocument;
use GitScan\DiffReport;
use GitScan\GitFormatter\PlainFormatter;
use GitScan\GitFormatter\HtmlFormatter;
use GitScan\Util\Filesystem;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class DiffCommand extends BaseCommand {
/**
* @var \GitScan\Util\Filesystem
*/
public $fs;
/**
* @param string|NULL $name
*/
public function __construct($name = NULL) {
$this->fs = new Filesystem();
parent::__construct($name);
}
protected function configure() {
$this
->setName('diff')
->setDescription('Compare the commits/revisions in different source trees')
->setHelp('Compare the commits/revisions in different source trees')
->addArgument('from', InputArgument::REQUIRED, 'Path to the project folder or JSON export')
->addArgument('to', InputArgument::REQUIRED, 'Path to the project folder or JSON export')
->addOption('max-depth', NULL, InputOption::VALUE_REQUIRED, 'Limit the depth of the search', -1)
->addOption('format', NULL, InputOption::VALUE_REQUIRED, 'Output format (text|html|json)', 'text');
}
protected function initialize(InputInterface $input, OutputInterface $output) {
$input->setArgument('from', $this->fs->toAbsolutePath($input->getArgument('from')));
$this->fs->validateExists($input->getArgument('from'));
$input->setArgument('to', $this->fs->toAbsolutePath($input->getArgument('to')));
$this->fs->validateExists($input->getArgument('to'));
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$fromDoc = $this->getCheckoutDocument($input->getArgument('from'), $input->getOption('max-depth'));
$toDoc = $this->getCheckoutDocument($input->getArgument('to'), $input->getOption('max-depth'));
$report = new DiffReport(
$fromDoc,
$toDoc,
$input->getOption('format') == 'html' ? new HtmlFormatter() : new PlainFormatter()
);
switch ($input->getOption('format')) {
case 'json':
$output->write(json_encode($report->getRows()));
break;
case 'text':
$output->writeln(sprintf("Compare <info>%s</info> to <info>%s</info>",
$input->getArgument('from'),
$input->getArgument('to')
));
$rows = array();
foreach ($report->getRows() as $row) {
$rows[] = array($row['status'], $row['path'], $row['from'], $row['to'], $row['changes']);
}
$table = new Table($output);
$table
->setHeaders(array(' ', 'Path', 'From', 'To', 'Changes'))
->setRows($rows);
$table->render();
break;
case 'html':
// TODO
default:
$output->writeln('<error>Unsupported output format</error>');
return 1;
}
return 0;
}
/**
* @param string $path path to a directory or JSON file
* @param int $maxDepth
* @return \GitScan\CheckoutDocument
*/
protected function getCheckoutDocument($path, $maxDepth = -1) {
if (is_dir($path)) {
$scanner = new \GitScan\GitRepoScanner();
$gitRepos = $scanner->scan($path, $maxDepth);
return CheckoutDocument::create($path)
->importRepos($gitRepos);
}
else {
$json = file_get_contents($path);
return CheckoutDocument::create(NULL)
->importJson($json);
}
}
}