. */ class GitCommand { private static $git = null; private static $counter = 0; private static function command_header() { if(!self::$git) { //find git executable if(GitConfig::GIT_EXEC) self::$git = GitConfig::GIT_EXEC; else self::$git = str_replace(array("\n", "\r"), array("", ""), `which git`); if(!self::$git) { trigger_error("Can not find git executable.", E_USER_ERROR); self::$git = "git"; } } $command = self::$git; return $command; } private static function git_execute($params, $git_path = null) { $result = array(); if($git_path) { $args = array("--git-dir=".$git_path); $args = array_merge($args, $params); } else $args = $params; $command = self::command_header(); foreach($args as $arg) { $command .= ' '.escapeshellarg($arg); } exec($command, $result); self::$counter++; return implode("\n", $result); } public static function core_version() { $args = array("--version"); $version = self::git_execute($args); preg_match("/[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/i", $version, $result); return $result[0]; } public static function last_activity($git_path, $ref = null) { $args = array("for-each-ref", "--format=%(committer)", "--sort=-committerdate", "--count=1"); if($ref) $args[] = $ref; $age = self::git_execute($args, $git_path); preg_match("/[0-9]{9,}/i", $age, $result); return $result[0]; } private static function parse_commit($commit_data) { $commit = array(); $rev_lines = explode("\n", str_replace("\r", "", $commit_data)); $commit['id'] = $rev_lines[0]; $commit['parent'] = array(); if(!preg_match("/^[a-f0-9]{40}$/i", $commit['id'])) return null; foreach($rev_lines as $rev_line) { if(substr($rev_line, 0, 4) == " ") { if(array_key_exists('text', $commit)) $commit['text'] .= "\n"; else $commit['text'] = ''; $commit['text'] .= substr($rev_line, 4); } else { $opt = explode(" ", $rev_line, 2); if($opt[0] == "tree") $commit['tree'] = $opt[1]; else if($opt[0] == "parent") $commit['parent'][] = $opt[1]; else if($opt[0] == "author") { preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); $commit['author'] = $matches[1]; $commit['author_mail'] = $matches[2]; $commit['author_time'] = $matches[3]; $commit['author_timezone'] = $matches[4]; } else if($opt[0] == "committer") { preg_match('/(.*) <([^>]*)> ([0-9]*) ([+\-0-9]{5})/i', $opt[1], $matches); $commit['committer'] = $matches[1]; $commit['committer_mail'] = $matches[2]; $commit['committer_time'] = $matches[3]; $commit['committer_timezone'] = $matches[4]; } } } return $commit; } public static function get_commits($git_path, $head, $maxcount, $skip, $file = null) { $args = array("rev-list", "--header", "--max-count=".$maxcount, "--skip=".$skip, ($head ? $head : "--all"), "--"); if($file) $args[] = $file; $commit_list = self::git_execute($args, $git_path); $commits = array(); foreach(explode("\000", $commit_list) as $commit) { if($commit) $commits[] = self::parse_commit($commit); } return $commits; } public static function get_commit($git_path, $commit_id) { $args = array("rev-list", "--header", "--max-count=1", $commit_id, "--"); $commit_data = self::git_execute($args, $git_path); $commit = self::parse_commit($commit_data); return $commit; } public static function get_hash($git_path, $ref) { $args = array("rev-parse", "--verify", "-q", $ref); $result = self::git_execute($args, $git_path); if(preg_match("#([a-f0-9]{40})#i", $result, $match)) return $match[1]; return null; } }