2 /* GitCommand.class.php - phpgitweb
3 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 private static $git = null;
21 private static $counter = 0;
23 private static function command_header() {
26 if(GitConfig::GIT_EXEC)
27 self::$git = GitConfig::GIT_EXEC;
29 self::$git = str_replace(array("\n", "\r"), array("", ""), `which git`);
31 trigger_error("Can not find git executable.", E_USER_ERROR);
35 $command = self::$git;
40 private static function git_execute($params, $git_path = null) {
44 $args = array("--git-dir=".$git_path);
45 $args = array_merge($args, $params);
49 $command = self::command_header();
50 foreach($args as $arg) {
51 $command .= ' '.escapeshellarg($arg);
54 exec($command, $result);
56 return implode("\n", $result);
59 public static function core_version() {
60 $args = array("--version");
61 $version = self::git_execute($args);
62 preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/i", $version, $result);
66 public static function last_activity($git_path, $ref = null) {
67 $args = array("for-each-ref", "--format=%(committer)", "--sort=-committerdate", "--count=1");
70 $age = self::git_execute($args, $git_path);
71 preg_match("/[0-9]{9,}/i", $age, $result);
75 private static function parse_commit($commit_data) {
77 $rev_lines = explode("\n", str_replace("\r", "", $commit_data));
78 $commit['id'] = $rev_lines[0];
79 $commit['parent'] = array();
80 if(!preg_match("/^[a-f0-9]{40}$/i", $commit['id']))
82 foreach($rev_lines as $rev_line) {
83 if(substr($rev_line, 0, 4) == " ") {
84 if(array_key_exists('text', $commit))
85 $commit['text'] .= "\n";
88 $commit['text'] .= substr($rev_line, 4);
90 $opt = explode(" ", $rev_line, 2);
92 $commit['tree'] = $opt[1];
93 else if($opt[0] == "parent")
94 $commit['parent'][] = $opt[1];
95 else if($opt[0] == "author") {
96 preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches);
97 $commit['author'] = $matches[1];
98 $commit['author_mail'] = $matches[2];
99 $commit['author_time'] = $matches[3];
100 $commit['author_timezone'] = $matches[4];
101 } else if($opt[0] == "committer") {
102 preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches);
103 $commit['committer'] = $matches[1];
104 $commit['committer_mail'] = $matches[2];
105 $commit['committer_time'] = $matches[3];
106 $commit['committer_timezone'] = $matches[4];
113 public static function get_commits($git_path, $head, $maxcount, $skip, $file = null) {
114 $args = array("rev-list", "--header", "--max-count=".$maxcount, "--skip=".$skip, ($head ? $head : "--all"), "--");
117 $commit_list = self::git_execute($args, $git_path);
119 foreach(explode("\000", $commit_list) as $commit) {
121 $commits[] = self::parse_commit($commit);
126 public static function get_commit($git_path, $commit_id) {
127 $args = array("rev-list", "--header", "--max-count=1", $commit_id, "--");
128 $commit_data = self::git_execute($args, $git_path);
129 $commit = self::parse_commit($commit_data);
133 public static function get_hash($git_path, $ref) {
134 $args = array("rev-parse", "--verify", "-q", $ref);
135 $result = self::git_execute($args, $git_path);
136 if(preg_match("#([a-f0-9]{40})#i", $result, $match))