Completed
Pull Request — master (#112)
by Pieter
04:21 queued 27s
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 196
    public function __construct(GitWrapper $wrapper, $directory)
55
    {
56 196
        $this->wrapper = $wrapper;
57 196
        $this->directory = $directory;
58 196
    }
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 132
    public function getOutput()
86
    {
87 132
        $output = $this->output;
88 132
        $this->output = '';
89 132
        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 184
    public function clearOutput()
98
    {
99 184
        $this->output = '';
100 184
        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 192
    public function setCloned($cloned)
112
    {
113 192
        $this->cloned = (bool) $cloned;
114 192
        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 192
    public function run($args, $setDirectory = true)
151
    {
152 192
        $command = call_user_func_array(array('GitWrapper\GitCommand', 'getInstance'), $args);
153 192
        if ($setDirectory) {
154 188
            $command->setDirectory($this->directory);
155 188
        }
156 192
        $this->output .= $this->wrapper->run($command);
157 192
        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 4
    public function getBranches()
305
    {
306 4
        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 184
    public function pushTags($repository = 'origin', array $options = array())
343
    {
344 184
        $options['tags'] = true;
345 184
        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 184
    public function checkoutNewBranch($branch, array $options = array())
375
    {
376 184
        $options['b'] = true;
377 184
        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 32
    public function addRemote($name, $url, $options = array()) {
409 32
        if (empty($name)) {
410
            throw new GitException('Cannot add remote without a name.');
411
        }
412 32
        if (empty($url)) {
413
            throw new GitException('Cannot add remote without a URL.');
414
        }
415
416 32
        $args = array('add');
417
418
        // Add boolean options.
419 32
        foreach (array('-f', '--tags', '--no-tags') as $option) {
420 32
            if (!empty($options[$option])) {
421 20
                $args[] = $option;
422 20
            }
423 32
        }
424
425
        // Add tracking branches.
426 32
        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 32
        if (!empty($options['-m'])) {
434 4
            array_push($args, '-m', $options['-m']);
435 4
        }
436
437
        // Add remote name and URL.
438 32
        array_push($args, $name, $url);
439
440 32
        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
    public function removeRemote($name) {
452
        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
        $remotes = $this->getRemotes();
486
        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'] = rtrim($this->remote('get-url', $remote)->getOutput());
504
            $remotes[$remote]['push'] = rtrim($this->remote('get-url', '--push', $remote)->getOutput());
505
        }
506
        return $remotes;
507
    }
508
509
    /**
510
     * @} End of "defgroup command_helpers".
511
     */
512
513
    /**
514
     * @defgroup commands Git Commands
515
     *
516
     * All methods in this group correspond with Git commands, for example
517
     * "git add", "git commit", "git push", etc.
518
     *
519
     * @{
520
     */
521
522
    /**
523
     * Executes a `git add` command.
524
     *
525
     * Add file contents to the index.
526
     *
527
     * @code
528
     * $git->add('some/file.txt');
529
     * @endcode
530
     *
531
     * @param string $filepattern
532
     *   Files to add content from. Fileglobs (e.g.  *.c) can be given to add
533
     *   all matching files. Also a leading directory name (e.g.  dir to add
534
     *   dir/file1 and dir/file2) can be given to add all files in the
535
     *   directory, recursively.
536
     * @param array $options
537
     *   An optional array of command line options.
538
     *
539
     * @return \GitWrapper\GitWorkingCopy
540
     *
541
     * @throws \GitWrapper\GitException
542
     */
543 184
    public function add($filepattern, array $options = array())
544
    {
545
        $args = array(
546 184
            'add',
547 184
            $filepattern,
548 184
            $options,
549 184
        );
550 184
        return $this->run($args);
551
    }
552
553
    /**
554
     * Executes a `git apply` command.
555
     *
556
     * Apply a patch to files and/or to the index
557
     *
558
     * @code
559
     * $git->apply('the/file/to/read/the/patch/from');
560
     * @endcode
561
     *
562
     * @param string ...
563
     *   (optional) Additional command line arguments.
564
     * @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...
565
     *   (optional) An associative array of command line options.
566
     *
567
     * @return GitWorkingCopy
568
     *
569
     * @throws GitException
570
     */
571 4
    public function apply()
572
    {
573 4
        $args = func_get_args();
574 4
        array_unshift($args, 'apply');
575 4
        return $this->run($args);
576
    }
577
578
    /**
579
     * Executes a `git bisect` command.
580
     *
581
     * Find by binary search the change that introduced a bug.
582
     *
583
     * @code
584
     * $git->bisect('good', '2.6.13-rc2');
585
     * $git->bisect('view', array('stat' => true));
586
     * @endcode
587
     *
588
     * @param string $sub_command
589
     *   The subcommand passed to `git bisect`.
590
     * @param string ...
591
     *   (optional) Additional command line arguments.
592
     * @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...
593
     *   (optional) An associative array of command line options.
594
     *
595
     * @return \GitWrapper\GitWorkingCopy
596
     *
597
     * @throws \GitWrapper\GitException
598
     */
599 4
    public function bisect($sub_command)
600
    {
601 4
        $args = func_get_args();
602 4
        $args[0] = 'bisect ' . ProcessUtils::escapeArgument($sub_command);
603 4
        return $this->run($args);
604
    }
605
606
    /**
607
     * Executes a `git branch` command.
608
     *
609
     * List, create, or delete branches.
610
     *
611
     * @code
612
     * $git->branch('my2.6.14', 'v2.6.14');
613
     * $git->branch('origin/html', 'origin/man', array('d' => true, 'r' => 'origin/todo'));
614
     * @endcode
615
     *
616
     * @param string ...
617
     *   (optional) Additional command line arguments.
618
     * @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...
619
     *   (optional) An associative array of command line options.
620
     *
621
     * @return \GitWrapper\GitWorkingCopy
622
     *
623
     * @throws \GitWrapper\GitException
624
     */
625 8
    public function branch()
626
    {
627 8
        $args = func_get_args();
628 8
        array_unshift($args, 'branch');
629 8
        return $this->run($args);
630
    }
631
632
    /**
633
     * Executes a `git checkout` command.
634
     *
635
     * Checkout a branch or paths to the working tree.
636
     *
637
     * @code
638
     * $git->checkout('new-branch', array('b' => true));
639
     * @endcode
640
     *
641
     * @param string ...
642
     *   (optional) Additional command line arguments.
643
     * @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...
644
     *   (optional) An associative array of command line options.
645
     *
646
     * @return \GitWrapper\GitWorkingCopy
647
     *
648
     * @throws \GitWrapper\GitException
649
     */
650 184
    public function checkout()
651
    {
652 184
        $args = func_get_args();
653 184
        array_unshift($args, 'checkout');
654 184
        return $this->run($args);
655
    }
656
657
    /**
658
     * Executes a `git clone` command.
659
     *
660
     * Clone a repository into a new directory. Use GitWorkingCopy::clone()
661
     * instead for more readable code.
662
     *
663
     * @code
664
     * $git->clone('git://github.com/cpliakas/git-wrapper.git');
665
     * @endcode
666
     *
667
     * @param string $repository
668
     *   The Git URL of the repository being cloned.
669
     * @param array $options
670
     *   (optional) An associative array of command line options.
671
     *
672
     * @param string $repository
673
     *   The URL of the repository being cloned.
674
     *
675
     * @return \GitWrapper\GitWorkingCopy
676
     *
677
     * @throws \GitWrapper\GitException
678
     */
679 188
    public function cloneRepository($repository, $options = array())
680
    {
681
        $args = array(
682 188
            'clone',
683 188
            $repository,
684 188
            $this->directory,
685 188
            $options,
686 188
        );
687 188
        return $this->run($args, false);
688
    }
689
690
    /**
691
     * Executes a `git commit` command.
692
     *
693
     * Record changes to the repository. If only one argument is passed, it is
694
     * assumed to be the commit message. Therefore `$git->commit('Message');`
695
     * yields a `git commit -am "Message"` command.
696
     *
697
     * @code
698
     * $git->commit('My commit message');
699
     * $git->commit('Makefile', array('m' => 'My commit message'));
700
     * @endcode
701
     *
702
     * @param string ...
703
     *   (optional) Additional command line arguments.
704
     * @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...
705
     *   (optional) An associative array of command line options.
706
     *
707
     * @return \GitWrapper\GitWorkingCopy
708
     *
709
     * @throws \GitWrapper\GitException
710
     */
711 188
    public function commit()
712
    {
713 188
        $args = func_get_args();
714 188
        if (isset($args[0]) && is_string($args[0]) && !isset($args[1])) {
715 188
            $args[0] = array(
716 188
                'm' => $args[0],
717 188
                'a' => true,
718
            );
719 188
        }
720 188
        array_unshift($args, 'commit');
721 188
        return $this->run($args);
722
    }
723
724
    /**
725
     * Executes a `git config` command.
726
     *
727
     * Get and set repository options.
728
     *
729
     * @code
730
     * $git->config('user.email', '[email protected]');
731
     * $git->config('user.name', 'Chris Pliakas');
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 184
    public function config()
744
    {
745 184
        $args = func_get_args();
746 184
        array_unshift($args, 'config');
747 184
        return $this->run($args);
748
    }
749
750
    /**
751
     * Executes a `git diff` command.
752
     *
753
     * Show changes between commits, commit and working tree, etc.
754
     *
755
     * @code
756
     * $git->diff();
757
     * $git->diff('topic', 'master');
758
     * @endcode
759
     *
760
     * @param string ...
761
     *   (optional) Additional command line arguments.
762
     * @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...
763
     *   (optional) An associative array of command line options.
764
     *
765
     * @return \GitWrapper\GitWorkingCopy
766
     *
767
     * @throws \GitWrapper\GitException
768
     */
769 4
    public function diff()
770
    {
771 4
        $args = func_get_args();
772 4
        array_unshift($args, 'diff');
773 4
        return $this->run($args);
774
    }
775
776
    /**
777
     * Executes a `git fetch` command.
778
     *
779
     * Download objects and refs from another repository.
780
     *
781
     * @code
782
     * $git->fetch('origin');
783
     * $git->fetch(array('all' => true));
784
     * @endcode
785
     *
786
     * @param string ...
787
     *   (optional) Additional command line arguments.
788
     * @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...
789
     *   (optional) An associative array of command line options.
790
     *
791
     * @return \GitWrapper\GitWorkingCopy
792
     *
793
     * @throws \GitWrapper\GitException
794
     */
795 4
    public function fetch()
796
    {
797 4
        $args = func_get_args();
798 4
        array_unshift($args, 'fetch');
799 4
        return $this->run($args);
800
    }
801
802
    /**
803
     * Executes a `git grep` command.
804
     *
805
     * Print lines matching a pattern.
806
     *
807
     * @code
808
     * $git->grep('time_t', '--', '*.[ch]');
809
     * @endcode
810
     *
811
     * @param string ...
812
     *   (optional) Additional command line arguments.
813
     * @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...
814
     *   (optional) An associative array of command line options.
815
     *
816
     * @return \GitWrapper\GitWorkingCopy
817
     *
818
     * @throws \GitWrapper\GitException
819
     */
820 4
    public function grep()
821
    {
822 4
        $args = func_get_args();
823 4
        array_unshift($args, 'grep');
824 4
        return $this->run($args);
825
    }
826
827
    /**
828
     * Executes a `git init` command.
829
     *
830
     * Create an empty git repository or reinitialize an existing one.
831
     *
832
     * @code
833
     * $git->init(array('bare' => true));
834
     * @endcode
835
     *
836
     * @param array $options
837
     *   (optional) An associative array of command line options.
838
     *
839
     * @return \GitWrapper\GitWorkingCopy
840
     *
841
     * @throws \GitWrapper\GitException
842
     */
843 188
    public function init(array $options = array())
844
    {
845
        $args = array(
846 188
            'init',
847 188
            $this->directory,
848 188
            $options,
849 188
        );
850 188
        return $this->run($args, false);
851
    }
852
853
    /**
854
     * Executes a `git log` command.
855
     *
856
     * Show commit logs.
857
     *
858
     * @code
859
     * $git->log(array('no-merges' => true));
860
     * $git->log('v2.6.12..', 'include/scsi', 'drivers/scsi');
861
     * @endcode
862
     *
863
     * @param string ...
864
     *   (optional) Additional command line arguments.
865
     * @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...
866
     *   (optional) An associative array of command line options.
867
     *
868
     * @return \GitWrapper\GitWorkingCopy
869
     *
870
     * @throws \GitWrapper\GitException
871
     */
872 8
    public function log()
873
    {
874 8
        $args = func_get_args();
875 8
        array_unshift($args, 'log');
876 8
        return $this->run($args);
877
    }
878
879
    /**
880
     * Executes a `git merge` command.
881
     *
882
     * Join two or more development histories together.
883
     *
884
     * @code
885
     * $git->merge('fixes', 'enhancements');
886
     * @endcode
887
     *
888
     * @param string ...
889
     *   (optional) Additional command line arguments.
890
     * @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...
891
     *   (optional) An associative array of command line options.
892
     *
893
     * @return \GitWrapper\GitWorkingCopy
894
     *
895
     * @throws \GitWrapper\GitException
896
     */
897 8
    public function merge()
898
    {
899 8
        $args = func_get_args();
900 8
        array_unshift($args, 'merge');
901 8
        return $this->run($args);
902
    }
903
904
    /**
905
     * Executes a `git mv` command.
906
     *
907
     * Move or rename a file, a directory, or a symlink.
908
     *
909
     * @code
910
     * $git->mv('orig.txt', 'dest.txt');
911
     * @endcode
912
     *
913
     * @param string $source
914
     *   The file / directory being moved.
915
     * @param string $destination
916
     *   The target file / directory that the source is being move to.
917
     * @param array $options
918
     *   (optional) An associative array of command line options.
919
     *
920
     * @return \GitWrapper\GitWorkingCopy
921
     *
922
     * @throws \GitWrapper\GitException
923
     */
924 4
    public function mv($source, $destination, array $options = array())
925
    {
926
        $args = array(
927 4
            'mv',
928 4
            $source,
929 4
            $destination,
930 4
            $options,
931 4
        );
932 4
        return $this->run($args);
933
    }
934
935
    /**
936
     * Executes a `git pull` command.
937
     *
938
     * Fetch from and merge with another repository or a local branch.
939
     *
940
     * @code
941
     * $git->pull('upstream', 'master');
942
     * @endcode
943
     *
944
     * @param string ...
945
     *   (optional) Additional command line arguments.
946
     * @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...
947
     *   (optional) An associative array of command line options.
948
     *
949
     * @return \GitWrapper\GitWorkingCopy
950
     *
951
     * @throws \GitWrapper\GitException
952
     */
953 4
    public function pull()
954
    {
955 4
        $args = func_get_args();
956 4
        array_unshift($args, 'pull');
957 4
        return $this->run($args);
958
    }
959
960
    /**
961
     * Executes a `git push` command.
962
     *
963
     * Update remote refs along with associated objects.
964
     *
965
     * @code
966
     * $git->push('upstream', 'master');
967
     * @endcode
968
     *
969
     * @param string ...
970
     *   (optional) Additional command line arguments.
971
     * @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...
972
     *   (optional) An associative array of command line options.
973
     *
974
     * @return \GitWrapper\GitWorkingCopy
975
     *
976
     * @throws \GitWrapper\GitException
977
     */
978 184
    public function push()
979
    {
980 184
        $args = func_get_args();
981 184
        array_unshift($args, 'push');
982 184
        return $this->run($args);
983
    }
984
985
    /**
986
     * Executes a `git rebase` command.
987
     *
988
     * Forward-port local commits to the updated upstream head.
989
     *
990
     * @code
991
     * $git->rebase('subsystem@{1}', array('onto' => 'subsystem'));
992
     * @endcode
993
     *
994
     * @param string ...
995
     *   (optional) Additional command line arguments.
996
     * @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...
997
     *   (optional) An associative array of command line options.
998
     *
999
     * @return \GitWrapper\GitWorkingCopy
1000
     *
1001
     * @throws \GitWrapper\GitException
1002
     */
1003 4
    public function rebase()
1004
    {
1005 4
        $args = func_get_args();
1006 4
        array_unshift($args, 'rebase');
1007 4
        return $this->run($args);
1008
    }
1009
1010
    /**
1011
     * Executes a `git remote` command.
1012
     *
1013
     * Manage the set of repositories ("remotes") whose branches you track.
1014
     *
1015
     * @code
1016
     * $git->remote('add', 'upstream', 'git://github.com/cpliakas/git-wrapper.git');
1017
     * @endcode
1018
     *
1019
     * @param string ...
1020
     *   (optional) Additional command line arguments.
1021
     * @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...
1022
     *   (optional) An associative array of command line options.
1023
     *
1024
     * @return \GitWrapper\GitWorkingCopy
1025
     *
1026
     * @throws \GitWrapper\GitException
1027
     */
1028 44
    public function remote()
1029
    {
1030 44
        $args = func_get_args();
1031 44
        array_unshift($args, 'remote');
1032 44
        return $this->run($args);
1033
    }
1034
1035
    /**
1036
     * Executes a `git reset` command.
1037
     *
1038
     * Reset current HEAD to the specified state.
1039
     *
1040
     * @code
1041
     * $git->reset(array('hard' => true));
1042
     * @endcode
1043
     *
1044
     * @param string ...
1045
     *   (optional) Additional command line arguments.
1046
     * @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...
1047
     *   (optional) An associative array of command line options.
1048
     *
1049
     * @return \GitWrapper\GitWorkingCopy
1050
     *
1051
     * @throws \GitWrapper\GitException
1052
     */
1053 16
    public function reset()
1054
    {
1055 16
        $args = func_get_args();
1056 16
        array_unshift($args, 'reset');
1057 16
        return $this->run($args);
1058
    }
1059
1060
    /**
1061
     * Executes a `git rm` command.
1062
     *
1063
     * Remove files from the working tree and from the index.
1064
     *
1065
     * @code
1066
     * $git->rm('oldfile.txt');
1067
     * @endcode
1068
     *
1069
     * @param string $filepattern
1070
     *   Files to remove from version control. Fileglobs (e.g.  *.c) can be
1071
     *   given to add all matching files. Also a leading directory name (e.g.
1072
     *   dir to add dir/file1 and dir/file2) can be given to add all files in
1073
     *   the directory, recursively.
1074
     * @param array $options
1075
     *   (optional) An associative array of command line options.
1076
     *
1077
     * @return \GitWrapper\GitWorkingCopy
1078
     *
1079
     * @throws \GitWrapper\GitException
1080
     */
1081 4
    public function rm($filepattern, array $options = array())
1082
    {
1083
        $args = array(
1084 4
            'rm',
1085 4
            $filepattern,
1086 4
            $options,
1087 4
        );
1088 4
        return $this->run($args);
1089
    }
1090
1091
    /**
1092
     * Executes a `git show` command.
1093
     *
1094
     * Show various types of objects.
1095
     *
1096
     * @code
1097
     * $git->show('v1.0.0');
1098
     * @endcode
1099
     *
1100
     * @param string $object
1101
     *   The names of objects to show. For a more complete list of ways to spell
1102
     *   object names, see "SPECIFYING REVISIONS" section in gitrevisions(7).
1103
     * @param array $options
1104
     *   (optional) An associative array of command line options.
1105
     *
1106
     * @return \GitWrapper\GitWorkingCopy
1107
     *
1108
     * @throws \GitWrapper\GitException
1109
     */
1110 4
    public function show($object, array $options = array())
1111
    {
1112 4
        $args = array('show', $object, $options);
1113 4
        return $this->run($args);
1114
    }
1115
1116
    /**
1117
     * Executes a `git status` command.
1118
     *
1119
     * Show the working tree status.
1120
     *
1121
     * @code
1122
     * $git->status(array('s' => true));
1123
     * @endcode
1124
     *
1125
     * @param string ...
1126
     *   (optional) Additional command line arguments.
1127
     * @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...
1128
     *   (optional) An associative array of command line options.
1129
     *
1130
     * @return \GitWrapper\GitWorkingCopy
1131
     *
1132
     * @throws \GitWrapper\GitException
1133
     */
1134 20
    public function status()
1135
    {
1136 20
        $args = func_get_args();
1137 20
        array_unshift($args, 'status');
1138 20
        return $this->run($args);
1139
    }
1140
1141
    /**
1142
     * Executes a `git tag` command.
1143
     *
1144
     * Create, list, delete or verify a tag object signed with GPG.
1145
1146
     * @code
1147
     * $git->tag('v1.0.0');
1148
     * @endcode
1149
     *
1150
     * @param string ...
1151
     *   (optional) Additional command line arguments.
1152
     * @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...
1153
     *   (optional) An associative array of command line options.
1154
     *
1155
     * @return \GitWrapper\GitWorkingCopy
1156
     *
1157
     * @throws \GitWrapper\GitException
1158
     */
1159 184
    public function tag()
1160
    {
1161 184
        $args = func_get_args();
1162 184
        array_unshift($args, 'tag');
1163 184
        return $this->run($args);
1164
    }
1165
1166
    /**
1167
     * Executes a `git clean` command.
1168
     *
1169
     * Remove untracked files from the working tree
1170
     *
1171
     * @code
1172
     * $git->clean('-d', '-f');
1173
     * @endcode
1174
     *
1175
     * @param string ...
1176
     *   (optional) Additional command line arguments.
1177
     * @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...
1178
     *   (optional) An associative array of command line options.
1179
     *
1180
     * @return \GitWrapper\GitWorkingCopy
1181
     *
1182
     * @throws \GitWrapper\GitException
1183
     */
1184 4
    public function clean()
1185
    {
1186 4
        $args = func_get_args();
1187 4
        array_unshift($args, 'clean');
1188 4
        return $this->run($args);
1189
    }
1190
1191
     /**
1192
     * Executes a `git archive` command.
1193
     *
1194
     * Create an archive of files from a named tree
1195
     *
1196
     * @code
1197
     * $git->archive('HEAD', array('o' => '/path/to/archive'));
1198
     * @endcode
1199
     *
1200
     * @param string ...
1201
     *   (optional) Additional command line arguments.
1202
     * @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...
1203
     *   (optional) An associative array of command line options.
1204
     *
1205
     * @return \GitWrapper\GitWorkingCopy
1206
     *
1207
     * @throws \GitWrapper\GitException
1208
     */
1209 4
    public function archive()
1210
    {
1211 4
        $args = func_get_args();
1212 4
        array_unshift($args, 'archive');
1213 4
        return $this->run($args);
1214
    }
1215
1216
    /**
1217
     * @} End of "defgroup command".
1218
     */
1219
1220
    /**
1221
     * Hackish, allows us to use "clone" as a method name.
1222
     *
1223
     * $throws \BadMethodCallException
1224
     * @throws \GitWrapper\GitException
1225
     */
1226 188 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...
1227
    {
1228 188
        if ('clone' == $method) {
1229 188
            return call_user_func_array(array($this, 'cloneRepository'), $args);
1230
        } else {
1231 4
            $class = get_called_class();
1232 4
            $message = "Call to undefined method $class::$method()";
1233 4
            throw new \BadMethodCallException($message);
1234
        }
1235
    }
1236
1237
    /**
1238
     * Gets the output captured by the last run Git commnd(s).
1239
     *
1240
     * @return string
1241
     *
1242
     * @see GitWorkingCopy::getOutput()
1243
     */
1244 88
    public function __toString()
1245
    {
1246 88
        return $this->getOutput();
1247
    }
1248
}
1249