-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUpdateCommand.php
More file actions
81 lines (70 loc) · 3.15 KB
/
UpdateCommand.php
File metadata and controls
81 lines (70 loc) · 3.15 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
<?php
namespace GitScan\Command;
use GitScan\Util\Filesystem;
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 UpdateCommand 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('update')
->setAliases(array('up'))
->setDescription('Execute fast-forward merges on all nested repositories')
->setHelp('Execute fast-forward merges on all nested repositories (which are already amenable to fast-forwarding)')
->addOption('max-depth', NULL, InputOption::VALUE_REQUIRED, 'Limit the depth of the search', -1)
->addArgument('path', InputArgument::IS_ARRAY, 'The local base path to search', array(getcwd()));
}
protected function initialize(InputInterface $input, OutputInterface $output) {
$input->setArgument('path', $this->fs->toAbsolutePaths($input->getArgument('path')));
$this->fs->validateExists($input->getArgument('path'));
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$statusCode = 0;
$output->writeln("<comment>[[ Finding repositories ]]</comment>");
$scanner = new \GitScan\GitRepoScanner();
$gitRepos = $scanner->scan($input->getArgument('path'), $input->getOption('max-depth'));
$output->writeln("<comment>[[ Fast-forwarding ]]</comment>");
foreach ($gitRepos as $gitRepo) {
/** @var \GitScan\GitRepo $gitRepo */
$path = $this->fs->formatPrettyPath($gitRepo->getPath(), $input->getArgument('path'));
if ($gitRepo->getUpstreamBranch() === NULL) {
$output->writeln("<comment>Skip <info>$path</info>: No upstream tracking branch</comment>");
}
elseif (!$gitRepo->isLocalFastForwardable()) {
$output->writeln("<comment>Skip <info>$path</info>: Cannot be fast-forwarded</comment>");
}
else {
$output->writeln("<comment>Fast-forward <info>$path</info> (<info>{$gitRepo->getLocalBranch()}</info> <= <info>{$gitRepo->getUpstreamBranch()}</info>)...</comment>");
$process = $gitRepo->command('git pull --ff-only');
$process->run();
if (!$process->isSuccessful()) {
$output->writeln("<error>Failed to update {$gitRepo->getPath()}/<error>");
if ($process->getOutput()) {
$output->writeln("//---------- BEGIN STDOUT ----------\\\\");
$output->writeln($process->getOutput(), OutputInterface::OUTPUT_RAW);
$output->writeln("\\\\----------- END STDOUT -----------//");
}
if ($process->getErrorOutput()) {
$output->writeln("//---------- BEGIN STDERR ----------\\\\");
$output->writeln($process->getErrorOutput(), OutputInterface::OUTPUT_RAW);
$output->writeln("\\\\----------- END STDERR -----------//");
}
$statusCode = 1;
}
}
}
return $statusCode;
}
}