Commit | Line | Data |
---|---|---|
9e72965e S |
1 | <?php |
2 | /* git.class.php | |
3 | * Copyright (C) 2013 Jan Altensen (Stricted) | |
4 | * http://stricted.de/ | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | class git { | |
4c51208b S |
20 | protected $repo; |
21 | ||
22 | public function __construct ($repo) { | |
23 | $this->repo = $repo; | |
24 | } | |
25 | ||
26 | /* | |
9e72965e S |
27 | * parse all commits from repo |
28 | * | |
29 | * @param string $repo | |
30 | * @param int $count | |
31 | * @param string $branch | |
32 | * @return array | |
33 | */ | |
4c51208b | 34 | public function get_commits ($count = -1, $branch = "master") { |
9e72965e S |
35 | $data = array(); |
36 | if(!$data) { | |
4c51208b | 37 | $cmd = "git --git-dir=".$this->repo." rev-list --header --max-count=".$count." ".$branch." "; |
9e72965e S |
38 | $rev_list = shell_exec($cmd); |
39 | foreach(explode("\000", $rev_list) as $rev) { | |
40 | $commit = array(); | |
41 | $rev_lines = explode("\n", str_replace("\r", "", $rev)); | |
42 | $commit['id'] = $rev_lines[0]; | |
43 | foreach($rev_lines as $rev_line) { | |
44 | if(substr($rev_line, 0, 4) == " ") { | |
45 | if(isset($commit['text'])) { | |
46 | $commit['text'] .= "\n".substr($rev_line, 4); | |
47 | } else | |
48 | $commit['text'] = substr($rev_line, 4); | |
49 | } else { | |
9c7f88d2 | 50 | $opt = explode(" ", $rev_line, 2); |
9e72965e S |
51 | if($opt[0] == "tree") { |
52 | $commit['tree'] = $opt[1]; | |
53 | } else if($opt[0] == "parent") { | |
54 | $commit['parent'][] = $opt[1]; | |
55 | } else if($opt[0] == "author") { | |
9c7f88d2 S |
56 | preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); |
57 | $commit['author'] = $matches[1]; | |
58 | $commit['author_mail'] = $matches[2]; | |
59 | $commit['author_time'] = $matches[3]; | |
60 | $commit['author_timezone'] = $matches[4]; | |
9e72965e | 61 | } else if($opt[0] == "committer") { |
9c7f88d2 S |
62 | preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); |
63 | $commit['committer'] = $matches[1]; | |
64 | $commit['committer_mail'] = $matches[2]; | |
65 | $commit['committer_time'] = $matches[3]; | |
66 | $commit['committer_timezone'] = $matches[4]; | |
9e72965e S |
67 | } |
68 | } | |
69 | } | |
70 | $data[] = $commit; | |
71 | } | |
72 | } | |
73 | return $data; | |
74 | } | |
75 | ||
4c51208b | 76 | /* |
9e72965e S |
77 | * get latest commit hash |
78 | * | |
79 | * @param string $repo | |
80 | * @param string $branch | |
81 | * @raturn string | |
82 | */ | |
4c51208b S |
83 | public function get_hash ($branch = "master") { |
84 | $hash = shell_exec('git --git-dir='.$this->repo.' rev-list -n 1 --pretty="format:%h" --header '.$branch.' | grep "^[0-9a-f]*$"'); | |
9e72965e S |
85 | $hash = str_replace("\r","",str_replace("\n","",$hash)); |
86 | return $hash; | |
87 | } | |
88 | ||
4c51208b | 89 | /* |
9e72965e S |
90 | * get commit count |
91 | * | |
92 | * @param string $repo | |
93 | * @param string $branch | |
94 | * @raturn string | |
95 | */ | |
4c51208b S |
96 | public function get_count ($branch = "master") { |
97 | $count = shell_exec('git --git-dir='.$this->repo.' rev-list --oneline --first-parent --header '.$branch.' | wc -l | sed "s/[ \t]//g"'); | |
9e72965e S |
98 | $count = str_replace("\r","",str_replace("\n","",$count)); |
99 | return $count; | |
100 | } | |
101 | ||
4c51208b | 102 | /* |
9e72965e S |
103 | * get commit version |
104 | * | |
105 | * @param string $repo | |
106 | * @param string $branch | |
107 | * @raturn string | |
108 | */ | |
4c51208b S |
109 | public function get_version ($branch = "master") { |
110 | return "git-".$this->get_count($this->repo, $branch)."-".$this->get_hash($repo, $branch); | |
9e72965e S |
111 | } |
112 | ||
4c51208b | 113 | /* |
9e72965e S |
114 | * get commit age |
115 | * | |
9c7f88d2 | 116 | * @param string $last_change |
9e72965e S |
117 | * @raturn string |
118 | */ | |
9c7f88d2 S |
119 | public function get_age ($last_change) { |
120 | $now = time(); | |
121 | $age = ($last_change > 0 ? ($now - $last_change) : 0); | |
122 | ||
9e72965e S |
123 | if ($age > 60*60*24*365*2) { |
124 | $age_str = floor($age/60/60/24/365); | |
125 | $age_str .= " years ago"; | |
126 | } else if ($age > 60*60*24*(365/12)*2) { | |
127 | $age_str = floor($age/60/60/24/(365/12)); | |
128 | $age_str .= " months ago"; | |
129 | } else if ($age > 60*60*24*7*2) { | |
130 | $age_str = floor($age/60/60/24/7); | |
131 | $age_str .= " weeks ago"; | |
132 | } else if ($age > 60*60*24*2) { | |
133 | $age_str = floor($age/60/60/24); | |
134 | $age_str .= " days ago"; | |
135 | } else if ($age > 60*60*2) { | |
136 | $age_str = floor($age/60/60); | |
137 | $age_str .= " hours ago"; | |
138 | } else if ($age > 60*2) { | |
139 | $age_str = floor($age/60); | |
140 | $age_str .= " min ago"; | |
141 | } else if ($age > 2) { | |
142 | $age_str = $age; | |
143 | $age_str .= " sec ago"; | |
9c7f88d2 S |
144 | } else if ($age >= 0) { |
145 | $age_str = "right now"; | |
9e72965e S |
146 | } else { |
147 | $age_str = "right now"; | |
148 | } | |
149 | return $age_str; | |
150 | } | |
151 | ||
9c7f88d2 S |
152 | /* |
153 | * get css class from age | |
154 | * | |
155 | * @param string $last_change | |
156 | * @return string | |
157 | */ | |
158 | public function get_age_class ($last_change) { | |
159 | $now = time(); | |
160 | $age = ($last_change > 0 ? ($now - $last_change) : 0); | |
161 | ||
162 | if($age == 0) { | |
163 | $age_class = "noage"; | |
164 | } else if ($age < 60*60*2) { | |
165 | $age_class = "age0"; | |
166 | } else if ($age < 60*60*24*2) { | |
167 | $age_class = "age1"; | |
168 | } else { | |
169 | $age_class = "age2"; | |
170 | } | |
171 | return $age_class; | |
172 | } | |
173 | ||
4c51208b S |
174 | /* |
175 | * pull current repo | |
9e72965e | 176 | */ |
4c51208b S |
177 | public function pull () { |
178 | shell_exec("git --git-dir=".$this->repo." pull"); | |
9e72965e S |
179 | } |
180 | } | |
181 | ?> |