Completed
Pull Request — develop (#148)
by
unknown
10:27
created

CloneCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * GitElephant - An abstraction layer for git written in PHP
4
 * Copyright (C) 2013  Matteo Giachino
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see [http://www.gnu.org/licenses/].
18
 */
19
20
namespace GitElephant\Command;
21
22
use GitElephant\Repository;
23
24
/**
25
 * CloneCommand generator
26
 *
27
 * @author Matteo Giachino <[email protected]>
28
 * @author Kirk Madera <[email protected]>
29
 */
30
class CloneCommand extends BaseCommand
31
{
32
    const GIT_CLONE_COMMAND = 'clone';
33
34
    /**
35
     * constructor
36
     *
37
     * @param \GitElephant\Repository $repo The repository object this command
38
     *                                      will interact with
39 3
     */
40
    public function __construct(Repository $repo = null)
41 3
    {
42 3
        parent::__construct($repo);
43
    }
44
45
    /**
46
     * Command to clone a repository
47
     *
48
     * @param string      $url           repository url
49
     * @param string      $to            where to clone the repo
50
     * @param string|null $repoReference Repo reference to clone. Required if performing a shallow clone.
51
     * @param int|null    $depth         Depth of commits to clone
52
     * @param bool        $recursive     Whether to recursively clone submodules.
53 3
     *
54
     * @throws \RuntimeException
55 3
     * @return string command
56 3
     */
57 3
    public function cloneUrl(string $url, string $to = null, string $repoReference = null, int $depth = null, bool $recursive = false)
58 3
    {
59 3
        $this->clearAll();
60
        $this->addCommandName(static::GIT_CLONE_COMMAND);
61
        $this->addCommandSubject($url);
62 3
        if (null !== $to) {
63
            $this->addCommandSubject2($to);
64
        }
65
66
        if (null !== $repoReference) {
67
            // git documentation says the --branch was added in 2.0.0, but it exists undocumented at least back to 1.8.3.1
68
            if (version_compare($binaryVersion, '1.8.3.1', '<')) {
0 ignored issues
show
Bug introduced by
The variable $binaryVersion does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
69
                throw new \RuntimeException(
70
                    'Please upgrade to git v1.8.3.1 or newer to support cloning a specific branch.'
71
                );
72
            }
73
            $this->addCommandArgument('--branch=' . $repoReference);
74
        }
75
76
        if (null !== $depth) {
77
            $this->addCommandArgument('--depth=' . $depth);
78
            // shallow-submodules is a nice to have feature. Just ignoring if git version not high enough
79
            // It would be nice if this had a logger injected for us to log notices
80
            if (version_compare($binaryVersion, '2.9.0', '>=') && $recursive && 1 == $depth) {
81
                $this->addCommandArgument('--shallow-submodules');
82
            }
83
        }
84
85
        if ($recursive) {
86
            $this->addCommandArgument('--recursive');
87
        }
88
89
        return $this->getCommand();
90
    }
91
}
92