Skip to content

Commit 6016337

Browse files
committed
Display list of available branches during selection. Current branch will appear as default
1 parent 4c48721 commit 6016337

File tree

1 file changed

+97
-16
lines changed

1 file changed

+97
-16
lines changed

src/Command/SiteCheckoutCommand.php

Lines changed: 97 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class SiteCheckoutCommand extends SiteBaseCommand {
3535
*/
3636
protected $branch;
3737

38+
/**
39+
* Stores current branch of the checked out code.
40+
*
41+
* @var array currentBranch.
42+
*/
43+
protected $currentBranch;
44+
3845
/**
3946
* {@inheritdoc}
4047
*/
@@ -64,23 +71,31 @@ protected function configure() {
6471
protected function interact(InputInterface $input, OutputInterface $output) {
6572
parent::interact($input, $output);
6673

74+
// Validate repo.
75+
$this->_validateRepo();
76+
77+
$remoteBranches = $this->getRemoteBranches();
78+
$defaultBranch = $this->getDefaultBranch();
79+
$this->currentBranch = $this->getCurrentBranch();
80+
6781
$branch = $input->getOption('branch');
6882
if (!$branch) {
69-
// Typical branches.
70-
$branches = ['8.x', 'master'];
7183

72-
if (isset($this->config['repo']['branch'])) {
73-
// Populate branches from config.
74-
$siteBranch = $this->config['repo']['branch'];
75-
}
84+
$options = array_values(array_unique(array_merge(
85+
['8.x'],
86+
[$defaultBranch],
87+
[$this->currentBranch],
88+
$remoteBranches
89+
)));
90+
91+
$branch = $this->io->choice(
92+
$this->trans('Select a branch'),
93+
$options,
94+
isset($this->currentBranch) ? $this->currentBranch : $defaultBranch,
95+
TRUE
96+
);
7697

77-
$branch = $this->io->choice(
78-
$this->trans('Select a branch'),
79-
array_values(array_unique(array_merge([$siteBranch], $branches))),
80-
isset($siteBranch) ? $siteBranch : '8.x',
81-
true
82-
);
83-
$input->setOption('branch', reset($branch));
98+
$input->setOption('branch', reset($branch));
8499
}
85100
}
86101

@@ -90,12 +105,14 @@ protected function interact(InputInterface $input, OutputInterface $output) {
90105
protected function execute(InputInterface $input, OutputInterface $output) {
91106
parent::execute($input, $output);
92107

93-
// Validate repo.
94-
$this->_validateRepo();
95-
96108
// Validate branch.
97109
$this->_validateBranch($input);
98110

111+
if ($this->branch == $this->currentBranch) {
112+
$this->io->commentBlock('Current branch selected, skipping checkout command.');
113+
return;
114+
}
115+
99116
$this->io->comment(sprintf('Checking out %s (%s) on %s',
100117
$this->siteName,
101118
$this->branch,
@@ -273,4 +290,68 @@ protected function gitCheckout($branch, $destination) {
273290

274291
return TRUE;
275292
}
293+
294+
/**
295+
* Pulls a list of branches from remote.
296+
*
297+
* @param $repo
298+
*
299+
* @return mixed
300+
* @throws SiteCommandException
301+
*/
302+
protected function getRemoteBranches() {
303+
$command = sprintf('git ls-remote --heads %s',
304+
$this->repo['url']
305+
);
306+
307+
$shellProcess = $this->getShellProcess();
308+
309+
if ($shellProcess->exec($command, TRUE)) {
310+
preg_match_all("|refs/heads/(.*)|", $shellProcess->getOutput(), $matches);
311+
if (!empty($matches[1] && is_array($matches[1]))) {
312+
return $matches[1];
313+
}
314+
}
315+
else {
316+
throw new SiteCommandException($shellProcess->getOutput());
317+
318+
}
319+
}
320+
321+
/**
322+
* Helper to retrieve the default branch from yml.
323+
*
324+
* @return mixed
325+
*/
326+
protected function getDefaultBranch() {
327+
// Get branch from yml.
328+
if (isset($this->config['repo']['branch'])) {
329+
// Populate branches from config.
330+
return $this->config['repo']['branch'];
331+
}
332+
}
333+
334+
/**
335+
* Helper to retrieve the current working branch on the site's directory.
336+
*
337+
* @return mixed
338+
*/
339+
protected function getCurrentBranch() {
340+
if ($this->fileExists($this->destination)) {
341+
// Get branch from site directory.
342+
$command = sprintf('cd %s && git branch',
343+
$this->shellPath($this->destination)
344+
);
345+
346+
$shellProcess = $this->getShellProcess();
347+
348+
if ($shellProcess->exec($command, TRUE)) {
349+
preg_match_all("|\*\s(.*)|", $shellProcess->getOutput(), $matches);
350+
if (!empty($matches[1] && is_array($matches[1]))) {
351+
return reset($matches[1]);
352+
}
353+
}
354+
}
355+
}
356+
276357
}

0 commit comments

Comments
 (0)