ChangeBranchTask::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Magallanes package.
5
 *
6
 * (c) Andrés Montañez <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Mage\Task\BuiltIn\Git;
13
14
use Mage\Task\Exception\SkipException;
15
use Symfony\Component\Process\Process;
16
use Mage\Task\AbstractTask;
17
18
/**
19
 * Git Task - Checkout Branch
20
 *
21
 * @author Andrés Montañez <[email protected]>
22
 */
23
class ChangeBranchTask extends AbstractTask
24
{
25 48
    public function getName(): string
26
    {
27 48
        return 'git/change-branch';
28
    }
29
30 32
    public function getDescription(): string
31
    {
32 32
        $options = $this->getOptions();
33 32
        $tag = $options['tag'];
34 32
        $branch = $options['branch'];
35
36 32
        if ($this->runtime->getVar('git_revert_branch', null)) {
37 30
            $branch = $this->runtime->getVar('git_revert_branch');
38
        }
39
40 32
        if ($tag) {
41 1
            return sprintf('[Git] Checkout Tag (%s)', $tag);
42
        }
43
44 31
        return sprintf('[Git] Change Branch (%s)', $branch);
45
    }
46
47 32
    public function execute(): bool
48
    {
49 32
        $options = $this->getOptions();
50
        /** @var string|bool */
51 32
        $branch = $this->runtime->getVar('git_revert_branch', null);
52
53 32
        if (!$branch) {
54 32
            $cmdGetCurrent = sprintf('%s branch | grep "*"', $options['path']);
55
56
            /** @var Process $process */
57 32
            $process = $this->runtime->runLocalCommand($cmdGetCurrent);
58 32
            if (!$process->isSuccessful()) {
59 1
                return false;
60
            }
61
62 31
            $currentBranch = str_replace('* ', '', trim($process->getOutput()));
63 31
            if ($currentBranch === $options['branch']) {
64 1
                throw new SkipException();
65
            }
66
67 30
            $branch = $options['tag'] ? $options['tag'] : $options['branch'];
68 30
            $this->runtime->setVar('git_revert_branch', $currentBranch);
69
        }
70
71 30
        $cmdChange = sprintf('%s checkout %s', $options['path'], $branch);
72
73
        /** @var Process $process */
74 30
        $process = $this->runtime->runLocalCommand($cmdChange);
75 30
        return $process->isSuccessful();
76
    }
77
78
    /**
79
     * @return array<string, string>
80
     */
81 32
    protected function getOptions(): array
82
    {
83 32
        $tag = $this->runtime->getEnvOption('tag', false);
84 32
        $branch = $this->runtime->getEnvOption('branch', 'master');
85 32
        $options = array_merge(
86 32
            ['path' => 'git', 'branch' => $branch, 'tag' => $tag],
87 32
            $this->options
88
        );
89
90 32
        return $options;
91
    }
92
}
93