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-pull
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 GitPullTask extends GitBaseTask
29
{
30
    /**
31
     * <repository> argument to git-pull
32
     *
33
     * @var string
34
     */
35
    private $source = 'origin';
36

37
    /**
38
     * <refspec> argument to git-pull
39
     *
40
     * @var string
41
     */
42
    private $refspec;
43

44
    /**
45
     * --rebase key to git-pull
46
     *
47
     * @var boolean
48
     */
49
    private $rebase = false;
50

51
    /**
52
     * --no-rebase key to git-pull
53
     * Allow to override --rebase (if set to default true in configuration)
54
     *
55
     * @var boolean
56
     */
57
    private $noRebase = false;
58

59
    /**
60
     * Merge strategy. See -s <strategy> of git-pull
61
     *
62
     * @var string
63
     */
64
    private $strategy;
65

66
    /**
67
     * -X or --strategy-option of git-pull
68
     *
69
     * @var string
70
     */
71
    private $strategyOption;
72

73
    /**
74
     * Fetch all remotes
75
     * --all key to git-pull
76
     *
77
     * @var boolean
78
     */
79
    private $allRemotes = false;
80

81
    /**
82
     * --append key to git-pull
83
     *
84
     * @var boolean
85
     */
86
    private $append = false;
87

88
    /**
89
     * Keep downloaded pack
90
     * --keep key to git-pull
91
     *
92
     * @var boolean
93
     */
94
    private $keepFiles = false;
95

96
    /**
97
     * Disable/enable automatic tag following
98
     * --no-tags key to git-pull
99
     *
100
     * @var boolean
101
     */
102
    private $noTags = false;
103

104
    /**
105
     * Fetch all tags (even not reachable from branch heads)
106
     * --tags key to git-pull
107
     *
108
     * @var boolean
109
     */
110
    private $tags = false;
111

112
    /**
113
     * --quiet, -q key to git-pull
114
     *
115
     * @var boolean
116
     */
117
    private $quiet = true;
118

119
    /**
120
     * --force, -f key to git-pull
121
     *
122
     * @var boolean
123
     */
124
    private $force = false;
125

126
    /**
127
     * Valid merge strategies
128
     *
129
     * @var array
130
     */
131
    private $validStrategies = [
132
        'octopus',
133
        'ours',
134
        'recursive',
135
        'resolve',
136
        'subtree'
137
    ];
138

139
    /**
140
     * The main entry point for the task
141
     */
142 1
    public function main()
143
    {
144 1
        if (null === $this->getRepository()) {
145 1
            throw new BuildException('"repository" is required parameter');
146
        }
147

148 1
        $client = $this->getGitClient(false, $this->getRepository());
149 1
        $command = $client->getCommand('pull');
150
        $command
151 1
            ->setOption('rebase', $this->isRebase());
152

153 1
        if (!$this->isRebase()) {
154 1
            $command->setOption('no-rebase', $this->isNoRebase());
155
        }
156

157 1
        $strategy = $this->getStrategy();
158 1
        if ($strategy) {
159
            // check if strategy is valid
160 1
            if (false === in_array($strategy, $this->validStrategies)) {
161 1
                throw new BuildException(
162 1
                    "Could not find merge strategy '" . $strategy . "'\n" .
163 1
                    "Available strategies are: " . implode(', ', $this->validStrategies)
164
                );
165
            }
166 0
            $command->setOption('strategy', $strategy);
167 0
            if ($this->getStrategyOption()) {
168 0
                $command->setOption(
169 0
                    'strategy-option',
170 0
                    $this->getStrategyOption()
171
                );
172
            }
173
        }
174

175
        // order of arguments is important
176
        $command
177 1
            ->setOption('tags', $this->isTags())
178 1
            ->setOption('no-tags', $this->isNoTags())
179 1
            ->setOption('keep', $this->isKeepFiles())
180 1
            ->setOption('append', $this->isAppend())
181 1
            ->setOption('q', $this->isQuiet())
182 1
            ->setOption('force', $this->isForce());
183

184
        // set operation target
185 1
        if ($this->isAllRemotes()) { // --all
186 1
            $command->setOption('all', true);
187 1
            $this->log('git-pull: fetching from all remotes', Project::MSG_INFO);
188 1
        } elseif ($this->getSource()) { // <repository> [<refspec>]
189 0
            $command->addArgument($this->getSource());
190 0
            if ($this->getRefspec()) {
191 0
                $command->addArgument($this->getRefspec());
192
            }
193 0
            $this->log(
194 0
                sprintf(
195 0
                    'git-pull: pulling from %s %s',
196 0
                    $this->getSource(),
197 0
                    $this->getRefspec()
198
                ),
199 0
                Project::MSG_INFO
200
            );
201
        } else {
202 1
            throw new BuildException('No source repository specified');
203
        }
204

205 1
        $this->log('git-pull command: ' . $command->createCommandString(), Project::MSG_INFO);
206

207
        try {
208 1
            $output = $command->execute();
209 0
        } catch (Exception $e) {
210 0
            throw new BuildException('Task execution failed.', $e);
211
        }
212

213 1
        $this->log('git-pull: complete', Project::MSG_INFO);
214 1
        $this->log('git-pull output: ' . trim($output), Project::MSG_INFO);
215
    }
216

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

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

233
    /**
234
     * @param $strategyOption
235
     */
236 0
    public function setStrategyOption($strategyOption)
237
    {
238 0
        $this->strategyOption = $strategyOption;
239
    }
240

241
    /**
242
     * @return string
243
     */
244 0
    public function getStrategyOption()
245
    {
246 0
        return $this->strategyOption;
247
    }
248

249
    /**
250
     * @param $source
251
     */
252 1
    public function setSource($source)
253
    {
254 1
        $this->source = $source;
255
    }
256

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

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

273
    /**
274
     * @return string
275
     */
276 0
    public function getRefspec()
277
    {
278 0
        return $this->refspec;
279
    }
280

281
    /**
282
     * @param $flag
283
     */
284 1
    public function setAll($flag)
285
    {
286 1
        $this->allRemotes = $flag;
287
    }
288

289
    /**
290
     * @return bool
291
     */
292 1
    public function getAll()
293
    {
294 1
        return $this->allRemotes;
295
    }
296

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

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

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

321
    /**
322
     * @return bool
323
     */
324 1
    public function isAppend()
325
    {
326 1
        return $this->getAppend();
327
    }
328

329
    /**
330
     * @param $flag
331
     */
332 0
    public function setKeep($flag)
333
    {
334 0
        $this->keepFiles = $flag;
335
    }
336

337
    /**
338
     * @return bool
339
     */
340 1
    public function getKeep()
341
    {
342 1
        return $this->keepFiles;
343
    }
344

345
    /**
346
     * @return bool
347
     */
348 1
    public function isKeepFiles()
349
    {
350 1
        return $this->getKeep();
351
    }
352

353
    /**
354
     * @param $flag
355
     */
356 0
    public function setNoTags($flag)
357
    {
358 0
        $this->noTags = $flag;
359
    }
360

361
    /**
362
     * @return bool
363
     */
364 1
    public function getNoTags()
365
    {
366 1
        return $this->noTags;
367
    }
368

369
    /**
370
     * @return bool
371
     */
372 1
    public function isNoTags()
373
    {
374 1
        return $this->getNoTags();
375
    }
376

377
    /**
378
     * @param $flag
379
     */
380 0
    public function setTags($flag)
381
    {
382 0
        $this->tags = $flag;
383
    }
384

385
    /**
386
     * @return bool
387
     */
388 1
    public function getTags()
389
    {
390 1
        return $this->tags;
391
    }
392

393
    /**
394
     * @return bool
395
     */
396 1
    public function isTags()
397
    {
398 1
        return $this->getTags();
399
    }
400

401
    /**
402
     * @param $flag
403
     */
404 1
    public function setQuiet($flag)
405
    {
406 1
        $this->quiet = $flag;
407
    }
408

409
    /**
410
     * @return bool
411
     */
412 1
    public function getQuiet()
413
    {
414 1
        return $this->quiet;
415
    }
416

417
    /**
418
     * @return bool
419
     */
420 1
    public function isQuiet()
421
    {
422 1
        return $this->getQuiet();
423
    }
424

425
    /**
426
     * @param $flag
427
     */
428 0
    public function setRebase($flag)
429
    {
430 0
        $this->rebase = (bool) $flag;
431
    }
432

433
    /**
434
     * @return bool
435
     */
436 1
    public function getRebase()
437
    {
438 1
        return $this->rebase;
439
    }
440

441
    /**
442
     * @return bool
443
     */
444 1
    public function isRebase()
445
    {
446 1
        return $this->getRebase();
447
    }
448

449
    /**
450
     * @param $flag
451
     */
452 0
    public function setNoRebase($flag)
453
    {
454 0
        $this->noRebase = (bool) $flag;
455
    }
456

457
    /**
458
     * @return bool
459
     */
460 1
    public function getNoRebase()
461
    {
462 1
        return $this->noRebase;
463
    }
464

465
    /**
466
     * @return bool
467
     */
468 1
    public function isNoRebase()
469
    {
470 1
        return $this->getNoRebase();
471
    }
472

473
    /**
474
     * @param $flag
475
     */
476 1
    public function setForce($flag)
477
    {
478 1
        $this->force = $flag;
479
    }
480

481
    /**
482
     * @return bool
483
     */
484 1
    public function getForce()
485
    {
486 1
        return $this->force;
487
    }
488

489
    /**
490
     * @return bool
491
     */
492 1
    public function isForce()
493
    {
494 1
        return $this->getForce();
495
    }
496
}

Read our documentation on viewing source code .

Loading