-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.php
216 lines (181 loc) · 5.59 KB
/
git.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
namespace diversen;
use diversen\cli\common;
use diversen\conf;
class git {
/**
* Get a module name from a git repo url
* Works with e.g.
* git@github.com:diversen/debug.git
* git@github.com:diversen/debug
* https://github.com/diversen/debug.git
* https://github.com/diversen/debug
* git://github.com/diversen/debug.git
* git://github.com/diversen/debug
* @param string $repo url
* @return string $module_name
*/
public static function getModulenameFromRepo($repo) {
$url = parse_url($repo);
$parts = explode('/', $url['path']);
$module_name = array_pop($parts);
if (strstr($module_name, '.git')) {
$module_name = substr($module_name, 0, -4);
}
return $module_name;
}
/**
* get tags for a system git repo
* @return string $tags tags as a string
*/
public static function getTagsInstall() {
$command = "git tag -l";
$ret = exec($command, $output);
return common::parseShellArray($output);
}
/**
* get system tags as array
* @return array $tags
*/
public static function getTagsInstallAsArray () {
$tags = self::getTagsInstall();
$ary = explode("\n", $tags);
return array_filter($ary);
}
/**
* get latest installed tag. This is the latest tag
* @return array $tags
*/
public static function getTagsInstallLatest () {
$ary = self::getTagsInstallAsArray();
return array_pop($ary);
}
/**
* get tags for a module or template
* @param string $module
* @param string $type 'module' or 'template'
* @return array|false a array or false
*/
public static function getTagsModule($module, $type = 'module') {
if ($type == 'module') {
$path = conf::pathModules() . "/$module";
}
if ($type == 'template') {
$path = conf::pathHtdocs() . "/templates/$module";
}
$command = "cd $path && git tag -l";
exec($command, $ary, $ret);
// ok
if ($ret == 0) {
$str = shell_exec($command);
$ary = explode("\n", $str);
$tags = array();
foreach ($ary as $line) {
trim($line);
if (empty($line)){
continue;
}
$tags[] = $line;
}
} else {
return false;
}
return $tags;
}
/**
* Get remote tags
*
* See: https://github.com/troelskn/pearhub
*
* @param string $url a git url
* @param mixed $clear set this and tags will not be cached in static var
* @return array $ary array of remote tags
*/
public static function getTagsRemote($url = null) {
$tags = array();
$output = array();
$ret = 0;
$command = "git ls-remote --tags $url";
exec($command . ' 2>&1', $output, $ret);
foreach ($output as $line) {
trim($line);
if (preg_match('~^[0-9a-f]{40}\s+refs/tags/(([a-zA-Z_-]+)?([0-9]+)(\.([0-9]+))?(\.([0-9]+))?([A-Za-z]+[0-9A-Za-z-]*)?)$~', $line, $reg)) {
$tags[] = $reg[1];
}
}
return $tags;
}
/**
* Get latest remote tag
*
* See: https://github.com/troelskn/pearhub
*
* @param string $repo a git url url
* @param boolean $clear and tags will not be cached in static var
* @return array $tags array of remote tags
*/
public static function getTagsRemoteLatest($repo) {
$tags = self::getTagsRemote($repo);
if (count($tags) > 0) {
sort($tags);
return $tags[count($tags) - 1];
}
return null;
}
public static function isMaster () {
$branch = shell_exec('git branch');
if ('* master' == trim($branch)){
return true;
}
return false;
}
/**
* get a SSH clone URL from a HTTPS clone URL
* @param string $url
* @return string $url
*/
public static function getSshFromHttps($url) {
$ary = parse_url(trim($url));
$num = count($ary);
if ($num == 1) {
return $ary['path'];
}
// E.g. git://github.com/diversen/vote.git
if (isset($ary['scheme']) && $ary['scheme'] == 'git') {
return "git@$ary[host]:" . ltrim($ary['path'], '/');
}
return "git@$ary[host]:" . ltrim($ary['path'], '/');
}
/**
* get a SSH clone URL from a HTTPS clone URL
* @param string $url
* @return string $url
*/
public static function getHttpsFromSsh($url) {
$ary = parse_url(trim($url));
// Is it already https
if (isset($ary['scheme']) && $ary['scheme'] == 'https') {
return $url;
}
// E.g. git://github.com/diversen/vote.git
if (isset($ary['scheme']) && $ary['scheme'] == 'git') {
return 'https://' . $ary['host'] . $ary['path'];
}
$num = count($ary);
if ($num == 1) {
return self::parsePrivateUrl($url);
}
return "$ary[scheme]@$ary[host]:$ary[path]";
}
/**
* return a SSH path from a HTTPS URL
* @param string $url
* @return string $path
*/
public static function parsePrivateUrl ($url) {
$ary = explode('@', $url);
$ary = explode(':', $ary[1]);
$url = 'https://' . $ary[0] . "/$ary[1]";
return $url;
}
}