Completed
Push — develop ( 61d4f1...4b8b90 )
by
unknown
9s
created

CloneCommand::cloneUrl()   B

Complexity

Conditions 9
Paths 26

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9.0945

Importance

Changes 0
Metric Value
dl 0
loc 37
ccs 17
cts 19
cp 0.8947
rs 7.7724
c 0
b 0
f 0
cc 9
nc 26
nop 5
crap 9.0945
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
     */
40 3
    public function __construct(Repository $repo = null)
41
    {
42 3
        parent::__construct($repo);
43 3
    }
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
     *
54
     * @throws \RuntimeException
55
     * @return string command
56
     */
57 3
    public function cloneUrl(string $url, string $to = null, string $repoReference = null, int $depth = null, bool $recursive = false)
58
    {
59
        // get binary version before reset
60 3
        $v = $this->getBinaryVersion();
61
62 3
        $this->clearAll();
63 3
        $this->addCommandName(static::GIT_CLONE_COMMAND);
64 3
        $this->addCommandSubject($url);
65 3
        if (null !== $to) {
66 3
            $this->addCommandSubject2($to);
67
        }
68
69 3
        if (null !== $repoReference) {
70
            // git documentation says the --branch was added in 2.0.0, but it exists undocumented at least back to 1.8.3.1
71 2
            if (version_compare($v, '1.8.3.1', '<')) {
72
                throw new \RuntimeException(
73
                    'Please upgrade to git v1.8.3.1 or newer to support cloning a specific branch. You have ' . $v . '.'
74
                );
75
            }
76 2
            $this->addCommandArgument('--branch=' . $repoReference);
77
        }
78
79 3
        if (null !== $depth) {
80 2
            $this->addCommandArgument('--depth=' . $depth);
81
            // shallow-submodules is a nice to have feature. Just ignoring if git version not high enough
82
            // It would be nice if this had a logger injected for us to log notices
83 2
            if (version_compare($v, '2.9.0', '>=') && $recursive && 1 == $depth) {
84 1
                $this->addCommandArgument('--shallow-submodules');
85
            }
86
        }
87
88 3
        if ($recursive) {
89 1
            $this->addCommandArgument('--recursive');
90
        }
91
92 3
        return $this->getCommand();
93
    }
94
}
95