started diff parser
[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                         } else
157                                 $entry['from_file'] = $entry['to_file'] = $entry['file'];
158                 } else if(preg_match('/^(::+)((?:[0-7]{6} )+)((?:[0-9a-fA-F]{40} )+)([a-zA-Z]+)\t(.*)$/i', $line, $matches)) {
159                         $entry['parents'] = strlen($matches[1]);
160                         $from_modes = explode(" ", $matches[2]);
161                         $entry['from_mode'] = array_slice($from_modes, 1, -1);
162                         $entry['to_mode'] = $from_modes[0];
163                         $from_ids = explode(" ", $matches[3]);
164                         $entry['from_id'] = array_slice($from_ids, 1, -1);
165                         $entry['to_id'] = $from_ids[0];
166                         $entry['status'] = str_split($matches[4]);
167                         $entry['file'] = $entry['from_file'] = $entry['to_file'] = $matches[5];
168                 }
169                 return $entry;
170         }
171         
172         public static function get_commit_changes($git_path, $commit_id, $parents) {
173                 $args = array("diff-tree", "-r", "--no-commit-id");
174                 switch(GitConfig::DETECT_RENAME_LEVEL) {
175                 case 0:
176                         $args[] = "-M";
177                         break;
178                 case 1:
179                         $args[] = "-C";
180                         break;
181                 case 2:
182                         $args[] = "-C";
183                         $args[] = "--find-copies-harder";
184                         break;
185                 }
186                 if(GitConfig::DETECT_REWRITES)
187                         $args[] = "-B";
188                 if(is_array($parents) && count($parents) > 1)
189                         $args[] = "-c";
190                 else if(is_array($parents) && count($parents) == 1)
191                         $args[] = $parents[0];
192                 else
193                         $args[] = "--root";
194                 $args[] = $commit_id;
195                 $args[] = "--";
196                 $result = self::git_execute($args, $git_path);
197                 $tree = array();
198                 foreach(explode("\n", $result) as $line) {
199                         if($line == "")
200                                 continue;
201                         $tree[] = self::parse_difftree($line);
202                 }
203                 return $tree;
204         }
205         
206         public static function get_commit_diff($git_path, $commit_id, $parents) {
207                 $args = array("diff-tree", "-r", "--no-commit-id", "--patch-with-raw", "--full-index");
208                 switch(GitConfig::DETECT_RENAME_LEVEL) {
209                 case 0:
210                         $args[] = "-M";
211                         break;
212                 case 1:
213                         $args[] = "-C";
214                         break;
215                 case 2:
216                         $args[] = "-C";
217                         $args[] = "--find-copies-harder";
218                         break;
219                 }
220                 if(GitConfig::DETECT_REWRITES)
221                         $args[] = "-B";
222                 if(is_array($parents) && count($parents) > 1)
223                         $args[] = "--cc";
224                 else if(is_array($parents) && count($parents) == 1)
225                         $args[] = $parents[0];
226                 else
227                         $args[] = "--root";
228                 $args[] = $commit_id;
229                 $args[] = "--";
230                 $result = self::git_execute($args, $git_path);
231                 $tree = array();
232                 $diffs = array();
233                 $diff = null;
234                 $parse_difftree = true;
235                 foreach(explode("\n", $result) as $line) {
236                         if($line == "" && $parse_difftree)
237                                 $parse_difftree = false;
238                         else if($parse_difftree)
239                                 $tree[] = self::parse_difftree($line);
240                         else {
241                                 if(preg_match('/^diff/i', $line)) {
242                                         if($diff)
243                                                 $diffs[] = $diff;
244                                         $diff = array();
245                                 }
246                                 $diff[] = $line;
247                         }
248                 }
249                 if($diff)
250                         $diffs[] = $diff;
251                 $diff_data['tree'] = $tree;
252                 return array('tree' => $tree, 'diffs' => $diffs);
253         }
254 }