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\Objects; |
22
|
|
|
|
23
|
|
|
use GitElephant\Command\Caller\CallerInterface; |
24
|
|
|
use GitElephant\Command\RevListCommand; |
25
|
|
|
use GitElephant\Command\TagCommand; |
26
|
|
|
use GitElephant\Repository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* An object representing a git tag |
30
|
|
|
* |
31
|
|
|
* @author Matteo Giachino <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class Tag extends NodeObject |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* tag name |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $name; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* full reference |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $fullRef; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* sha |
51
|
|
|
* |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $sha; |
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates a new tag on the repository and returns it |
58
|
|
|
* |
59
|
|
|
* @param \GitElephant\Repository $repository repository instance |
60
|
|
|
* @param string $name branch name |
61
|
|
|
* @param string $startPoint branch to start from |
62
|
|
|
* @param string $message tag message |
63
|
|
|
* |
64
|
|
|
* @throws \RuntimeException |
65
|
|
|
* @return \GitElephant\Objects\Tag |
66
|
|
|
*/ |
67
|
27 |
|
public static function create( |
68
|
|
|
Repository $repository, |
69
|
|
|
string $name, |
70
|
|
|
$startPoint = null, |
71
|
|
|
string $message = null |
72
|
|
|
): ?\GitElephant\Objects\Tag { |
73
|
|
|
$repository |
74
|
27 |
|
->getCaller() |
75
|
27 |
|
->execute(TagCommand::getInstance($repository)->create($name, $startPoint, $message)); |
76
|
|
|
|
77
|
27 |
|
return $repository->getTag($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* static generator to generate a single commit from output of command.show service |
82
|
|
|
* |
83
|
|
|
* @param \GitElephant\Repository $repository repository |
84
|
|
|
* @param array $outputLines output lines |
85
|
|
|
* @param string $name name |
86
|
|
|
* |
87
|
|
|
* @throws \RuntimeException |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
90
|
|
|
* @return Tag |
91
|
|
|
*/ |
92
|
|
|
public static function createFromOutputLines( |
93
|
|
|
Repository $repository, |
94
|
|
|
array $outputLines, |
95
|
|
|
string $name |
96
|
|
|
): \GitElephant\Objects\Tag { |
97
|
|
|
$tag = new self($repository, $name); |
98
|
|
|
$tag->parseOutputLines($outputLines); |
99
|
|
|
|
100
|
|
|
return $tag; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Class constructor |
105
|
|
|
* |
106
|
|
|
* @param \GitElephant\Repository $repository repository instance |
107
|
|
|
* @param string $name name |
108
|
|
|
* |
109
|
|
|
* @throws \RuntimeException |
110
|
|
|
* @throws \InvalidArgumentException |
111
|
|
|
* @internal param string $line a single tag line from the git binary |
112
|
|
|
*/ |
113
|
27 |
|
public function __construct(Repository $repository, string $name) |
114
|
|
|
{ |
115
|
27 |
|
$this->repository = $repository; |
116
|
27 |
|
$this->name = $name; |
117
|
27 |
|
$this->fullRef = 'refs/tags/' . $this->name; |
118
|
27 |
|
$this->createFromCommand(); |
119
|
27 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* factory method |
123
|
|
|
* |
124
|
|
|
* @param \GitElephant\Repository $repository repository instance |
125
|
|
|
* @param string $name name |
126
|
|
|
* |
127
|
|
|
* @return \GitElephant\Objects\Tag |
128
|
|
|
*/ |
129
|
1 |
|
public static function pick(Repository $repository, string $name): \GitElephant\Objects\Tag |
130
|
|
|
{ |
131
|
1 |
|
return new self($repository, $name); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* deletes the tag |
136
|
|
|
*/ |
137
|
1 |
|
public function delete(): void |
138
|
|
|
{ |
139
|
1 |
|
$this->repository |
140
|
1 |
|
->getCaller() |
141
|
1 |
|
->execute(TagCommand::getInstance($this->getRepository())->delete($this)); |
142
|
1 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* get the commit properties from command |
146
|
|
|
* |
147
|
|
|
* @see ShowCommand::commitInfo |
148
|
|
|
*/ |
149
|
27 |
|
private function createFromCommand(): void |
150
|
|
|
{ |
151
|
27 |
|
$command = TagCommand::getInstance($this->getRepository())->listTags(); |
152
|
27 |
|
$outputLines = $this->getCaller()->execute($command, true, $this->getRepository()->getPath())->getOutputLines(); |
153
|
27 |
|
$this->parseOutputLines($outputLines); |
154
|
27 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* parse the output of a git command showing a commit |
158
|
|
|
* |
159
|
|
|
* @param array $outputLines output lines |
160
|
|
|
* |
161
|
|
|
* @throws \RuntimeException |
162
|
|
|
* @throws \Symfony\Component\Process\Exception\InvalidArgumentException |
163
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException |
164
|
|
|
* @throws \InvalidArgumentException |
165
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
27 |
|
private function parseOutputLines(array $outputLines) |
169
|
|
|
{ |
170
|
27 |
|
$found = false; |
171
|
27 |
|
foreach ($outputLines as $tagString) { |
172
|
27 |
|
if ($tagString != '' and $this->name === trim($tagString)) { |
173
|
27 |
|
$lines = $this->getCaller() |
174
|
27 |
|
->execute(RevListCommand::getInstance($this->getRepository())->getTagCommit($this)) |
175
|
27 |
|
->getOutputLines(); |
176
|
27 |
|
$this->setSha($lines[0]); |
177
|
27 |
|
$found = true; |
178
|
27 |
|
break; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
27 |
|
if (!$found) { |
183
|
1 |
|
throw new \InvalidArgumentException(sprintf('the tag %s doesn\'t exists', $this->name)); |
184
|
|
|
} |
185
|
27 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* toString magic method |
189
|
|
|
* |
190
|
|
|
* @return string the sha |
191
|
|
|
*/ |
192
|
|
|
public function __toString(): string |
193
|
|
|
{ |
194
|
|
|
return $this->getSha(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return CallerInterface |
199
|
|
|
*/ |
200
|
27 |
|
private function getCaller(): CallerInterface |
201
|
|
|
{ |
202
|
27 |
|
return $this->getRepository()->getCaller(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* name getter |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
3 |
|
public function getName(): string |
211
|
|
|
{ |
212
|
3 |
|
return $this->name; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* fullRef getter |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
27 |
|
public function getFullRef(): string |
221
|
|
|
{ |
222
|
27 |
|
return $this->fullRef; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* sha setter |
227
|
|
|
* |
228
|
|
|
* @param string $sha sha |
229
|
|
|
*/ |
230
|
27 |
|
public function setSha(string $sha): void |
231
|
|
|
{ |
232
|
27 |
|
$this->sha = $sha; |
233
|
27 |
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* sha getter |
237
|
|
|
* |
238
|
|
|
* @return string |
239
|
|
|
*/ |
240
|
4 |
|
public function getSha(): string |
241
|
|
|
{ |
242
|
4 |
|
return $this->sha; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|