1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GitElephant - An abstraction layer for git written in PHP |
5
|
|
|
* Copyright (C) 2013 Matteo Giachino |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program. If not, see [http://www.gnu.org/licenses/]. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace GitElephant\Command; |
22
|
|
|
|
23
|
|
|
use GitElephant\Repository; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* CloneCommand generator |
27
|
|
|
* |
28
|
|
|
* @author Matteo Giachino <[email protected]> |
29
|
|
|
* @author Kirk Madera <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class CloneCommand extends BaseCommand |
32
|
|
|
{ |
33
|
|
|
public const GIT_CLONE_COMMAND = 'clone'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* constructor |
37
|
|
|
* |
38
|
|
|
* @param \GitElephant\Repository $repo The repository object this command |
39
|
|
|
* will interact with |
40
|
|
|
*/ |
41
|
3 |
|
public function __construct(Repository $repo = null) |
42
|
|
|
{ |
43
|
3 |
|
parent::__construct($repo); |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Command to clone a repository |
48
|
|
|
* |
49
|
|
|
* @param string $url repository url |
50
|
|
|
* @param string $to where to clone the repo |
51
|
|
|
* @param string|null $repoReference Repo reference to clone. Required if performing a shallow clone. |
52
|
|
|
* @param int|null $depth Depth of commits to clone |
53
|
|
|
* @param bool $recursive Whether to recursively clone submodules. |
54
|
|
|
* |
55
|
|
|
* @throws \RuntimeException |
56
|
|
|
* @return string command |
57
|
|
|
*/ |
58
|
3 |
|
public function cloneUrl( |
59
|
|
|
string $url, |
60
|
|
|
string $to = null, |
61
|
|
|
string $repoReference = null, |
62
|
|
|
int $depth = null, |
63
|
|
|
bool $recursive = false |
64
|
|
|
): string { |
65
|
|
|
// get binary version before reset |
66
|
3 |
|
$version = $this->getBinaryVersion(); |
67
|
|
|
|
68
|
3 |
|
$this->clearAll(); |
69
|
3 |
|
$this->addCommandName(static::GIT_CLONE_COMMAND); |
70
|
3 |
|
$this->addCommandSubject($url); |
71
|
3 |
|
if (null !== $to) { |
72
|
3 |
|
$this->addCommandSubject2($to); |
73
|
|
|
} |
74
|
|
|
|
75
|
3 |
|
if (null !== $repoReference) { |
76
|
|
|
// git documentation says the --branch was added in 2.0.0, but it exists undocumented at least back to 1.8.3.1 |
77
|
2 |
|
if (version_compare($version, '1.8.3.1', '<')) { |
78
|
|
|
throw new \RuntimeException( |
79
|
|
|
'Please upgrade to git v1.8.3.1 or newer to support cloning a specific branch. You have ' . $version . '.' |
80
|
|
|
); |
81
|
|
|
} |
82
|
2 |
|
$this->addCommandArgument('--branch=' . $repoReference); |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
if (null !== $depth) { |
86
|
2 |
|
$this->addCommandArgument('--depth=' . $depth); |
87
|
|
|
// shallow-submodules is a nice to have feature. Just ignoring if git version not high enough |
88
|
|
|
// It would be nice if this had a logger injected for us to log notices |
89
|
2 |
|
if (version_compare($version, '2.9.0', '>=') && $recursive && 1 == $depth) { |
90
|
1 |
|
$this->addCommandArgument('--shallow-submodules'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
if ($recursive) { |
95
|
1 |
|
$this->addCommandArgument('--recursive'); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
return $this->getCommand(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|