-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathBranchCommand.php
More file actions
139 lines (118 loc) · 5.31 KB
/
BranchCommand.php
File metadata and controls
139 lines (118 loc) · 5.31 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace GitScan\Command;
use GitScan\GitRepo;
use GitScan\Util\Filesystem;
use GitScan\Util\ProcessBatch;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\Question;
class BranchCommand 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('branch')
->setDescription('Create branches across repos')
->addOption('path', NULL, InputOption::VALUE_REQUIRED, 'The local base path to search', getcwd())
->addOption('max-depth', NULL, InputOption::VALUE_REQUIRED, 'Limit the depth of the search', -1)
->addOption('prefix', 'p', InputOption::VALUE_NONE, 'Autodetect prefixed variations')
->addOption('delete', 'd', InputOption::VALUE_NONE, 'Delete fully merged branches')
->addOption('force-delete', 'D', InputOption::VALUE_NONE, 'Delete branch (even if not merged)')
->addOption('dry-run', 'T', InputOption::VALUE_NONE, 'Display what would be done')
->addArgument('branchName', InputArgument::REQUIRED, 'The name of the new branch(es)')
->addArgument('head', InputArgument::OPTIONAL, 'The name of the head(s) to use for new branch(s). *Must* be a branch name.');
}
protected function initialize(InputInterface $input, OutputInterface $output) {
$input->setOption('path', $this->fs->toAbsolutePath($input->getOption('path')));
$this->fs->validateExists($input->getOption('path'));
}
protected function execute(InputInterface $input, OutputInterface $output): int {
if ($input->getOption('delete') || $input->getOption('force-delete')) {
return $this->executeDelete($input, $output);
}
else {
return $this->executeCreate($input, $output);
}
}
protected function executeCreate(InputInterface $input, OutputInterface $output): int {
if (!$input->getArgument('head')) {
throw new \RuntimeException("Missing argument \"head\". Please specify the name of original base branch.");
}
$helper = $this->getHelper('question');
$scanner = new \GitScan\GitRepoScanner();
$gitRepos = $scanner->scan($input->getOption('path'), $input->getOption('max-depth'));
$batch = new ProcessBatch('Creating branch(es)...');
$self = $this;
$g = new \GitScan\GitBranchGenerator($gitRepos);
$g->generate(
$input->getArgument('head'),
$input->getArgument('branchName'),
$input->getOption('prefix'),
function (GitRepo $gitRepo, $oldBranch, $newBranch) use ($input, $output, $helper, &$batch, $self) {
$relPath = $self->fs->makePathRelative($gitRepo->getPath(), $input->getOption('path'));
$question = new ChoiceQuestion("\n<comment>In \"<info>{$relPath}</info>\", found existing branch \"<info>$oldBranch</info>\". Create a new branch \"<info>$newBranch</info>\"?</comment>",
array("y" => "yes (default)", "n" => "no", "c" => "customize"),
"y"
);
$mode = $helper->ask($input, $output, $question);
if ($mode === 'n') {
return;
}
if ($mode === 'c') {
$newBranch = $helper->ask($input, $output, new Question("<comment>Enter the new branch name:</comment> "));
if (!$newBranch) {
$output->writeln("Skipped");
return;
}
}
$label = "In \"<info>{$relPath}</info>\", make branch \"<info>$newBranch</info>\" from \"<info>$oldBranch</info>\"";
$batch->add($label, $gitRepo->command(sprintf(
"git branch %s %s",
escapeshellarg($newBranch),
escapeshellarg($oldBranch)
)));
});
$batch->runAllOk($output, $input->getOption('dry-run'));
return 0;
}
protected function executeDelete(InputInterface $input, OutputInterface $output): int {
$helper = $this->getHelper('question');
$scanner = new \GitScan\GitRepoScanner();
$gitRepos = $scanner->scan($input->getOption('path'), $input->getOption('max-depth'));
$batch = new ProcessBatch('Deleting branch(es)...');
$branchName = $input->getArgument('branchName');
$branchQuoted = preg_quote($branchName, '/');
foreach ($gitRepos as $gitRepo) {
/** @var \GitScan\GitRepo $gitRepo */
$relPath = $this->fs->makePathRelative($gitRepo->getPath(), $input->getOption('path'));
$branches = $gitRepo->getBranches();
$matches = array();
if ($input->getOption('prefix')) {
$matches = preg_grep("/[-_]$branchQuoted\$/", $branches);
}
if (in_array($branchName, $branches)) {
$matches[] = $branchName;
}
// TODO: Verify that user wants to delete these.
foreach ($matches as $match) {
$label = "In \"<info>{$relPath}</info>\", delete branch \"<info>$match</info>\" .";
$modifier = $input->getOption('force-delete') ? '-D' : '-d';
$batch->add($label, $gitRepo->command("git branch $modifier " . escapeshellarg($match)));
}
}
$batch->runAllOk($output, $input->getOption('dry-run'));
return 0;
}
}