Commit | Line | Data |
---|---|---|
9e72965e | 1 | <?php |
a4685b95 S |
2 | /** |
3 | * @author Jan Altensen (Stricted) | |
4 | * @copyright 2013-2014 Jan Altensen (Stricted) | |
5 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> | |
9e72965e S |
6 | */ |
7 | class git { | |
4c51208b S |
8 | protected $repo; |
9 | ||
10 | public function __construct ($repo) { | |
11 | $this->repo = $repo; | |
12 | } | |
13 | ||
14 | /* | |
9e72965e S |
15 | * parse all commits from repo |
16 | * | |
17 | * @param string $repo | |
18 | * @param int $count | |
19 | * @param string $branch | |
20 | * @return array | |
21 | */ | |
4c51208b | 22 | public function get_commits ($count = -1, $branch = "master") { |
9e72965e | 23 | $data = array(); |
5f6347bf | 24 | if (!$data) { |
4c51208b | 25 | $cmd = "git --git-dir=".$this->repo." rev-list --header --max-count=".$count." ".$branch." "; |
9e72965e S |
26 | $rev_list = shell_exec($cmd); |
27 | foreach(explode("\000", $rev_list) as $rev) { | |
28 | $commit = array(); | |
29 | $rev_lines = explode("\n", str_replace("\r", "", $rev)); | |
30 | $commit['id'] = $rev_lines[0]; | |
31 | foreach($rev_lines as $rev_line) { | |
5f6347bf S |
32 | if (substr($rev_line, 0, 4) == " ") { |
33 | if (isset($commit['text'])) { | |
9e72965e | 34 | $commit['text'] .= "\n".substr($rev_line, 4); |
5f6347bf S |
35 | } |
36 | else { | |
9e72965e | 37 | $commit['text'] = substr($rev_line, 4); |
5f6347bf S |
38 | } |
39 | } | |
40 | else { | |
9c7f88d2 | 41 | $opt = explode(" ", $rev_line, 2); |
5f6347bf | 42 | if ($opt[0] == "tree") { |
9e72965e | 43 | $commit['tree'] = $opt[1]; |
5f6347bf S |
44 | } |
45 | else if ($opt[0] == "parent") { | |
9e72965e | 46 | $commit['parent'][] = $opt[1]; |
5f6347bf S |
47 | } |
48 | else if ($opt[0] == "author") { | |
9c7f88d2 S |
49 | preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); |
50 | $commit['author'] = $matches[1]; | |
51 | $commit['author_mail'] = $matches[2]; | |
52 | $commit['author_time'] = $matches[3]; | |
53 | $commit['author_timezone'] = $matches[4]; | |
5f6347bf S |
54 | } |
55 | else if ($opt[0] == "committer") { | |
9c7f88d2 S |
56 | preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); |
57 | $commit['committer'] = $matches[1]; | |
58 | $commit['committer_mail'] = $matches[2]; | |
59 | $commit['committer_time'] = $matches[3]; | |
60 | $commit['committer_timezone'] = $matches[4]; | |
9e72965e S |
61 | } |
62 | } | |
63 | } | |
64 | $data[] = $commit; | |
65 | } | |
66 | } | |
67 | return $data; | |
68 | } | |
69 | ||
4c51208b | 70 | /* |
9e72965e S |
71 | * get latest commit hash |
72 | * | |
73 | * @param string $repo | |
74 | * @param string $branch | |
75 | * @raturn string | |
76 | */ | |
4c51208b S |
77 | public function get_hash ($branch = "master") { |
78 | $hash = shell_exec('git --git-dir='.$this->repo.' rev-list -n 1 --pretty="format:%h" --header '.$branch.' | grep "^[0-9a-f]*$"'); | |
9e72965e S |
79 | $hash = str_replace("\r","",str_replace("\n","",$hash)); |
80 | return $hash; | |
81 | } | |
82 | ||
4c51208b | 83 | /* |
9e72965e S |
84 | * get commit count |
85 | * | |
86 | * @param string $repo | |
87 | * @param string $branch | |
88 | * @raturn string | |
89 | */ | |
4c51208b S |
90 | public function get_count ($branch = "master") { |
91 | $count = shell_exec('git --git-dir='.$this->repo.' rev-list --oneline --first-parent --header '.$branch.' | wc -l | sed "s/[ \t]//g"'); | |
9e72965e S |
92 | $count = str_replace("\r","",str_replace("\n","",$count)); |
93 | return $count; | |
94 | } | |
95 | ||
4c51208b | 96 | /* |
9e72965e S |
97 | * get commit version |
98 | * | |
99 | * @param string $repo | |
100 | * @param string $branch | |
101 | * @raturn string | |
102 | */ | |
4c51208b S |
103 | public function get_version ($branch = "master") { |
104 | return "git-".$this->get_count($this->repo, $branch)."-".$this->get_hash($repo, $branch); | |
9e72965e S |
105 | } |
106 | ||
4c51208b | 107 | /* |
9e72965e S |
108 | * get commit age |
109 | * | |
9c7f88d2 | 110 | * @param string $last_change |
9e72965e S |
111 | * @raturn string |
112 | */ | |
9c7f88d2 S |
113 | public function get_age ($last_change) { |
114 | $now = time(); | |
115 | $age = ($last_change > 0 ? ($now - $last_change) : 0); | |
116 | ||
9e72965e S |
117 | if ($age > 60*60*24*365*2) { |
118 | $age_str = floor($age/60/60/24/365); | |
119 | $age_str .= " years ago"; | |
5f6347bf S |
120 | } |
121 | else if ($age > 60*60*24*(365/12)*2) { | |
9e72965e S |
122 | $age_str = floor($age/60/60/24/(365/12)); |
123 | $age_str .= " months ago"; | |
5f6347bf S |
124 | } |
125 | else if ($age > 60*60*24*7*2) { | |
9e72965e S |
126 | $age_str = floor($age/60/60/24/7); |
127 | $age_str .= " weeks ago"; | |
5f6347bf S |
128 | } |
129 | else if ($age > 60*60*24*2) { | |
9e72965e S |
130 | $age_str = floor($age/60/60/24); |
131 | $age_str .= " days ago"; | |
5f6347bf S |
132 | } |
133 | else if ($age > 60*60*2) { | |
9e72965e S |
134 | $age_str = floor($age/60/60); |
135 | $age_str .= " hours ago"; | |
5f6347bf S |
136 | } |
137 | else if ($age > 60*2) { | |
9e72965e S |
138 | $age_str = floor($age/60); |
139 | $age_str .= " min ago"; | |
5f6347bf S |
140 | } |
141 | else if ($age > 2) { | |
9e72965e S |
142 | $age_str = $age; |
143 | $age_str .= " sec ago"; | |
5f6347bf S |
144 | } |
145 | else if ($age >= 0) { | |
9c7f88d2 | 146 | $age_str = "right now"; |
5f6347bf S |
147 | } |
148 | else { | |
9e72965e S |
149 | $age_str = "right now"; |
150 | } | |
151 | return $age_str; | |
152 | } | |
153 | ||
9c7f88d2 S |
154 | /* |
155 | * get css class from age | |
156 | * | |
157 | * @param string $last_change | |
158 | * @return string | |
159 | */ | |
160 | public function get_age_class ($last_change) { | |
161 | $now = time(); | |
162 | $age = ($last_change > 0 ? ($now - $last_change) : 0); | |
163 | ||
5f6347bf | 164 | if ($age == 0) { |
9c7f88d2 | 165 | $age_class = "noage"; |
5f6347bf S |
166 | } |
167 | else if ($age < 60*60*2) { | |
9c7f88d2 | 168 | $age_class = "age0"; |
5f6347bf S |
169 | } |
170 | else if ($age < 60*60*24*2) { | |
9c7f88d2 | 171 | $age_class = "age1"; |
5f6347bf S |
172 | } |
173 | else { | |
9c7f88d2 S |
174 | $age_class = "age2"; |
175 | } | |
176 | return $age_class; | |
177 | } | |
178 | ||
4c51208b S |
179 | /* |
180 | * pull current repo | |
9e72965e | 181 | */ |
4c51208b S |
182 | public function pull () { |
183 | shell_exec("git --git-dir=".$this->repo." pull"); | |
9e72965e | 184 | } |
01281009 S |
185 | |
186 | /* | |
187 | * clone repo | |
188 | */ | |
189 | public function clone () { | |
190 | shell_exec("git clone --bare ".$this->repo); | |
191 | } | |
9e72965e S |
192 | } |
193 | ?> |