continued :)
[phpgitweb.git] / htdocs / lib / GitCommand.class.php
index 4e20281778e1d6c1ab58642f70a0e4beda3d1c4a..609d0ad7d0575f06a1125b631cf1ce81889580ba 100644 (file)
@@ -20,7 +20,7 @@ class GitCommand {
        private static $git = null;
        private static $counter = 0;
        
-       private static function command_header($git_path = null) {
+       private static function command_header() {
                if(!self::$git) {
                        //find git executable
                        if(GitConfig::GIT_EXEC)
@@ -33,36 +33,41 @@ class GitCommand {
                        }
                }
                $command = self::$git;
-               if($git_path)
-                       $command .= " --git-dir=".$git_path;
+               
                return $command;
        }
        
-       private static function execute($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() {
-               $command = self::command_header();
-               if(!$command)
-                       return null;
-               $command .= " --version";
-               $version = self::execute($command);
+               $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) {
-               $command = self::command_header($git_path);
-               if(!$command)
-                       return null;
-               $command .= " for-each-ref --format=%\(committer\) --sort=-committerdate --count=1";
+               $args = array("for-each-ref", "--format=%(committer)", "--sort=-committerdate", "--count=1");
                if($ref)
-                       $command .= " ".$ref;
-               $age = self::execute($command);
+                       $args[] = $ref;
+               $age = self::git_execute($args, $git_path);
                preg_match("/[0-9]{9,}/i", $age, $result);
                return $result[0];
        }
@@ -71,6 +76,9 @@ class GitCommand {
                $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))
@@ -85,15 +93,17 @@ class GitCommand {
                                else if($opt[0] == "parent")
                                        $commit['parent'][] = $opt[1];
                                else if($opt[0] == "author") {
-                                       preg_match('/(.*) <([^>]*)> ([0-9]*) [+-0-9]*/i', $opt[1], $matches);
+                                       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]*/i', $opt[1], $matches);
+                                       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];
                                }
                        }
                }
@@ -101,13 +111,10 @@ class GitCommand {
        }
        
        public static function get_commits($git_path, $head, $maxcount, $skip, $file = null) {
-               $command = self::command_header($git_path);
-               if(!$command)
-                       return null;
-               $command .= " rev-list --header --max-count=".$maxcount." --skip=".$skip." ".($head ? $head : "--all")." --";
+               $args = array("rev-list", "--header", "--max-count=".$maxcount, "--skip=".$skip, ($head ? $head : "--all"), "--");
                if($file)
-                       $command .= " ".$file;
-               $commit_list = self::execute($command);
+                       $args[] = $file;
+               $commit_list = self::git_execute($args, $git_path);
                $commits = array();
                foreach(explode("\000", $commit_list) as $commit) {
                        if($commit)
@@ -116,4 +123,19 @@ class GitCommand {
                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(prag_match("#([a-f0-9]{40})#i", $result, $match))
+                       return $match[1];
+               return null;
+       }
+       
 }