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 <simple.square@gmail.com>
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 1
    public function main()
102
    {
103 1
        if (null === $this->getRepository()) {
104 1
            throw new BuildException('"repository" is required parameter');
105
        }
106 1
        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 1
        if ($this->isMove() || $this->isForceMove()) {
112 1
            if (null === $this->getNewBranch()) {
113 1
                throw new BuildException('"newbranch" is required parameter');
114
            }
115
        }
116

117 1
        $client = $this->getGitClient(false, $this->getRepository());
118

119 1
        $command = $client->getCommand('branch');
120

121 1
        if (version_compare($client->getGitVersion(), '2.15.0', '<')) {
122 0
            $command->setOption('set-upstream', $this->isSetUpstream());
123 1
        } elseif ($this->isSetUpstreamTo()) {
124 1
            $command->setOption('set-upstream-to', $this->getSetUpstreamTo());
125
        }
126

127
        $command
128 1
            ->setOption('no-track', $this->isNoTrack())
129 1
            ->setOption('force', $this->isForce());
130 1
        if ($this->isNoTrack() == false) {
131 1
            $command->setOption('track', $this->getTrack());
132
        }
133

134
        // check extra options (delete, move)
135 1
        foreach ($this->extraOptions as $option => $flag) {
136 1
            if ($flag) {
137 1
                $command->setOption($option, true);
138
            }
139
        }
140

141 1
        $command->addArgument($this->getBranchname());
142

143 1
        if (null !== $this->getStartPoint()) {
144 1
            $command->addArgument($this->getStartPoint());
145
        }
146

147 1
        if (null !== $this->getNewBranch()) {
148 1
            $command->addArgument($this->getNewBranch());
149
        }
150

151 1
        $this->log('git-branch command: ' . $command->createCommandString(), Project::MSG_INFO);
152

153
        try {
154 1
            $output = $command->execute();
155 0
        } catch (Exception $e) {
156 0
            throw new BuildException(
157 0
                'Task execution failed with git command "' . $command->createCommandString() . '""',
158
                $e
159
            );
160
        }
161

162 1
        $this->log(
163 1
            sprintf('git-branch: branch "%s" repository', $this->getRepository()),
164 1
            Project::MSG_INFO
165
        );
166 1
        $this->log('git-branch output: ' . str_replace('\'', '', trim($output)), Project::MSG_INFO);
167
    }
168

169
    /**
170
     * @param $flag
171
     */
172 0
    public function setSetUpstream($flag)
173
    {
174 0
        $this->setUpstream = $flag;
175
    }
176

177
    /**
178
     * @return bool
179
     */
180 0
    public function getSetUpstream()
181
    {
182 0
        return $this->setUpstream;
183
    }
184

185
    /**
186
     * @return bool
187
     */
188 0
    public function isSetUpstream()
189
    {
190 0
        return $this->getSetUpstream();
191
    }
192

193
    /**
194
     * @param string $branch
195
     */
196 1
    public function setSetUpstreamTo($branch)
197
    {
198 1
        $this->setUpstreamTo = $branch;
199
    }
200

201
    /**
202
     * @return string
203
     */
204 1
    public function getSetUpstreamTo()
205
    {
206 1
        return $this->setUpstreamTo;
207
    }
208

209
    /**
210
     * @return bool
211
     */
212 1
    public function isSetUpstreamTo()
213
    {
214 1
        return $this->getSetUpstreamTo() !== '';
215
    }
216

217
    /**
218
     * @param $flag
219
     */
220 1
    public function setTrack($flag)
221
    {
222 1
        $this->track = $flag;
223
    }
224

225
    /**
226
     * @return bool
227
     */
228 1
    public function getTrack()
229
    {
230 1
        return $this->track;
231
    }
232

233
    /**
234
     * @return bool
235
     */
236 0
    public function isTrack()
237
    {
238 0
        return $this->getTrack();
239
    }
240

241
    /**
242
     * @param $flag
243
     */
244 1
    public function setNoTrack($flag)
245
    {
246 1
        $this->noTrack = $flag;
247
    }
248

249
    /**
250
     * @return bool
251
     */
252 1
    public function getNoTrack()
253
    {
254 1
        return $this->noTrack;
255
    }
256

257
    /**
258
     * @return bool
259
     */
260 1
    public function isNoTrack()
261
    {
262 1
        return $this->getNoTrack();
263
    }
264

265
    /**
266
     * @param $flag
267
     */
268 1
    public function setForce($flag)
269
    {
270 1
        $this->force = $flag;
271
    }
272

273
    /**
274
     * @return bool
275
     */
276 1
    public function getForce()
277
    {
278 1
        return $this->force;
279
    }
280

281
    /**
282
     * @return bool
283
     */
284 1
    public function isForce()
285
    {
286 1
        return $this->getForce();
287
    }
288

289
    /**
290
     * @param $branchname
291
     */
292 1
    public function setBranchname($branchname)
293
    {
294 1
        $this->branchname = $branchname;
295
    }
296

297
    /**
298
     * @return string
299
     */
300 1
    public function getBranchname()
301
    {
302 1
        return $this->branchname;
303
    }
304

305
    /**
306
     * @param $startPoint
307
     */
308 1
    public function setStartPoint($startPoint)
309
    {
310 1
        $this->startPoint = $startPoint;
311
    }
312

313
    /**
314
     * @return string
315
     */
316 1
    public function getStartPoint()
317
    {
318 1
        return $this->startPoint;
319
    }
320

321
    /**
322
     * @param $flag
323
     */
324 1
    public function setDelete($flag)
325
    {
326 1
        $this->extraOptions['d'] = $flag;
327
    }
328

329 0
    public function getDelete()
330
    {
331 0
        return $this->extraOptions['d'];
332
    }
333

334 0
    public function isDelete()
335
    {
336 0
        return $this->getDelete();
337
    }
338

339
    /**
340
     * @param $flag
341
     */
342 1
    public function setForceDelete($flag)
343
    {
344 1
        $this->extraOptions['D'] = $flag;
345
    }
346

347 0
    public function getForceDelete()
348
    {
349 0
        return $this->extraOptions['D'];
350
    }
351

352
    /**
353
     * @param $flag
354
     */
355 1
    public function setMove($flag)
356
    {
357 1
        $this->extraOptions['m'] = $flag;
358
    }
359

360 1
    public function getMove()
361
    {
362 1
        return $this->extraOptions['m'];
363
    }
364

365 1
    public function isMove()
366
    {
367 1
        return $this->getMove();
368
    }
369

370
    /**
371
     * @param $flag
372
     */
373 1
    public function setForceMove($flag)
374
    {
375 1
        $this->extraOptions['M'] = $flag;
376
    }
377

378 1
    public function getForceMove()
379
    {
380 1
        return $this->extraOptions['M'];
381
    }
382

383 1
    public function isForceMove()
384
    {
385 1
        return $this->getForceMove();
386
    }
387

388
    /**
389
     * @param $name
390
     */
391 1
    public function setNewBranch($name)
392
    {
393 1
        $this->newbranch = $name;
394
    }
395

396
    /**
397
     * @return string
398
     */
399 1
    public function getNewBranch()
400
    {
401 1
        return $this->newbranch;
402
    }
403
}

Read our documentation on viewing source code .

Loading