1e4b07f6b4c29e4001c22e2bc2031804ea7809ee
[phpgitweb.git] / htdocs / lib / GitCommand.class.php
1 <?php
2 /* GitCommand.class.php - phpgitweb
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
4  * 
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.
9  * 
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.
14  * 
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/>. 
17  */
18
19 class GitCommand {
20         private static $git = null;
21         private static $counter = 0;
22         
23         private static function command_header() {
24                 if(!self::$git) {
25                         //find git executable
26                         if(GitConfig::GIT_EXEC)
27                                 self::$git = GitConfig::GIT_EXEC;
28                         else
29                                 self::$git = str_replace(array("\n", "\r"), array("", ""), `which git`);
30                         if(!self::$git) {
31                                 trigger_error("Can not find git executable.", E_USER_ERROR);
32                                 self::$git = "git";
33                         }
34                 }
35                 $command = self::$git;
36                 
37                 return $command;
38         }
39         
40         private static function git_execute($params, $git_path = null) {
41                 $result = array();
42                 
43                 if($git_path) {
44                         $args = array("--git-dir=".$git_path);
45                         $args = array_merge($args, $params);
46                 } else
47                         $args = $params;
48                 
49                 $command = self::command_header();
50                 foreach($args as $arg) {
51                         $command .= ' '.escapeshellarg($arg);
52                 }
53                 
54                 exec($command, $result);
55                 self::$counter++;
56                 return implode("\n", $result);
57         }
58         
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);
63                 return $result[0];
64         }
65         
66         public static function last_activity($git_path, $ref = null) {
67                 $args = array("for-each-ref", "--format=%(committer)", "--sort=-committerdate", "--count=1");
68                 if($ref)
69                         $args[] = $ref;
70                 $age = self::git_execute($args, $git_path);
71                 preg_match("/[0-9]{9,}/i", $age, $result);
72                 return $result[0];
73         }
74         
75         private static function parse_commit($commit_data) {
76                 $commit = array();
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']))
81                         return null;
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";
86                                 else
87                                         $commit['text'] = '';
88                                 $commit['text'] .= substr($rev_line, 4);
89                         } else {
90                                 $opt = explode(" ", $rev_line, 2);
91                                 if($opt[0] == "tree")
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];
107                                 }
108                         }
109                 }
110                 return $commit;
111         }
112         
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"), "--");
115                 if($file)
116                         $args[] = $file;
117                 $commit_list = self::git_execute($args, $git_path);
118                 $commits = array();
119                 foreach(explode("\000", $commit_list) as $commit) {
120                         if($commit)
121                                 $commits[] = self::parse_commit($commit);
122                 }
123                 return $commits;
124         }
125         
126         public static function get_commit($git_path, $commit_id) {
127                 $args = array("rev-list", "--header", "--max-count=1", ($commit_id ? $commit_id : "--all"), "--");
128                 $commit_data = self::git_execute($args, $git_path);
129                 $commit = self::parse_commit($commit_data);
130                 return $commit;
131         }
132         
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))
137                         return $match[1];
138                 return null;
139         }
140         
141         private static function parse_difftree($line) {
142                 $entry = array();
143                 if(preg_match('/^:([0-7]{6}) ([0-7]{6}) ([0-9a-fA-F]{40}) ([0-9a-fA-F]{40}) (.)([0-9]{0,3})\t(.*)$/i', $line, $matches)) {
144                         $entry['parents'] = 1;
145                         $entry['from_mode'] = $matches[1];
146                         $entry['to_mode'] = $matches[2];
147                         $entry['from_id'] = $matches[3];
148                         $entry['to_id'] = $matches[4];
149                         $entry['status'] = $matches[5];
150                         $entry['similarity'] = $matches[6];
151                         $entry['file'] = $matches[7];
152                         if($entry['status'] == 'R' || $entry['status'] == 'C') { //renamed or copied
153                                 $files = explode("\t", $entry['file']);
154                                 $entry['from_file'] = $files[0];
155                                 $entry['to_file'] = $files[1];
156                         }
157                 } else if(preg_match('/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$/i', $line, $matches)) {
158                         $entry['parents'] = strlen($matches[1]);
159                         $from_modes = explode(" ", $matches[2]);
160                         $entry['from_mode'] = array_slice($from_modes, 1);
161                         $entry['to_mode'] = $from_modes[0];
162                         $from_ids = explode(" ", $matches[3]);
163                         $entry['from_id'] = array_slice($from_ids, 1);
164                         $entry['to_id'] = $from_ids[0];
165                         $entry['status'] = str_split($matches[4]);
166                         $entry['file'] = $matches[5];
167                 }
168                 return $entry;
169         }
170         
171         public static function get_commit_changes($git_path, $commit_id, $parents) {
172                 $args = array("diff-tree", "-r", "--no-commit-id");
173                 switch(GitConfig::DETECT_RENAME_LEVEL) {
174                 case 0:
175                         $args[] = "-M";
176                         break;
177                 case 1:
178                         $args[] = "-C";
179                         break;
180                 case 2:
181                         $args[] = "-C";
182                         $args[] = "--find-copies-harder";
183                         break;
184                 }
185                 if(GitConfig::DETECT_REWRITES)
186                         $args[] = "-B";
187                 if(is_array($parents) && count($parents) > 1)
188                         $args[] = "-c";
189                 else if(is_array($parents) && count($parents) == 1)
190                         $args[] = $parents[0];
191                 else
192                         $args[] = "--root";
193                 $args[] = $commit_id;
194                 $args[] = "--";
195                 $result = self::git_execute($args, $git_path);
196                 $tree = array();
197                 foreach(explode("\n", $result) as $line) {
198                         if($line == "")
199                                 continue;
200                         $tree[] = self::parse_difftree($line);
201                 }
202                 return $tree;
203         }
204         
205         public static function get_commit_diff($git_path, $commit_id, $parents) {
206                 $args = array("diff-tree", "-r", "--no-commit-id", "--patch-with-raw", "--full-index");
207                 switch(GitConfig::DETECT_RENAME_LEVEL) {
208                 case 0:
209                         $args[] = "-M";
210                         break;
211                 case 1:
212                         $args[] = "-C";
213                         break;
214                 case 2:
215                         $args[] = "-C";
216                         $args[] = "--find-copies-harder";
217                         break;
218                 }
219                 if(GitConfig::DETECT_REWRITES)
220                         $args[] = "-B";
221                 if(is_array($parents) && count($parents) > 1)
222                         $args[] = "--cc";
223                 else if(is_array($parents) && count($parents) == 1)
224                         $args[] = $parents[0];
225                 else
226                         $args[] = "--root";
227                 $args[] = $commit_id;
228                 $args[] = "--";
229                 $result = self::git_execute($args, $git_path);
230                 $tree = array();
231                 $diffs = array();
232                 $diff = null;
233                 $parse_difftree = true;
234                 foreach(explode("\n", $result) as $line) {
235                         if($line == "" && $parse_difftree)
236                                 $parse_difftree = false;
237                         else if($parse_difftree)
238                                 $tree[] = self::parse_difftree($line);
239                         else {
240                                 if(preg_match('/^diff/i', $line)) {
241                                         if($diff)
242                                                 $diffs[] = $diff;
243                                         $diff = array();
244                                 }
245                                 $diff[] = $line;
246                         }
247                 }
248                 if($diff)
249                         $diffs[] = $diff;
250                 $diff_data['tree'] = $tree;
251                 return array('tree' => $tree, 'diffs' => $diffs);
252         }
253 }