1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Wrapper aroung git-branch |
22
|
|
|
* |
23
|
|
|
* @author Victor Farazdagi <[email protected]> |
24
|
|
|
* @package phing.tasks.ext.git |
25
|
|
|
* @see VersionControl_Git |
26
|
|
|
* @since 2.4.3 |
27
|
|
|
*/ |
28
|
|
|
class GitBranchTask extends GitBaseTask |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Branch name |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $branchname; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* New Branch name for git-branch -m | -M |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $newbranch; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* If not HEAD, specify starting point |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $startPoint; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* --set-upstream key to git-branch |
53
|
|
|
* |
54
|
|
|
* @var boolean |
55
|
|
|
*/ |
56
|
|
|
private $setUpstream = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* --track key to git-branch |
60
|
|
|
* |
61
|
|
|
* @var boolean |
62
|
|
|
*/ |
63
|
|
|
private $track = false; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* --no-track key to git-branch |
67
|
|
|
* |
68
|
|
|
* @var boolean |
69
|
|
|
*/ |
70
|
|
|
private $noTrack = false; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* --force, -f key to git-branch |
74
|
|
|
* |
75
|
|
|
* @var boolean |
76
|
|
|
*/ |
77
|
|
|
private $force = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* -d, -D, -m, -M options to git-branch |
81
|
|
|
* Respective task options: |
82
|
|
|
* delete, forceDelete, move, forceMove |
83
|
|
|
* |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
private $extraOptions = [ |
87
|
|
|
'd' => false, |
88
|
|
|
'D' => false, |
89
|
|
|
'm' => false, |
90
|
|
|
'M' => false, |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var string $setUpstreamTo |
95
|
|
|
*/ |
96
|
|
|
private $setUpstreamTo = ''; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* The main entry point for the task |
100
|
|
|
*/ |
101
|
22 |
|
public function main() |
102
|
|
|
{ |
103
|
22 |
|
if (null === $this->getRepository()) { |
|
|
|
|
104
|
1 |
|
throw new BuildException('"repository" is required parameter'); |
105
|
|
|
} |
106
|
21 |
|
if (null === $this->getBranchname()) { |
|
|
|
|
107
|
1 |
|
throw new BuildException('"branchname" is required parameter'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// if we are moving branch, we need to know new name |
111
|
20 |
|
if ($this->isMove() || $this->isForceMove()) { |
112
|
4 |
|
if (null === $this->getNewBranch()) { |
|
|
|
|
113
|
2 |
|
throw new BuildException('"newbranch" is required parameter'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
20 |
|
$client = $this->getGitClient(false, $this->getRepository()); |
118
|
|
|
|
119
|
20 |
|
$command = $client->getCommand('branch'); |
120
|
|
|
|
121
|
20 |
|
if (version_compare($client->getGitVersion(), '2.15.0', '<')) { |
122
|
|
|
$command->setOption('set-upstream', $this->isSetUpstream()); |
123
|
20 |
|
} elseif ($this->isSetUpstreamTo()) { |
124
|
1 |
|
$command->setOption('set-upstream-to', $this->getSetUpstreamTo()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
$command |
128
|
20 |
|
->setOption('no-track', $this->isNoTrack()) |
129
|
20 |
|
->setOption('force', $this->isForce()); |
130
|
20 |
|
if ($this->isNoTrack() == false) { |
|
|
|
|
131
|
19 |
|
$command->setOption('track', $this->getTrack()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// check extra options (delete, move) |
135
|
20 |
|
foreach ($this->extraOptions as $option => $flag) { |
136
|
20 |
|
if ($flag) { |
137
|
7 |
|
$command->setOption($option, true); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
20 |
|
$command->addArgument($this->getBranchname()); |
142
|
|
|
|
143
|
20 |
|
if (null !== $this->getStartPoint()) { |
|
|
|
|
144
|
8 |
|
$command->addArgument($this->getStartPoint()); |
145
|
|
|
} |
146
|
|
|
|
147
|
20 |
|
if (null !== $this->getNewBranch()) { |
|
|
|
|
148
|
2 |
|
$command->addArgument($this->getNewBranch()); |
149
|
|
|
} |
150
|
|
|
|
151
|
20 |
|
$this->log('git-branch command: ' . $command->createCommandString(), Project::MSG_INFO); |
152
|
|
|
|
153
|
|
|
try { |
154
|
20 |
|
$output = $command->execute(); |
155
|
|
|
} catch (Exception $e) { |
156
|
|
|
throw new BuildException( |
157
|
|
|
'Task execution failed with git command "' . $command->createCommandString() . '""', |
158
|
|
|
$e |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
20 |
|
$this->log( |
163
|
20 |
|
sprintf('git-branch: branch "%s" repository', $this->getRepository()), |
164
|
20 |
|
Project::MSG_INFO |
165
|
|
|
); |
166
|
20 |
|
$this->log('git-branch output: ' . str_replace('\'', '', trim($output)), Project::MSG_INFO); |
167
|
20 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $flag |
171
|
|
|
*/ |
172
|
|
|
public function setSetUpstream($flag) |
173
|
|
|
{ |
174
|
|
|
$this->setUpstream = $flag; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function getSetUpstream() |
181
|
|
|
{ |
182
|
|
|
return $this->setUpstream; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
|
public function isSetUpstream() |
189
|
|
|
{ |
190
|
|
|
return $this->getSetUpstream(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $branch |
195
|
|
|
*/ |
196
|
1 |
|
public function setSetUpstreamTo($branch) |
197
|
|
|
{ |
198
|
1 |
|
$this->setUpstreamTo = $branch; |
199
|
1 |
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
20 |
|
public function getSetUpstreamTo() |
205
|
|
|
{ |
206
|
20 |
|
return $this->setUpstreamTo; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
20 |
|
public function isSetUpstreamTo() |
213
|
|
|
{ |
214
|
20 |
|
return $this->getSetUpstreamTo() !== ''; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param $flag |
219
|
|
|
*/ |
220
|
7 |
|
public function setTrack($flag) |
221
|
|
|
{ |
222
|
7 |
|
$this->track = $flag; |
223
|
7 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return bool |
227
|
|
|
*/ |
228
|
19 |
|
public function getTrack() |
229
|
|
|
{ |
230
|
19 |
|
return $this->track; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @return bool |
235
|
|
|
*/ |
236
|
|
|
public function isTrack() |
237
|
|
|
{ |
238
|
|
|
return $this->getTrack(); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param $flag |
243
|
|
|
*/ |
244
|
1 |
|
public function setNoTrack($flag) |
245
|
|
|
{ |
246
|
1 |
|
$this->noTrack = $flag; |
247
|
1 |
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return bool |
251
|
|
|
*/ |
252
|
20 |
|
public function getNoTrack() |
253
|
|
|
{ |
254
|
20 |
|
return $this->noTrack; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return bool |
259
|
|
|
*/ |
260
|
20 |
|
public function isNoTrack() |
261
|
|
|
{ |
262
|
20 |
|
return $this->getNoTrack(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @param $flag |
267
|
|
|
*/ |
268
|
2 |
|
public function setForce($flag) |
269
|
|
|
{ |
270
|
2 |
|
$this->force = $flag; |
271
|
2 |
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return bool |
275
|
|
|
*/ |
276
|
20 |
|
public function getForce() |
277
|
|
|
{ |
278
|
20 |
|
return $this->force; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return bool |
283
|
|
|
*/ |
284
|
20 |
|
public function isForce() |
285
|
|
|
{ |
286
|
20 |
|
return $this->getForce(); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* @param $branchname |
291
|
|
|
*/ |
292
|
20 |
|
public function setBranchname($branchname) |
293
|
|
|
{ |
294
|
20 |
|
$this->branchname = $branchname; |
295
|
20 |
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
21 |
|
public function getBranchname() |
301
|
|
|
{ |
302
|
21 |
|
return $this->branchname; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* @param $startPoint |
307
|
|
|
*/ |
308
|
8 |
|
public function setStartPoint($startPoint) |
309
|
|
|
{ |
310
|
8 |
|
$this->startPoint = $startPoint; |
311
|
8 |
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return string |
315
|
|
|
*/ |
316
|
20 |
|
public function getStartPoint() |
317
|
|
|
{ |
318
|
20 |
|
return $this->startPoint; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @param $flag |
323
|
|
|
*/ |
324
|
7 |
|
public function setDelete($flag) |
325
|
|
|
{ |
326
|
7 |
|
$this->extraOptions['d'] = $flag; |
327
|
7 |
|
} |
328
|
|
|
|
329
|
|
|
public function getDelete() |
330
|
|
|
{ |
331
|
|
|
return $this->extraOptions['d']; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
public function isDelete() |
335
|
|
|
{ |
336
|
|
|
return $this->getDelete(); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param $flag |
341
|
|
|
*/ |
342
|
1 |
|
public function setForceDelete($flag) |
343
|
|
|
{ |
344
|
1 |
|
$this->extraOptions['D'] = $flag; |
345
|
1 |
|
} |
346
|
|
|
|
347
|
|
|
public function getForceDelete() |
348
|
|
|
{ |
349
|
|
|
return $this->extraOptions['D']; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* @param $flag |
354
|
|
|
*/ |
355
|
2 |
|
public function setMove($flag) |
356
|
|
|
{ |
357
|
2 |
|
$this->extraOptions['m'] = $flag; |
358
|
2 |
|
} |
359
|
|
|
|
360
|
20 |
|
public function getMove() |
361
|
|
|
{ |
362
|
20 |
|
return $this->extraOptions['m']; |
363
|
|
|
} |
364
|
|
|
|
365
|
20 |
|
public function isMove() |
366
|
|
|
{ |
367
|
20 |
|
return $this->getMove(); |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @param $flag |
372
|
|
|
*/ |
373
|
2 |
|
public function setForceMove($flag) |
374
|
|
|
{ |
375
|
2 |
|
$this->extraOptions['M'] = $flag; |
376
|
2 |
|
} |
377
|
|
|
|
378
|
20 |
|
public function getForceMove() |
379
|
|
|
{ |
380
|
20 |
|
return $this->extraOptions['M']; |
381
|
|
|
} |
382
|
|
|
|
383
|
20 |
|
public function isForceMove() |
384
|
|
|
{ |
385
|
20 |
|
return $this->getForceMove(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param $name |
390
|
|
|
*/ |
391
|
2 |
|
public function setNewBranch($name) |
392
|
|
|
{ |
393
|
2 |
|
$this->newbranch = $name; |
394
|
2 |
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* @return string |
398
|
|
|
*/ |
399
|
20 |
|
public function getNewBranch() |
400
|
|
|
{ |
401
|
20 |
|
return $this->newbranch; |
402
|
|
|
} |
403
|
|
|
} |
404
|
|
|
|