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