-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathGit.php
More file actions
162 lines (131 loc) · 3.46 KB
/
Git.php
File metadata and controls
162 lines (131 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace CzProject\GitPhp;
class Git
{
/** @var IRunner */
protected $runner;
public function __construct(IRunner $runner = NULL)
{
$this->runner = $runner !== NULL ? $runner : new Runners\CliRunner;
}
/**
* Add a configuration parameter to the command.
* @param string $name
* @param mixed $value
*/
public function addConfig($name, $value)
{
$this->runner->addConfig($name, $value);
}
/**
* @param string $directory
* @return GitRepository
*/
public function open($directory)
{
return new GitRepository($directory, $this->runner);
}
/**
* Init repo in directory
* @param string $directory
* @param array<mixed>|NULL $params
* @return GitRepository
* @throws GitException
*/
public function init($directory, array $params = NULL)
{
if (is_dir("$directory/.git")) {
throw new GitException("Repo already exists in $directory.");
}
if (!is_dir($directory) && !@mkdir($directory, 0777, TRUE)) { // intentionally @; not atomic; from Nette FW
throw new GitException("Unable to create directory '$directory'.");
}
try {
$this->run($directory, [
'init',
$params,
'--end-of-options',
$directory
]);
} catch (GitException $e) {
throw new GitException("Git init failed (directory $directory).", $e->getCode(), $e);
}
return $this->open($directory);
}
/**
* Clones GIT repository from $url into $directory
* @param string $url
* @param string|NULL $directory
* @param array<mixed>|NULL $params
* @return GitRepository
* @throws GitException
*/
public function cloneRepository($url, $directory = NULL, array $params = NULL)
{
if ($directory !== NULL && is_dir("$directory/.git")) {
throw new GitException("Repo already exists in $directory.");
}
$cwd = $this->runner->getCwd();
if ($directory === NULL) {
$directory = Helpers::extractRepositoryNameFromUrl($url);
$directory = "$cwd/$directory";
} elseif(!Helpers::isAbsolute($directory)) {
$directory = "$cwd/$directory";
}
if ($params === NULL) {
$params = '-q';
}
try {
$this->run($cwd, [
'clone',
$params,
'--end-of-options',
$url,
$directory
]);
} catch (GitException $e) {
$stderr = '';
$result = $e->getRunnerResult();
if ($result !== NULL && $result->hasErrorOutput()) {
$stderr = implode(PHP_EOL, $result->getErrorOutput());
}
throw new GitException("Git clone failed (directory $directory)." . ($stderr !== '' ? ("\n$stderr") : ''));
}
return $this->open($directory);
}
/**
* @param string $url
* @param array<string>|NULL $refs
* @return bool
*/
public function isRemoteUrlReadable($url, array $refs = NULL)
{
$result = $this->runner->run($this->runner->getCwd(), [
'ls-remote',
'--heads',
'--quiet',
'--exit-code',
'--end-of-options',
$url,
$refs,
], [
'GIT_TERMINAL_PROMPT' => 0,
]);
return $result->isOk();
}
/**
* @param string $cwd
* @param array<mixed> $args
* @param array<string, scalar> $env
* @return RunnerResult
* @throws GitException
*/
private function run($cwd, array $args, array $env = NULL)
{
$result = $this->runner->run($cwd, $args, $env);
if (!$result->isOk()) {
throw new GitException("Command '{$result->getCommand()}' failed (exit-code {$result->getExitCode()}).", $result->getExitCode(), NULL, $result);
}
return $result;
}
}