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>
11 * Constructs a new instance of git class.
15 public function __construct ($repo) {
20 * parse all commits from repo
22 * @param integer $count
23 * @param string $branch
26 public function get_commits ($count = -1, $branch = "master") {
29 $cmd = "git --git-dir=".$this->repo
." rev-list --header --max-count=".$count." ".$branch." ";
30 $rev_list = shell_exec($cmd);
31 foreach(explode("\000", $rev_list) as $rev) {
33 $rev_lines = explode("\n", str_replace("\r", "", $rev));
34 $commit['id'] = $rev_lines[0];
35 foreach($rev_lines as $rev_line) {
36 if (substr($rev_line, 0, 4) == " ") {
37 if (isset($commit['text'])) {
38 $commit['text'] .= "\n".substr($rev_line, 4);
41 $commit['text'] = substr($rev_line, 4);
45 $opt = explode(" ", $rev_line, 2);
46 if ($opt[0] == "tree") {
47 $commit['tree'] = $opt[1];
49 else if ($opt[0] == "parent") {
50 $commit['parent'][] = $opt[1];
52 else if ($opt[0] == "author") {
53 preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches);
54 $commit['author'] = $matches[1];
55 $commit['author_mail'] = $matches[2];
56 $commit['author_time'] = $matches[3];
57 $commit['author_timezone'] = $matches[4];
59 else if ($opt[0] == "committer") {
60 preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches);
61 $commit['committer'] = $matches[1];
62 $commit['committer_mail'] = $matches[2];
63 $commit['committer_time'] = $matches[3];
64 $commit['committer_timezone'] = $matches[4];
75 * get latest commit hash
77 * @param string $branch
80 public function get_hash ($branch = "master") {
81 $hash = shell_exec('git --git-dir='.$this->repo
.' rev-list -n 1 --pretty="format:%h" --header '.$branch.' | grep "^[0-9a-f]*$"');
82 $hash = str_replace("\r","",str_replace("\n","",$hash));
89 * @param string $branch
92 public function get_count ($branch = "master") {
93 $count = shell_exec('git --git-dir='.$this->repo
.' rev-list --oneline --first-parent --header '.$branch.' | wc -l | sed "s/[ \t]//g"');
94 $count = str_replace("\r","",str_replace("\n","",$count));
101 * @param string $branch
104 public function get_version ($branch = "master") {
105 return "git-".$this->get_count($this->repo
, $branch)."-".$this->get_hash($repo, $branch);
111 * @param string $last_change
114 public function get_age ($last_change) {
116 $age = ($last_change > 0 ?
($now - $last_change) : 0);
118 if ($age > 60*60*24*365*2) {
119 $age_str = floor($age/60/60/24/365);
120 $age_str .= " years ago";
122 else if ($age > 60*60*24*(365/12)*2) {
123 $age_str = floor($age/60/60/24/(365/12));
124 $age_str .= " months ago";
126 else if ($age > 60*60*24*7*2) {
127 $age_str = floor($age/60/60/24/7);
128 $age_str .= " weeks ago";
130 else if ($age > 60*60*24*2) {
131 $age_str = floor($age/60/60/24);
132 $age_str .= " days ago";
134 else if ($age > 60*60*2) {
135 $age_str = floor($age/60/60);
136 $age_str .= " hours ago";
138 else if ($age > 60*2) {
139 $age_str = floor($age/60);
140 $age_str .= " min ago";
144 $age_str .= " sec ago";
146 else if ($age >= 0) {
147 $age_str = "right now";
150 $age_str = "right now";
156 * get css class from age
158 * @param string $last_change
161 public function get_age_class ($last_change) {
163 $age = ($last_change > 0 ?
($now - $last_change) : 0);
166 $age_class = "noage";
168 else if ($age < 60*60*2) {
171 else if ($age < 60*60*24*2) {
183 public function pull () {
184 shell_exec("git --git-dir=".$this->repo
." pull");
190 public function clone () {
191 shell_exec("git clone --bare ".$this->repo
);