This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.php
201 lines (166 loc) · 5.21 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
<?php
namespace diversen;
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 getRepoNameFromRepoUrl($repo) {
$url = parse_url($repo);
$url['path'] = rtrim($url['path'], '/');
$parts = explode('/', $url['path']);
$name = array_pop($parts);
if (strstr($name, '.git')) {
$name = substr($name, 0, -4);
}
return $name;
}
/**
* Get a repo name from repo url
* e.g. https://github.com/diversen/php-git-to-book.git should
* return php-git-to-book
* @param string $url
* @return string $name
*/
/*
public static function getRepoName($url) {
return self::getRepoNameFromRepoUrl($url);
$ary = parse_url($url);
$parts = explode('/', $ary['path']);
$last = array_pop($parts);
return str_replace('.git', '', $last);
}*/
/**
* Get all tags as a string from currenct path
* @return string $tags tags as a string
*/
public static function getTagsAsString() {
$command = "git tag -l";
$ret = exec($command, $output);
return implode(PHP_EOL, $output);
}
/**
* Get all tags for current path as an array
* @return array $tags
*/
public static function getTagAsArray () {
$tags = self::getTagsAsString();
$ary = explode("\n", $tags);
return array_filter($ary);
}
/**
* Get latest tag from current path
* @return string $tag
*/
public static function getTagLatest () {
$ary = self::getTagAsArray();
return array_pop($ary);
}
/**
* 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;
}
/**
* Check if main repo is master
* @return boolean true if master else false
*/
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;
}
}