Completed
Push — master ( af55ba...fa8032 )
by Chris
10s
created

GitWorkingCopy::hasRemote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace GitWrapper;
4
5
use Symfony\Component\Process\ProcessUtils;
6
7
/**
8
 * Interacts with a working copy.
9
 *
10
 * All commands executed via an instance of this class act on the working copy
11
 * that is set through the constructor.
12
 */
13
class GitWorkingCopy
14
{
15
    /**
16
     * The GitWrapper object that likely instantiated this class.
17
     *
18
     * @var \GitWrapper\GitWrapper
19
     */
20
    protected $wrapper;
21
22
    /**
23
     * Path to the directory containing the working copy.
24
     *
25
     * @var string
26
     */
27
    protected $directory;
28
29
    /**
30
     * The output captured by the last run Git commnd(s).
31
     *
32
     * @var string
33
     */
34
    protected $output = '';
35
36
    /**
37
     * A boolean flagging whether the repository is cloned.
38
     *
39
     * If the variable is null, the a rudimentary check will be performed to see
40
     * if the directory looks like it is a working copy.
41
     *
42
     * @param bool|null
43
     */
44
    protected $cloned;
45
46
    /**
47
     * Constructs a GitWorkingCopy object.
48
     *
49
     * @param \GitWrapper\GitWrapper $wrapper
50
     *   The GitWrapper object that likely instantiated this class.
51
     * @param string $directory
52
     *   Path to the directory containing the working copy.
53
     */
54 212
    public function __construct(GitWrapper $wrapper, $directory)
55
    {
56 212
        $this->wrapper = $wrapper;
57 212
        $this->directory = $directory;
58 212
    }
59
60
    /**
61
     * Returns the GitWrapper object that likely instantiated this class.
62
     *
63
     * @return \GitWrapper\GitWrapper
64
     */
65 12
    public function getWrapper()
66
    {
67 12
        return $this->wrapper;
68
    }
69
70
    /**
71
     * Gets the path to the directory containing the working copy.
72
     *
73
     * @return string
74
     */
75 4
    public function getDirectory()
76
    {
77 4
        return $this->directory;
78
    }
79
80
    /**
81
     * Gets the output captured by the last run Git commnd(s).
82
     *
83
     * @return string
84
     */
85 148
    public function getOutput()
86
    {
87 148
        $output = $this->output;
88 148
        $this->output = '';
89 148
        return $output;
90
    }
91
92
    /**
93
     * Clears the stored output captured by the last run Git command(s).
94
     *
95
     * @return \GitWrapper\GitWorkingCopy
96
     */
97 200
    public function clearOutput()
98
    {
99 200
        $this->output = '';
100 200
        return $this;
101
    }
102
103
    /**
104
     * Manually sets the cloned flag.
105
     *
106
     * @param boolean $cloned
107
     *   Whether the repository is cloned into the directory or not.
108
     *
109
     * @return \GitWrapper\GitWorkingCopy
110
     */
111 208
    public function setCloned($cloned)
112
    {
113 208
        $this->cloned = (bool) $cloned;
114 208
        return $this;
115
    }
116
117
    /**
118
     * Checks whether a repository has already been cloned to this directory.
119
     *
120
     * If the flag is not set, test if it looks like we're at a git directory.
121
     *
122
     * @return boolean
123
     */
124 4
    public function isCloned()
125
    {
126 4
        if (!isset($this->cloned)) {
127 4
            $gitDir = $this->directory;
128 4
            if (is_dir($gitDir . '/.git')) {
129 4
                $gitDir .= '/.git';
130 4
            };
131 4
            $this->cloned = (is_dir($gitDir . '/objects') && is_dir($gitDir . '/refs') && is_file($gitDir . '/HEAD'));
132 4
        }
133 4
        return $this->cloned;
134
    }
135
136
    /**
137
     * Runs a Git command and captures the output.
138
     *
139
     * @param array $args
140
     *   The arguments passed to the command method.
141
     * @param boolean $setDirectory
142
     *   Set the working directory, defaults to true.
143
     *
144
     * @return \GitWrapper\GitWorkingCopy
145
     *
146
     * @throws \GitWrapper\GitException
147
     *
148
     * @see GitWrapper::run()
149
     */
150 208
    public function run($args, $setDirectory = true)
151
    {
152 208
        $command = call_user_func_array(array('GitWrapper\GitCommand', 'getInstance'), $args);
153 208
        if ($setDirectory) {
154 204
            $command->setDirectory($this->directory);
155 204
        }
156 208
        $this->output .= $this->wrapper->run($command);
157 208
        return $this;
158
    }
159
160
    /**
161
     * @defgroup command_helpers Git Command Helpers
162
     *
163
     * Helper methods that wrap common Git commands.
164
     *
165
     * @{
166
     */
167
168
    /**
169
     * Returns the output of a `git status -s` command.
170
     *
171
     * @return string
172
     *
173
     * @throws \GitWrapper\GitException
174
     */
175 20
    public function getStatus()
176
    {
177 20
        return $this->wrapper->git('status -s', $this->directory);
178
    }
179
180
    /**
181
     * Returns true if there are changes to commit.
182
     *
183
     * @return bool
184
     *
185
     * @throws \GitWrapper\GitException
186
     */
187 12
    public function hasChanges()
188
    {
189 12
        $output = $this->getStatus();
190 12
        return !empty($output);
191
    }
192
193
    /**
194
     * Returns whether HEAD has a remote tracking branch.
195
     *
196
     * @return bool
197
     */
198 20
    public function isTracking()
199
    {
200
        try {
201 20
            $this->run(array('rev-parse @{u}'));
202 20
        } catch (GitException $e) {
203 4
            return false;
204
        }
205 20
        return true;
206
    }
207
208
    /**
209
     * Returns whether HEAD is up-to-date with its remote tracking branch.
210
     *
211
     * @return bool
212
     *
213
     * @throws \GitWrapper\GitException
214
     *   Thrown when HEAD does not have a remote tracking branch.
215
     */
216 4
    public function isUpToDate()
217
    {
218 4
        if (!$this->isTracking()) {
219
            throw new GitException('Error: HEAD does not have a remote tracking branch. Cannot check if it is up-to-date.');
220
        }
221 4
        $this->clearOutput();
222 4
        $merge_base = (string) $this->run(array('merge-base @ @{u}'));
223 4
        $remote_sha = (string) $this->run(array('rev-parse @{u}'));
224 4
        return $merge_base === $remote_sha;
225
    }
226
227
    /**
228
     * Returns whether HEAD is ahead of its remote tracking branch.
229
     *
230
     * If this returns true it means that commits are present locally which have
231
     * not yet been pushed to the remote.
232
     *
233
     * @return bool
234
     *
235
     * @throws \GitWrapper\GitException
236
     *   Thrown when HEAD does not have a remote tracking branch.
237
     */
238 4 View Code Duplication
    public function isAhead()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
239
    {
240 4
        if (!$this->isTracking()) {
241
            throw new GitException('Error: HEAD does not have a remote tracking branch. Cannot check if it is ahead.');
242
        }
243 4
        $this->clearOutput();
244 4
        $merge_base = (string) $this->run(array('merge-base @ @{u}'));
245 4
        $local_sha = (string) $this->run(array('rev-parse @'));
246 4
        $remote_sha = (string) $this->run(array('rev-parse @{u}'));
247 4
        return $merge_base === $remote_sha && $local_sha !== $remote_sha;
248
    }
249
250
    /**
251
     * Returns whether HEAD is behind its remote tracking branch.
252
     *
253
     * If this returns true it means that a pull is needed to bring the branch
254
     * up-to-date with the remote.
255
     *
256
     * @return bool
257
     *
258
     * @throws \GitWrapper\GitException
259
     *   Thrown when HEAD does not have a remote tracking branch.
260
     */
261 4 View Code Duplication
    public function isBehind()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
262
    {
263 4
        if (!$this->isTracking()) {
264
            throw new GitException('Error: HEAD does not have a remote tracking branch. Cannot check if it is behind.');
265
        }
266 4
        $this->clearOutput();
267 4
        $merge_base = (string) $this->run(array('merge-base @ @{u}'));
268 4
        $local_sha = (string) $this->run(array('rev-parse @'));
269 4
        $remote_sha = (string) $this->run(array('rev-parse @{u}'));
270 4
        return $merge_base === $local_sha && $local_sha !== $remote_sha;
271
    }
272
273
    /**
274
     * Returns whether HEAD needs to be merged with its remote tracking branch.
275
     *
276
     * If this returns true it means that HEAD has diverged from its remote
277
     * tracking branch; new commits are present locally as well as on the
278
     * remote.
279
     *
280
     * @return bool
281
     *   true if HEAD needs to be merged with the remote, false otherwise.
282
     *
283
     * @throws \GitWrapper\GitException
284
     *   Thrown when HEAD does not have a remote tracking branch.
285
     */
286 4 View Code Duplication
    public function needsMerge()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
    {
288 4
        if (!$this->isTracking()) {
289
            throw new GitException('Error: HEAD does not have a remote tracking branch. Cannot check if it is behind.');
290
        }
291 4
        $this->clearOutput();
292 4
        $merge_base = (string) $this->run(array('merge-base @ @{u}'));
293 4
        $local_sha = (string) $this->run(array('rev-parse @'));
294 4
        $remote_sha = (string) $this->run(array('rev-parse @{u}'));
295 4
        return $merge_base !== $local_sha && $merge_base !== $remote_sha;
296
    }
297
298
    /**
299
     * Returns a GitBranches object containing information on the repository's
300
     * branches.
301
     *
302
     * @return GitBranches
303
     */
304 28
    public function getBranches()
305
    {
306 28
        return new GitBranches($this);
307
    }
308
309
    /**
310
     * Helper method that pushes a tag to a repository.
311
     *
312
     * This is synonymous with `git push origin tag v1.2.3`.
313
     *
314
     * @param string $tag
315
     *   The tag being pushed.
316
     * @param string $repository
317
     *   The destination of the push operation, which is either a URL or name of
318
     *   the remote. Defaults to "origin".
319
     * @param array $options
320
     *   (optional) An associative array of command line options.
321
     *
322
     * @see GitWorkingCopy::push()
323
     */
324 4
    public function pushTag($tag, $repository = 'origin', array $options = array())
325
    {
326 4
        return $this->push($repository, 'tag', $tag, $options);
327
    }
328
329
    /**
330
     * Helper method that pushes all tags to a repository.
331
     *
332
     * This is synonymous with `git push --tags origin`.
333
     *
334
     * @param string $repository
335
     *   The destination of the push operation, which is either a URL or name of
336
     *   the remote. Defaults to "origin".
337
     * @param array $options
338
     *   (optional) An associative array of command line options.
339
     *
340
     * @see GitWorkingCopy::push()
341
     */
342 200
    public function pushTags($repository = 'origin', array $options = array())
343
    {
344 200
        $options['tags'] = true;
345 200
        return $this->push($repository, $options);
346
    }
347
348
    /**
349
     * Fetches all remotes.
350
     *
351
     * This is synonymous with `git fetch --all`.
352
     *
353
     * @param array $options
354
     *   (optional) An associative array of command line options.
355
     *
356
     * @see GitWorkingCopy::fetch()
357
     */
358 4
    public function fetchAll(array $options = array())
359
    {
360 4
        $options['all'] = true;
361 4
        return $this->fetch($options);
362
    }
363
364
    /**
365
     * Create a new branch and check it out.
366
     *
367
     * This is synonymous with `git checkout -b`.
368
     *
369
     * @param string $branch
370
     *   The new branch being created.
371
     *
372
     * @see GitWorkingCopy::checkout()
373
     */
374 200
    public function checkoutNewBranch($branch, array $options = array())
375
    {
376 200
        $options['b'] = true;
377 200
        return $this->checkout($branch, $options);
378
    }
379
380
    /**
381
     * Adds a remote to the repository.
382
     *
383
     * @param string $name
384
     *   The name of the remote to add.
385
     * @param string $url
386
     *   The URL of the remote to add.
387
     * @param array $options
388
     *   An associative array of options, with the following keys:
389
     *   - -f: Boolean, set to true to run git fetch immediately after the
390
     *     remote is set up. Defaults to false.
391
     *   - --tags: Boolean. By default only the tags from the fetched branches
392
     *     are imported when git fetch is run. Set this to true to import every
393
     *     tag from the remote repository. Defaults to false.
394
     *   - --no-tags: Boolean, when set to true, git fetch does not import tags
395
     *     from the remote repository. Defaults to false.
396
     *   - -t: Optional array of branch names to track. If left empty, all
397
     *     branches will be tracked.
398
     *   - -m: Optional name of the master branch to track. This will set up a
399
     *     symbolic ref 'refs/remotes/<name>/HEAD which points at the specified
400
     *     master branch on the remote. When omitted, no symbolic ref will be
401
     *     created.
402
     *
403
     * @return \GitWrapper\GitWorkingCopy
404
     *
405
     * @throws \GitWrapper\GitException
406
     *   Thrown when the name or URL are missing.
407
     */
408 56
    public function addRemote($name, $url, $options = array()) {
409 56
        if (empty($name)) {
410
            throw new GitException('Cannot add remote without a name.');
411
        }
412 56
        if (empty($url)) {
413
            throw new GitException('Cannot add remote without a URL.');
414
        }
415
416 56
        $args = array('add');
417
418
        // Add boolean options.
419 56
        foreach (array('-f', '--tags', '--no-tags') as $option) {
420 56
            if (!empty($options[$option])) {
421 20
                $args[] = $option;
422 20
            }
423 56
        }
424
425
        // Add tracking branches.
426 56
        if (!empty($options['-t'])) {
427 8
            foreach ($options['-t'] as $branch) {
428 8
                array_push($args, '-t', $branch);
429 8
            }
430 8
        }
431
432
        // Add master branch.
433 56
        if (!empty($options['-m'])) {
434 4
            array_push($args, '-m', $options['-m']);
435 4
        }
436
437
        // Add remote name and URL.
438 56
        array_push($args, $name, $url);
439
440 56
        return call_user_func_array(array($this, 'remote'), $args);
441
    }
442
443
    /**
444
     * Removes the given remote.
445
     *
446
     * @param string $name
447
     *   The name of the remote to remove.
448
     *
449
     * @return \GitWrapper\GitWorkingCopy
450
     */
451 4
    public function removeRemote($name) {
452 4
        return $this->remote('rm', $name);
453
    }
454
455
    /**
456
     * Checks if the given remote exists.
457
     *
458
     * @param string $name
459
     *   The name of the remote to check.
460
     *
461
     * @return bool
462
     */
463 36
    public function hasRemote($name) {
464 36
        return array_key_exists($name, $this->getRemotes());
465
    }
466
467
    /**
468
     * Returns the given remote.
469
     *
470
     * @param string $name
471
     *   The name of the remote.
472
     *
473
     * @return array
474
     *   An associative array with the following keys:
475
     *   - fetch: the fetch URL.
476
     *   - push: the push URL.
477
     *
478
     * @throws \GitWrapper\GitException
479
     *   Thrown when the remote does not exist.
480
     */
481 4
    public function getRemote($name) {
482 4
        if (!$this->hasRemote($name)) {
483
            throw new GitException('The remote "' . $name . '" does not exist.');
484
        }
485 4
        $remotes = $this->getRemotes();
486 4
        return $remotes[$name];
487
    }
488
489
    /**
490
     * Returns all existing remotes.
491
     *
492
     * @return array
493
     *   An associative array, keyed by remote name, containing an associative
494
     *   array with the following keys:
495
     *   - fetch: the fetch URL.
496
     *   - push: the push URL.
497
     */
498 40
    public function getRemotes() {
499 40
        $this->clearOutput();
500
501 40
        $remotes = array();
502 40
        foreach (explode("\n", rtrim($this->remote()->getOutput())) as $remote) {
503 40
            $remotes[$remote]['fetch'] = $this->getRemoteUrl($remote);
504 40
            $remotes[$remote]['push'] = $this->getRemoteUrl($remote, 'push');
505 40
        }
506 40
        return $remotes;
507
    }
508
509
    /**
510
     * Returns the fetch or push URL of a given remote.
511
     *
512
     * @param string $remote
513
     *   The name of the remote for which to return the fetch or push URL.
514
     * @param string $operation
515
     *   The operation for which to return the remote. Can be either 'fetch' or
516
     *   'push'. Defaults to 'fetch'.
517
     *
518
     * @return string
519
     *   The URL.
520
     */
521 56
    public function getRemoteUrl($remote, $operation = 'fetch') {
522 56
        $this->clearOutput();
523
524 56
        $args = $operation === 'push' ? array('get-url', '--push', $remote) : array('get-url', $remote);
525
        try {
526 56
            return rtrim(call_user_func_array(array($this, 'remote'), $args)->getOutput());
527
        }
528 56
        catch (GitException $e) {
529
            // Fall back to parsing 'git remote -v' for older versions of git
530
            // that do not support `git remote get-url`.
531 56
            $identifier = " ($operation)";
532 56
            foreach (explode("\n", rtrim($this->remote('-v')->getOutput())) as $line) {
533 56
                if (strpos($line, $remote) === 0 && strrpos($line, $identifier) === strlen($line) - strlen($identifier)) {
534 56
                    preg_match('/^.+\t(.+) \(' . $operation . '\)$/', $line, $matches);
535 56
                    return $matches[1];
536
                }
537 52
            }
538
        }
539
    }
540
541
    /**
542
     * @} End of "defgroup command_helpers".
543
     */
544
545
    /**
546
     * @defgroup commands Git Commands
547
     *
548
     * All methods in this group correspond with Git commands, for example
549
     * "git add", "git commit", "git push", etc.
550
     *
551
     * @{
552
     */
553
554
    /**
555
     * Executes a `git add` command.
556
     *
557
     * Add file contents to the index.
558
     *
559
     * @code
560
     * $git->add('some/file.txt');
561
     * @endcode
562
     *
563
     * @param string $filepattern
564
     *   Files to add content from. Fileglobs (e.g.  *.c) can be given to add
565
     *   all matching files. Also a leading directory name (e.g.  dir to add
566
     *   dir/file1 and dir/file2) can be given to add all files in the
567
     *   directory, recursively.
568
     * @param array $options
569
     *   An optional array of command line options.
570
     *
571
     * @return \GitWrapper\GitWorkingCopy
572
     *
573
     * @throws \GitWrapper\GitException
574
     */
575 200
    public function add($filepattern, array $options = array())
576
    {
577
        $args = array(
578 200
            'add',
579 200
            $filepattern,
580 200
            $options,
581 200
        );
582 200
        return $this->run($args);
583
    }
584
585
    /**
586
     * Executes a `git apply` command.
587
     *
588
     * Apply a patch to files and/or to the index
589
     *
590
     * @code
591
     * $git->apply('the/file/to/read/the/patch/from');
592
     * @endcode
593
     *
594
     * @param string ...
595
     *   (optional) Additional command line arguments.
596
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
597
     *   (optional) An associative array of command line options.
598
     *
599
     * @return GitWorkingCopy
600
     *
601
     * @throws GitException
602
     */
603 4
    public function apply()
604
    {
605 4
        $args = func_get_args();
606 4
        array_unshift($args, 'apply');
607 4
        return $this->run($args);
608
    }
609
610
    /**
611
     * Executes a `git bisect` command.
612
     *
613
     * Find by binary search the change that introduced a bug.
614
     *
615
     * @code
616
     * $git->bisect('good', '2.6.13-rc2');
617
     * $git->bisect('view', array('stat' => true));
618
     * @endcode
619
     *
620
     * @param string $sub_command
621
     *   The subcommand passed to `git bisect`.
622
     * @param string ...
623
     *   (optional) Additional command line arguments.
624
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
625
     *   (optional) An associative array of command line options.
626
     *
627
     * @return \GitWrapper\GitWorkingCopy
628
     *
629
     * @throws \GitWrapper\GitException
630
     */
631 4
    public function bisect($sub_command)
632
    {
633 4
        $args = func_get_args();
634 4
        $args[0] = 'bisect ' . ProcessUtils::escapeArgument($sub_command);
635 4
        return $this->run($args);
636
    }
637
638
    /**
639
     * Executes a `git branch` command.
640
     *
641
     * List, create, or delete branches.
642
     *
643
     * @code
644
     * $git->branch('my2.6.14', 'v2.6.14');
645
     * $git->branch('origin/html', 'origin/man', array('d' => true, 'r' => 'origin/todo'));
646
     * @endcode
647
     *
648
     * @param string ...
649
     *   (optional) Additional command line arguments.
650
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
651
     *   (optional) An associative array of command line options.
652
     *
653
     * @return \GitWrapper\GitWorkingCopy
654
     *
655
     * @throws \GitWrapper\GitException
656
     */
657 32
    public function branch()
658
    {
659 32
        $args = func_get_args();
660 32
        array_unshift($args, 'branch');
661 32
        return $this->run($args);
662
    }
663
664
    /**
665
     * Executes a `git checkout` command.
666
     *
667
     * Checkout a branch or paths to the working tree.
668
     *
669
     * @code
670
     * $git->checkout('new-branch', array('b' => true));
671
     * @endcode
672
     *
673
     * @param string ...
674
     *   (optional) Additional command line arguments.
675
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
676
     *   (optional) An associative array of command line options.
677
     *
678
     * @return \GitWrapper\GitWorkingCopy
679
     *
680
     * @throws \GitWrapper\GitException
681
     */
682 200
    public function checkout()
683
    {
684 200
        $args = func_get_args();
685 200
        array_unshift($args, 'checkout');
686 200
        return $this->run($args);
687
    }
688
689
    /**
690
     * Executes a `git clone` command.
691
     *
692
     * Clone a repository into a new directory. Use GitWorkingCopy::clone()
693
     * instead for more readable code.
694
     *
695
     * @code
696
     * $git->clone('git://github.com/cpliakas/git-wrapper.git');
697
     * @endcode
698
     *
699
     * @param string $repository
700
     *   The Git URL of the repository being cloned.
701
     * @param array $options
702
     *   (optional) An associative array of command line options.
703
     *
704
     * @param string $repository
705
     *   The URL of the repository being cloned.
706
     *
707
     * @return \GitWrapper\GitWorkingCopy
708
     *
709
     * @throws \GitWrapper\GitException
710
     */
711 204
    public function cloneRepository($repository, $options = array())
712
    {
713
        $args = array(
714 204
            'clone',
715 204
            $repository,
716 204
            $this->directory,
717 204
            $options,
718 204
        );
719 204
        return $this->run($args, false);
720
    }
721
722
    /**
723
     * Executes a `git commit` command.
724
     *
725
     * Record changes to the repository. If only one argument is passed, it is
726
     * assumed to be the commit message. Therefore `$git->commit('Message');`
727
     * yields a `git commit -am "Message"` command.
728
     *
729
     * @code
730
     * $git->commit('My commit message');
731
     * $git->commit('Makefile', array('m' => 'My commit message'));
732
     * @endcode
733
     *
734
     * @param string ...
735
     *   (optional) Additional command line arguments.
736
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
737
     *   (optional) An associative array of command line options.
738
     *
739
     * @return \GitWrapper\GitWorkingCopy
740
     *
741
     * @throws \GitWrapper\GitException
742
     */
743 204
    public function commit()
744
    {
745 204
        $args = func_get_args();
746 204
        if (isset($args[0]) && is_string($args[0]) && !isset($args[1])) {
747 204
            $args[0] = array(
748 204
                'm' => $args[0],
749 204
                'a' => true,
750
            );
751 204
        }
752 204
        array_unshift($args, 'commit');
753 204
        return $this->run($args);
754
    }
755
756
    /**
757
     * Executes a `git config` command.
758
     *
759
     * Get and set repository options.
760
     *
761
     * @code
762
     * $git->config('user.email', '[email protected]');
763
     * $git->config('user.name', 'Chris Pliakas');
764
     * @endcode
765
     *
766
     * @param string ...
767
     *   (optional) Additional command line arguments.
768
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
769
     *   (optional) An associative array of command line options.
770
     *
771
     * @return \GitWrapper\GitWorkingCopy
772
     *
773
     * @throws \GitWrapper\GitException
774
     */
775 200
    public function config()
776
    {
777 200
        $args = func_get_args();
778 200
        array_unshift($args, 'config');
779 200
        return $this->run($args);
780
    }
781
782
    /**
783
     * Executes a `git diff` command.
784
     *
785
     * Show changes between commits, commit and working tree, etc.
786
     *
787
     * @code
788
     * $git->diff();
789
     * $git->diff('topic', 'master');
790
     * @endcode
791
     *
792
     * @param string ...
793
     *   (optional) Additional command line arguments.
794
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
795
     *   (optional) An associative array of command line options.
796
     *
797
     * @return \GitWrapper\GitWorkingCopy
798
     *
799
     * @throws \GitWrapper\GitException
800
     */
801 4
    public function diff()
802
    {
803 4
        $args = func_get_args();
804 4
        array_unshift($args, 'diff');
805 4
        return $this->run($args);
806
    }
807
808
    /**
809
     * Executes a `git fetch` command.
810
     *
811
     * Download objects and refs from another repository.
812
     *
813
     * @code
814
     * $git->fetch('origin');
815
     * $git->fetch(array('all' => true));
816
     * @endcode
817
     *
818
     * @param string ...
819
     *   (optional) Additional command line arguments.
820
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
821
     *   (optional) An associative array of command line options.
822
     *
823
     * @return \GitWrapper\GitWorkingCopy
824
     *
825
     * @throws \GitWrapper\GitException
826
     */
827 4
    public function fetch()
828
    {
829 4
        $args = func_get_args();
830 4
        array_unshift($args, 'fetch');
831 4
        return $this->run($args);
832
    }
833
834
    /**
835
     * Executes a `git grep` command.
836
     *
837
     * Print lines matching a pattern.
838
     *
839
     * @code
840
     * $git->grep('time_t', '--', '*.[ch]');
841
     * @endcode
842
     *
843
     * @param string ...
844
     *   (optional) Additional command line arguments.
845
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
846
     *   (optional) An associative array of command line options.
847
     *
848
     * @return \GitWrapper\GitWorkingCopy
849
     *
850
     * @throws \GitWrapper\GitException
851
     */
852 4
    public function grep()
853
    {
854 4
        $args = func_get_args();
855 4
        array_unshift($args, 'grep');
856 4
        return $this->run($args);
857
    }
858
859
    /**
860
     * Executes a `git init` command.
861
     *
862
     * Create an empty git repository or reinitialize an existing one.
863
     *
864
     * @code
865
     * $git->init(array('bare' => true));
866
     * @endcode
867
     *
868
     * @param array $options
869
     *   (optional) An associative array of command line options.
870
     *
871
     * @return \GitWrapper\GitWorkingCopy
872
     *
873
     * @throws \GitWrapper\GitException
874
     */
875 204
    public function init(array $options = array())
876
    {
877
        $args = array(
878 204
            'init',
879 204
            $this->directory,
880 204
            $options,
881 204
        );
882 204
        return $this->run($args, false);
883
    }
884
885
    /**
886
     * Executes a `git log` command.
887
     *
888
     * Show commit logs.
889
     *
890
     * @code
891
     * $git->log(array('no-merges' => true));
892
     * $git->log('v2.6.12..', 'include/scsi', 'drivers/scsi');
893
     * @endcode
894
     *
895
     * @param string ...
896
     *   (optional) Additional command line arguments.
897
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
898
     *   (optional) An associative array of command line options.
899
     *
900
     * @return \GitWrapper\GitWorkingCopy
901
     *
902
     * @throws \GitWrapper\GitException
903
     */
904 8
    public function log()
905
    {
906 8
        $args = func_get_args();
907 8
        array_unshift($args, 'log');
908 8
        return $this->run($args);
909
    }
910
911
    /**
912
     * Executes a `git merge` command.
913
     *
914
     * Join two or more development histories together.
915
     *
916
     * @code
917
     * $git->merge('fixes', 'enhancements');
918
     * @endcode
919
     *
920
     * @param string ...
921
     *   (optional) Additional command line arguments.
922
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
923
     *   (optional) An associative array of command line options.
924
     *
925
     * @return \GitWrapper\GitWorkingCopy
926
     *
927
     * @throws \GitWrapper\GitException
928
     */
929 8
    public function merge()
930
    {
931 8
        $args = func_get_args();
932 8
        array_unshift($args, 'merge');
933 8
        return $this->run($args);
934
    }
935
936
    /**
937
     * Executes a `git mv` command.
938
     *
939
     * Move or rename a file, a directory, or a symlink.
940
     *
941
     * @code
942
     * $git->mv('orig.txt', 'dest.txt');
943
     * @endcode
944
     *
945
     * @param string $source
946
     *   The file / directory being moved.
947
     * @param string $destination
948
     *   The target file / directory that the source is being move to.
949
     * @param array $options
950
     *   (optional) An associative array of command line options.
951
     *
952
     * @return \GitWrapper\GitWorkingCopy
953
     *
954
     * @throws \GitWrapper\GitException
955
     */
956 4
    public function mv($source, $destination, array $options = array())
957
    {
958
        $args = array(
959 4
            'mv',
960 4
            $source,
961 4
            $destination,
962 4
            $options,
963 4
        );
964 4
        return $this->run($args);
965
    }
966
967
    /**
968
     * Executes a `git pull` command.
969
     *
970
     * Fetch from and merge with another repository or a local branch.
971
     *
972
     * @code
973
     * $git->pull('upstream', 'master');
974
     * @endcode
975
     *
976
     * @param string ...
977
     *   (optional) Additional command line arguments.
978
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
979
     *   (optional) An associative array of command line options.
980
     *
981
     * @return \GitWrapper\GitWorkingCopy
982
     *
983
     * @throws \GitWrapper\GitException
984
     */
985 4
    public function pull()
986
    {
987 4
        $args = func_get_args();
988 4
        array_unshift($args, 'pull');
989 4
        return $this->run($args);
990
    }
991
992
    /**
993
     * Executes a `git push` command.
994
     *
995
     * Update remote refs along with associated objects.
996
     *
997
     * @code
998
     * $git->push('upstream', 'master');
999
     * @endcode
1000
     *
1001
     * @param string ...
1002
     *   (optional) Additional command line arguments.
1003
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1004
     *   (optional) An associative array of command line options.
1005
     *
1006
     * @return \GitWrapper\GitWorkingCopy
1007
     *
1008
     * @throws \GitWrapper\GitException
1009
     */
1010 200
    public function push()
1011
    {
1012 200
        $args = func_get_args();
1013 200
        array_unshift($args, 'push');
1014 200
        return $this->run($args);
1015
    }
1016
1017
    /**
1018
     * Executes a `git rebase` command.
1019
     *
1020
     * Forward-port local commits to the updated upstream head.
1021
     *
1022
     * @code
1023
     * $git->rebase('subsystem@{1}', array('onto' => 'subsystem'));
1024
     * @endcode
1025
     *
1026
     * @param string ...
1027
     *   (optional) Additional command line arguments.
1028
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1029
     *   (optional) An associative array of command line options.
1030
     *
1031
     * @return \GitWrapper\GitWorkingCopy
1032
     *
1033
     * @throws \GitWrapper\GitException
1034
     */
1035 4
    public function rebase()
1036
    {
1037 4
        $args = func_get_args();
1038 4
        array_unshift($args, 'rebase');
1039 4
        return $this->run($args);
1040
    }
1041
1042
    /**
1043
     * Executes a `git remote` command.
1044
     *
1045
     * Manage the set of repositories ("remotes") whose branches you track.
1046
     *
1047
     * @code
1048
     * $git->remote('add', 'upstream', 'git://github.com/cpliakas/git-wrapper.git');
1049
     * @endcode
1050
     *
1051
     * @param string ...
1052
     *   (optional) Additional command line arguments.
1053
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1054
     *   (optional) An associative array of command line options.
1055
     *
1056
     * @return \GitWrapper\GitWorkingCopy
1057
     *
1058
     * @throws \GitWrapper\GitException
1059
     */
1060 60
    public function remote()
1061
    {
1062 60
        $args = func_get_args();
1063 60
        array_unshift($args, 'remote');
1064 60
        return $this->run($args);
1065
    }
1066
1067
    /**
1068
     * Executes a `git reset` command.
1069
     *
1070
     * Reset current HEAD to the specified state.
1071
     *
1072
     * @code
1073
     * $git->reset(array('hard' => true));
1074
     * @endcode
1075
     *
1076
     * @param string ...
1077
     *   (optional) Additional command line arguments.
1078
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1079
     *   (optional) An associative array of command line options.
1080
     *
1081
     * @return \GitWrapper\GitWorkingCopy
1082
     *
1083
     * @throws \GitWrapper\GitException
1084
     */
1085 16
    public function reset()
1086
    {
1087 16
        $args = func_get_args();
1088 16
        array_unshift($args, 'reset');
1089 16
        return $this->run($args);
1090
    }
1091
1092
    /**
1093
     * Executes a `git rm` command.
1094
     *
1095
     * Remove files from the working tree and from the index.
1096
     *
1097
     * @code
1098
     * $git->rm('oldfile.txt');
1099
     * @endcode
1100
     *
1101
     * @param string $filepattern
1102
     *   Files to remove from version control. Fileglobs (e.g.  *.c) can be
1103
     *   given to add all matching files. Also a leading directory name (e.g.
1104
     *   dir to add dir/file1 and dir/file2) can be given to add all files in
1105
     *   the directory, recursively.
1106
     * @param array $options
1107
     *   (optional) An associative array of command line options.
1108
     *
1109
     * @return \GitWrapper\GitWorkingCopy
1110
     *
1111
     * @throws \GitWrapper\GitException
1112
     */
1113 4
    public function rm($filepattern, array $options = array())
1114
    {
1115
        $args = array(
1116 4
            'rm',
1117 4
            $filepattern,
1118 4
            $options,
1119 4
        );
1120 4
        return $this->run($args);
1121
    }
1122
1123
    /**
1124
     * Executes a `git show` command.
1125
     *
1126
     * Show various types of objects.
1127
     *
1128
     * @code
1129
     * $git->show('v1.0.0');
1130
     * @endcode
1131
     *
1132
     * @param string $object
1133
     *   The names of objects to show. For a more complete list of ways to spell
1134
     *   object names, see "SPECIFYING REVISIONS" section in gitrevisions(7).
1135
     * @param array $options
1136
     *   (optional) An associative array of command line options.
1137
     *
1138
     * @return \GitWrapper\GitWorkingCopy
1139
     *
1140
     * @throws \GitWrapper\GitException
1141
     */
1142 4
    public function show($object, array $options = array())
1143
    {
1144 4
        $args = array('show', $object, $options);
1145 4
        return $this->run($args);
1146
    }
1147
1148
    /**
1149
     * Executes a `git status` command.
1150
     *
1151
     * Show the working tree status.
1152
     *
1153
     * @code
1154
     * $git->status(array('s' => true));
1155
     * @endcode
1156
     *
1157
     * @param string ...
1158
     *   (optional) Additional command line arguments.
1159
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1160
     *   (optional) An associative array of command line options.
1161
     *
1162
     * @return \GitWrapper\GitWorkingCopy
1163
     *
1164
     * @throws \GitWrapper\GitException
1165
     */
1166 20
    public function status()
1167
    {
1168 20
        $args = func_get_args();
1169 20
        array_unshift($args, 'status');
1170 20
        return $this->run($args);
1171
    }
1172
1173
    /**
1174
     * Executes a `git tag` command.
1175
     *
1176
     * Create, list, delete or verify a tag object signed with GPG.
1177
1178
     * @code
1179
     * $git->tag('v1.0.0');
1180
     * @endcode
1181
     *
1182
     * @param string ...
1183
     *   (optional) Additional command line arguments.
1184
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1185
     *   (optional) An associative array of command line options.
1186
     *
1187
     * @return \GitWrapper\GitWorkingCopy
1188
     *
1189
     * @throws \GitWrapper\GitException
1190
     */
1191 200
    public function tag()
1192
    {
1193 200
        $args = func_get_args();
1194 200
        array_unshift($args, 'tag');
1195 200
        return $this->run($args);
1196
    }
1197
1198
    /**
1199
     * Executes a `git clean` command.
1200
     *
1201
     * Remove untracked files from the working tree
1202
     *
1203
     * @code
1204
     * $git->clean('-d', '-f');
1205
     * @endcode
1206
     *
1207
     * @param string ...
1208
     *   (optional) Additional command line arguments.
1209
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1210
     *   (optional) An associative array of command line options.
1211
     *
1212
     * @return \GitWrapper\GitWorkingCopy
1213
     *
1214
     * @throws \GitWrapper\GitException
1215
     */
1216 4
    public function clean()
1217
    {
1218 4
        $args = func_get_args();
1219 4
        array_unshift($args, 'clean');
1220 4
        return $this->run($args);
1221
    }
1222
1223
     /**
1224
     * Executes a `git archive` command.
1225
     *
1226
     * Create an archive of files from a named tree
1227
     *
1228
     * @code
1229
     * $git->archive('HEAD', array('o' => '/path/to/archive'));
1230
     * @endcode
1231
     *
1232
     * @param string ...
1233
     *   (optional) Additional command line arguments.
1234
     * @param array $options
0 ignored issues
show
Bug introduced by
There is no parameter named $options. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1235
     *   (optional) An associative array of command line options.
1236
     *
1237
     * @return \GitWrapper\GitWorkingCopy
1238
     *
1239
     * @throws \GitWrapper\GitException
1240
     */
1241 4
    public function archive()
1242
    {
1243 4
        $args = func_get_args();
1244 4
        array_unshift($args, 'archive');
1245 4
        return $this->run($args);
1246
    }
1247
1248
    /**
1249
     * @} End of "defgroup command".
1250
     */
1251
1252
    /**
1253
     * Hackish, allows us to use "clone" as a method name.
1254
     *
1255
     * $throws \BadMethodCallException
1256
     * @throws \GitWrapper\GitException
1257
     */
1258 204 View Code Duplication
    public function __call($method, $args)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1259
    {
1260 204
        if ('clone' == $method) {
1261 204
            return call_user_func_array(array($this, 'cloneRepository'), $args);
1262
        } else {
1263 4
            $class = get_called_class();
1264 4
            $message = "Call to undefined method $class::$method()";
1265 4
            throw new \BadMethodCallException($message);
1266
        }
1267
    }
1268
1269
    /**
1270
     * Gets the output captured by the last run Git commnd(s).
1271
     *
1272
     * @return string
1273
     *
1274
     * @see GitWorkingCopy::getOutput()
1275
     */
1276 112
    public function __toString()
1277
    {
1278 112
        return $this->getOutput();
1279
    }
1280
}
1281