-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathAdmin.php
189 lines (164 loc) · 6.36 KB
/
Admin.php
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* This file is part of Gitonomy.
*
* (c) Alexandre Salomé <[email protected]>
* (c) Julien DIDIER <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Gitonomy\Git;
use Gitonomy\Git\Exception\RuntimeException;
use Symfony\Component\Process\Process;
/**
* Administration class for Git repositories.
*
* @author Alexandre Salomé <[email protected]>
*/
class Admin
{
/**
* Initializes a repository and returns the instance.
*
* @param string $path path to the repository
* @param bool $bare indicate to create a bare repository
* @param array $options options for Repository creation
*
* @throws RuntimeException Directory exists or not writable (only if debug=true)
*
* @return Repository
*/
public static function init($path, $bare = true, array $options = [])
{
$process = static::getProcess('init', array_merge(['-q'], $bare ? ['--bare'] : [], [$path]), $options);
$process->run();
if (!$process->isSuccessFul()) {
throw new RuntimeException(sprintf("Error on repository initialization, command wasn't successful (%s). Error output:\n%s", $process->getCommandLine(), $process->getErrorOutput()));
}
return new Repository($path, $options);
}
/**
* Checks the validity of a git repository url without cloning it.
*
* This will use the `ls-remote` command of git against the given url.
* Usually, this command returns 0 when successful, and 128 when the
* repository is not found.
*
* @param string $url url of repository to check
* @param array $options options for Repository creation
*
* @return bool true if url is valid
*/
public static function isValidRepository($url, array $options = [])
{
$process = static::getProcess('ls-remote', [$url], $options);
$process->run();
return $process->isSuccessFul();
}
/**
* Checks the validity of a git repository url without cloning it and
* check if a certain branch exists in that repository.
*
* This will use the `ls-remote` command of git against the given url.
* Usually, this command returns 0 when successful, and 128 when the
* repository is not found.
*
* @param string $url url of repository to check
* @param string $branchName name of branch to check
* @param array $options options for Repository creation
*
* @return bool true if url is valid and branch exists
*/
public static function isValidRepositoryAndBranch($url, $branchName, array $options = [])
{
$process = static::getProcess('ls-remote', ['--heads', $url, $branchName], $options);
$process->run();
$processOutput = $process->getOutput();
return $process->isSuccessFul() && strpos($processOutput, $branchName) !== false;
}
/**
* Clone a repository to a local path.
*
* @param string $path indicates where to clone repository
* @param string $url url of repository to clone
* @param bool $bare indicates if repository should be bare or have a working copy
* @param array $options options for Repository creation
* @param array $args arguments to be added to the command-line
*
* @return Repository
*/
public static function cloneTo($path, $url, $bare = true, array $options = [], array $args = [])
{
$args = array_merge($args,$bare ? ['--bare'] : []);
return static::cloneRepository($path, $url, $args, $options);
}
/**
* Clone a repository branch to a local path.
*
* @param string $path indicates where to clone repository
* @param string $url url of repository to clone
* @param string $branch branch to clone
* @param bool $bare indicates if repository should be bare or have a working copy
* @param array $options options for Repository creation
*
* @return Repository
*/
public static function cloneBranchTo($path, $url, $branch, $bare = true, $options = [])
{
$args = ['--branch', $branch];
if ($bare) {
$args[] = '--bare';
}
return static::cloneRepository($path, $url, $args, $options);
}
/**
* Mirrors a repository (fetch all revisions, not only branches).
*
* @param string $path indicates where to clone repository
* @param string $url url of repository to clone
* @param array $options options for Repository creation
*
* @return Repository
*/
public static function mirrorTo($path, $url, array $options = [])
{
return static::cloneRepository($path, $url, ['--mirror'], $options);
}
/**
* Internal method to launch effective ``git clone`` command.
*
* @param string $path indicates where to clone repository
* @param string $url url of repository to clone
* @param array $args arguments to be added to the command-line
* @param array $options options for Repository creation
*
* @return Repository
*/
public static function cloneRepository($path, $url, array $args = [], array $options = [])
{
$process = static::getProcess('clone', array_merge(['-q'], $args, [$url, $path]), $options);
$process->run();
if (!$process->isSuccessFul()) {
throw new RuntimeException(sprintf('Error while initializing repository: %s', $process->getErrorOutput()));
}
return new Repository($path, $options);
}
/**
* This internal method is used to create a process object.
*/
private static function getProcess($command, array $args = [], array $options = [])
{
$is_windows = defined('PHP_WINDOWS_VERSION_BUILD');
$options = array_merge([
'environment_variables' => $is_windows ? ['PATH' => getenv('PATH')] : [],
'command' => 'git',
'process_timeout' => 3600,
], $options);
$process = new Process(array_merge([$options['command'], $command], $args));
$process->setEnv($options['environment_variables']);
$process->setTimeout($options['process_timeout']);
$process->setIdleTimeout($options['process_timeout']);
return $process;
}
}