BranchCommand::create()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/**
4
 * GitElephant - An abstraction layer for git written in PHP
5
 * Copyright (C) 2013  Matteo Giachino
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
19
 */
20
21
namespace GitElephant\Command;
22
23
use GitElephant\Repository;
24
25
/**
26
 * Branch command generator
27
 *
28
 * @author Matteo Giachino <[email protected]>
29
 */
30
class BranchCommand extends BaseCommand
31
{
32
    public const BRANCH_COMMAND = 'branch';
33
34
    /**
35
     * constructor
36
     *
37
     * @param \GitElephant\Repository $repo The repository object this command
38
     *                                      will interact with
39
     */
40 43
    public function __construct(Repository $repo = null)
41
    {
42 43
        parent::__construct($repo);
43 43
    }
44
45
    /**
46
     * Locate branches that contain a reference
47
     *
48
     * @param string $reference reference
49
     *
50
     * @throws \RuntimeException
51
     * @return string the command
52
     */
53
    public function contains(string $reference): string
54
    {
55
        $this->clearAll();
56
        $this->addCommandName(self::BRANCH_COMMAND);
57
        $this->addCommandArgument('--contains');
58
        $this->addCommandSubject($reference);
59
60
        return $this->getCommand();
61
    }
62
63
    /**
64
     * Create a new branch
65
     *
66
     * @param string      $name       The new branch name
67
     * @param string|null $startPoint the new branch start point.
68
     *
69
     * @throws \RuntimeException
70
     * @return string the command
71
     */
72 34
    public function create(string $name, string $startPoint = null): string
73
    {
74 34
        $this->clearAll();
75 34
        $this->addCommandName(self::BRANCH_COMMAND);
76 34
        $this->addCommandSubject($name);
77 34
        if (null !== $startPoint) {
78 5
            $this->addCommandSubject2($startPoint);
79
        }
80
81 34
        return $this->getCommand();
82
    }
83
84
    /**
85
     * Lists branches
86
     *
87
     * @param bool $all    lists all remotes
88
     * @param bool $simple list only branch names
89
     *
90
     * @throws \RuntimeException
91
     * @return string the command
92
     */
93 43
    public function listBranches(bool $all = false, bool $simple = false): string
94
    {
95 43
        $this->clearAll();
96 43
        $this->addCommandName(self::BRANCH_COMMAND);
97 43
        if (!$simple) {
98 43
            $this->addCommandArgument('-v');
99
        }
100 43
        $this->addCommandArgument('--no-color');
101 43
        $this->addCommandArgument('--no-abbrev');
102 43
        if ($all) {
103 3
            $this->addCommandArgument('-a');
104
        }
105
106 43
        return $this->getCommand();
107
    }
108
109
    /**
110
     * Lists branches
111
     *
112
     * @deprecated This method uses an unconventional name but is being left in
113
     *             place to remain compatible with existing code relying on it.
114
     *             New code should be written to use listBranches().
115
     *
116
     * @param bool $all    lists all remotes
117
     * @param bool $simple list only branch names
118
     *
119
     * @throws \RuntimeException
120
     * @return string the command
121
     */
122 1
    public function lists($all = false, bool $simple = false): string
123
    {
124 1
        return $this->listBranches($all, $simple);
125
    }
126
127
    /**
128
     * get info about a single branch
129
     *
130
     * @param string $name    The branch name
131
     * @param bool   $all     lists all remotes
132
     * @param bool   $simple  list only branch names
133
     * @param bool   $verbose verbose, show also the upstream branch
134
     *
135
     * @throws \RuntimeException
136
     * @return string
137
     */
138 1
    public function singleInfo(string $name, bool $all = false, bool $simple = false, bool $verbose = false): string
139
    {
140 1
        $this->clearAll();
141 1
        $this->addCommandName(self::BRANCH_COMMAND);
142 1
        if (!$simple) {
143 1
            $this->addCommandArgument('-v');
144
        }
145 1
        $this->addCommandArgument('--list');
146 1
        $this->addCommandArgument('--no-color');
147 1
        $this->addCommandArgument('--no-abbrev');
148 1
        if ($all) {
149 1
            $this->addCommandArgument('-a');
150
        }
151 1
        if ($verbose) {
152 1
            $this->addCommandArgument('-vv');
153
        }
154 1
        $this->addCommandSubject($name);
155
156 1
        return $this->getCommand();
157
    }
158
159
    /**
160
     * Delete a branch by its name
161
     *
162
     * @param string $name  The branch to delete
163
     * @param bool   $force Force the delete
164
     *
165
     * @throws \RuntimeException
166
     * @return string the command
167
     */
168 1
    public function delete(string $name, bool $force = false): string
169
    {
170 1
        $arg = $force ? '-D' : '-d';
171 1
        $this->clearAll();
172 1
        $this->addCommandName(self::BRANCH_COMMAND);
173 1
        $this->addCommandArgument($arg);
174 1
        $this->addCommandSubject($name);
175
176 1
        return $this->getCommand();
177
    }
178
}
179