1 | <?php |
||
32 | class Branch extends NodeObject implements TreeishInterface |
||
33 | { |
||
34 | /** |
||
35 | * current checked out branch |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $current = false; |
||
40 | |||
41 | /** |
||
42 | * branch name |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | private $name; |
||
|
|||
47 | |||
48 | /** |
||
49 | * sha |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | private $sha; |
||
54 | |||
55 | /** |
||
56 | * branch comment |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | private $comment; |
||
61 | |||
62 | /** |
||
63 | * the full branch reference |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $fullRef; |
||
68 | |||
69 | /** |
||
70 | * Creates a new branch on the repository and returns it |
||
71 | * |
||
72 | * @param \GitElephant\Repository $repository repository instance |
||
73 | * @param string $name branch name |
||
74 | * @param string $startPoint branch to start from |
||
75 | * |
||
76 | * @throws \RuntimeException |
||
77 | * @throws \Symfony\Component\Process\Exception\LogicException |
||
78 | * @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
||
79 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
80 | * @return \GitElephant\Objects\Branch |
||
81 | */ |
||
82 | 33 | public static function create( |
|
83 | Repository $repository, |
||
84 | string $name, |
||
85 | string $startPoint = null |
||
86 | ): \GitElephant\Objects\Branch { |
||
87 | $repository |
||
88 | 33 | ->getCaller() |
|
89 | 33 | ->execute(BranchCommand::getInstance($repository)->create($name, $startPoint)); |
|
90 | |||
91 | 33 | return new self($repository, $name); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * static generator to generate a single commit from output of command.show service |
||
96 | * |
||
97 | * @param \GitElephant\Repository $repository repository |
||
98 | * @param string $outputLine output line |
||
99 | * |
||
100 | * @throws \InvalidArgumentException |
||
101 | * @return Branch |
||
102 | */ |
||
103 | 13 | public static function createFromOutputLine(Repository $repository, string $outputLine): \GitElephant\Objects\Branch |
|
104 | { |
||
105 | 13 | $matches = static::getMatches($outputLine); |
|
106 | 13 | $branch = new self($repository, $matches[1]); |
|
107 | 13 | $branch->parseOutputLine($outputLine); |
|
108 | |||
109 | 13 | return $branch; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @param \GitElephant\Repository $repository repository instance |
||
114 | * @param string|TreeishInterface $name branch name |
||
115 | * @param bool $create like checkout -b, create a branch and check it out |
||
116 | * |
||
117 | * @throws \RuntimeException |
||
118 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
119 | * @return Branch |
||
120 | */ |
||
121 | 2 | public static function checkout(Repository $repository, $name, $create = false): \GitElephant\Objects\Branch |
|
122 | { |
||
123 | 2 | $branch = $create ? self::create($repository, $name) : new self($repository, $name); |
|
124 | |||
125 | 1 | $repository->checkout($branch); |
|
126 | |||
127 | 1 | return $branch; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * Class constructor |
||
132 | * |
||
133 | * @param \GitElephant\Repository $repository repository instance |
||
134 | * @param string $name branch name |
||
135 | * |
||
136 | * @throws \RuntimeException |
||
137 | * @throws \InvalidArgumentException |
||
138 | * @throws \GitElephant\Exception\InvalidBranchNameException |
||
139 | * @throws \Symfony\Component\Process\Exception\RuntimeException |
||
140 | */ |
||
141 | 42 | public function __construct(Repository $repository, string $name) |
|
148 | |||
149 | /** |
||
150 | * get the branch properties from command |
||
151 | * |
||
152 | * @throws \InvalidArgumentException |
||
153 | */ |
||
154 | 42 | private function createFromCommand() |
|
170 | |||
171 | /** |
||
172 | * parse an output line from the BranchCommand::singleInfo command |
||
173 | * |
||
174 | * @param string $branchString an output line for a branch |
||
175 | * |
||
176 | * @throws \InvalidArgumentException |
||
177 | */ |
||
178 | 42 | public function parseOutputLine(string $branchString): void |
|
192 | |||
193 | /** |
||
194 | * get the matches from an output line |
||
195 | * |
||
196 | * @param string $branchString branch line output |
||
197 | * |
||
198 | * @throws \InvalidArgumentException |
||
199 | * @return array |
||
200 | */ |
||
201 | 45 | public static function getMatches(string $branchString): array |
|
220 | |||
221 | /** |
||
222 | * toString magic method |
||
223 | * |
||
224 | * @return string the sha |
||
225 | */ |
||
226 | 4 | public function __toString(): string |
|
230 | |||
231 | /** |
||
232 | * name setter |
||
233 | * |
||
234 | * @param string $name the branch name |
||
235 | */ |
||
236 | public function setName(string $name): void |
||
240 | |||
241 | /** |
||
242 | * name setter |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | 14 | public function getName(): string |
|
250 | |||
251 | /** |
||
252 | * sha setter |
||
253 | * |
||
254 | * @param string $sha the sha of the branch |
||
255 | */ |
||
256 | public function setSha(string $sha): void |
||
260 | |||
261 | /** |
||
262 | * sha getter |
||
263 | * |
||
264 | * @return string |
||
265 | */ |
||
266 | 9 | public function getSha(): string |
|
270 | |||
271 | /** |
||
272 | * current setter |
||
273 | * |
||
274 | * @param bool $current whether if the branch is the current or not |
||
275 | */ |
||
276 | public function setCurrent(bool $current): void |
||
280 | |||
281 | /** |
||
282 | * current getter |
||
283 | * |
||
284 | * @return bool |
||
285 | */ |
||
286 | 5 | public function getCurrent(): bool |
|
290 | |||
291 | /** |
||
292 | * comment setter |
||
293 | * |
||
294 | * @param string $comment the branch comment |
||
295 | */ |
||
296 | public function setComment(string $comment): void |
||
300 | |||
301 | /** |
||
302 | * comment getter |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | 1 | public function getComment(): string |
|
310 | |||
311 | /** |
||
312 | * fullref setter |
||
313 | * |
||
314 | * @param string $fullRef full git reference of the branch |
||
315 | */ |
||
316 | public function setFullRef(string $fullRef): void |
||
320 | |||
321 | /** |
||
322 | * fullRef getter |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | 2 | public function getFullRef(): string |
|
330 | } |
||
331 |