-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGitInfo.php
116 lines (93 loc) · 3.08 KB
/
GitInfo.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
<?php
/**
* PixelCog Web Services
*
* @copyright Copyright 2013, PixelCog Inc. (http://pixelcog.com) All Rights Reserved
*/
namespace app\extensions;
class GitInfo {
protected $_path = null;
protected $_statuses = array(
'-' => 'Not Initialized',
'+' => 'Checkout Mismatch',
'U' => 'Merge Conflicts',
);
public function __construct($path = __DIR__) {
$this->_path = $path;
}
public function getCommit($path = null) {
return $this->_execute('git rev-parse HEAD', $path, '');
}
public function getInfo($path = null) {
$data = array(
'fullpath' => $this->_execute('pwd', $path, ''),
'commit' => $this->getCommit($path),
'origin' => $this->_execute('git config --get remote.origin.url', $path, ''),
'branch' => $this->getBranch($path),
'comment' => $this->getCommitMessage($path)
);
$data += $this->parseOriginInfo($data['origin'], $data['commit']);
if ($modules = $this->getSubModules($path)) {
$data['modules'] = $modules;
}
return $data;
}
public function getBranch($path = null) {
$lines = $this->_execute('git branch', $path);
foreach($lines as $line) {
if ( strpos( $line, '*' ) === 0 ) {
return ltrim( $line, '* ' );
}
}
return null;
}
public function getCommitMessage($path = null) {
$msg = $this->_execute('git log -1 --oneline', $path, '');
if (preg_match('#^([0-9a-f]+?) (.*?)$#i', $msg, $match)) {
$msg = $match[2];
}
return $msg;
}
public function getSubModules($path = null) {
$modules = $this->_execute('git submodule status', $path);
foreach($modules as &$module) {
if (preg_match('#([\-\+U ])([0-9a-f]{40})\s([^\(]+?)\s\(([^\)]+?)\)#', $module, $match)) {
$module = $this->getInfo($match[3]) + array(
'path' => $match[3],
'commit' => $match[2],
'describe' => $match[4],
'status' => isset($this->_statuses[$match[1]]) ? $this->_statuses[$match[1]] : 'Normal'
);
continue;
}
$module = null;
}
$modules = array_filter($modules);
return $modules;
}
public function parseOriginInfo($origin, $commit) {
$remote = array();
if (preg_match('#^(?:(?:git|https?)://)?(?:[^@]*?@)?(bitbucket.org|github.com)[:/]([^/]*?)/([^/]*?)(?:.git)?$#i', $origin, $match)) {
$remote['host'] = $match[1];
$remote['repo'] = $match[2].'/'.$match[3];
if ($remote['host'] == 'github.com') {
$remote['link_commit'] = 'https://' . $remote['host'] . '/' . $remote['repo'] . '/commit/' . substr($commit,0,12);
$remote['link_source'] = 'https://' . $remote['host'] . '/' . $remote['repo'] . '/tree/' . substr($commit,0,12);
}
if ($remote['host'] == 'bitbucket.org') {
$remote['link_commit'] = 'https://' . $remote['host'] . '/' . $remote['repo'] . '/changeset/' . substr($commit,0,12);
$remote['link_source'] = 'https://' . $remote['host'] . '/' . $remote['repo'] . '/src/' . substr($commit,0,12);
}
}
return $remote;
}
protected function _execute($cmd, $path = null, $join = null) {
chdir($this->_path);
if ($path) {
chdir($path);
}
exec($cmd, $output);
return $join !== null ? join($join, $output) : $output;
}
}
?>