Simple shortlog page.
[viewgit.git] / index.php
bloba19f39519ca35b526a1f2e8c30c35f8088feab30
1 <?php
2 error_reporting(E_ALL);
4 require_once('inc/config.php');
6 /**
7 * Formats "git diff" output into xhtml.
8 */
9 function format_diff($text)
11 $text = htmlentities($text);
13 $text = preg_replace(
14 array(
15 '/^(\+.*)$/m',
16 '/^(-.*)$/m',
17 '/^(@.*)$/m',
18 '/^([^\+-@].*)$/m',
20 array(
21 '<span class="add">$1</span>',
22 '<span class="del">$1</span>',
23 '<span class="pos">$1</span>',
24 '<span class="etc">$1</span>',
26 $text);
28 return $text;
31 function get_project_info($name)
33 global $conf;
35 $info = $conf['projects'][$name];
36 $info['name'] = $name;
37 $info['description'] = file_get_contents($info['repo'] .'/description');
39 $headinfo = git_get_commit_info($name, 'HEAD');
40 $info['head_stamp'] = $headinfo['author_utcstamp'];
41 $info['head_datetime'] = strftime($conf['datetime'], $headinfo['author_utcstamp']);
42 $info['head_hash'] = $headinfo['h'];
43 $info['head_tree'] = $headinfo['tree'];
45 return $info;
48 /**
49 * Get details of a commit: tree, parent, author/committer (name, mail, date), message
51 function git_get_commit_info($project, $hash)
53 $info = array();
54 $info['h'] = $hash;
55 $info['message_full'] = '';
57 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
58 // tree <h>
59 // parent <h>
60 // author <name> "<"<mail>">" <stamp> <timezone>
61 // committer
62 // <empty>
63 // <message>
64 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
65 foreach ($output as $line) {
66 if (substr($line, 0, 4) === 'tree') {
67 $info['tree'] = substr($line, 5);
69 elseif (substr($line, 0, 6) === 'parent') {
70 $info['parent'] = substr($line, 7);
72 elseif (preg_match($pattern, $line, $matches) > 0) {
73 $info[$matches[1] .'_name'] = $matches[2];
74 $info[$matches[1] .'_mail'] = $matches[3];
75 $info[$matches[1] .'_stamp'] = $matches[4];
76 $info[$matches[1] .'_timezone'] = $matches[5];
77 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
79 elseif (substr($line, 0, 4) === ' ') {
80 $info['message_full'] .= substr($line, 4) ."\n";
81 if (!isset($info['message'])) {
82 $info['message'] = substr($line, 4, 40);
87 return $info;
90 function git_get_heads($project)
92 $heads = array();
94 $output = run_git($project, 'git show-ref --heads');
95 foreach ($output as $line) {
96 $fullname = substr($line, 41);
97 $name = array_pop(explode('/', $fullname));
98 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
101 return $heads;
104 function git_get_rev_list($project, $max_count = null, $start = 'HEAD')
106 $cmd = "git rev-list $start";
107 if (!is_null($max_count)) {
108 $cmd = "git rev-list --max-count=$max_count $start";
111 return run_git($project, $cmd);
114 function git_get_tags($project)
116 $tags = array();
118 $output = run_git($project, 'git show-ref --tags');
119 foreach ($output as $line) {
120 $fullname = substr($line, 41);
121 $name = array_pop(explode('/', $fullname));
122 $tags[] = array('h' => substr($line, 0, 40), 'fullname' => $fullname, 'name' => $name);
124 return $tags;
127 function git_ls_tree($project, $tree)
129 $entries = array();
130 $output = run_git($project, "git ls-tree $tree");
131 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
132 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
133 foreach ($output as $line) {
134 $parts = preg_split('/\s+/', $line, 4);
135 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
138 return $entries;
141 function makelink($dict)
143 $params = array();
144 foreach ($dict as $k => $v) {
145 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
147 if (count($params) > 0) {
148 return '?'. htmlentities(join('&', $params));
150 return '';
154 * Executes a git command in the project repo.
155 * @return array of output lines
157 function run_git($project, $command)
159 global $conf;
161 $output = array();
162 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
163 exec($cmd, &$output);
164 return $output;
168 * Executes a git command in the project repo, sending output directly to the
169 * client.
171 function run_git_passthru($project, $command)
173 global $conf;
175 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
176 $result = 0;
177 passthru($cmd, &$result);
178 return $result;
182 * Makes sure the given project is valid. If it's not, this function will
183 * die().
184 * @return the project
186 function validate_project($project)
188 global $conf;
190 if (!in_array($project, array_keys($conf['projects']))) {
191 die('Invalid project');
193 return $project;
197 * Makes sure the given hash is valid. If it's not, this function will die().
198 * @return the hash
200 function validate_hash($hash)
202 if (strlen($hash) != 40 || !preg_match('/^[0-9a-z]*$/', $hash)) {
203 die('Invalid hash');
206 return $hash;
209 $action = 'index';
210 $template = 'index';
211 $page['title'] = 'ViewGit';
213 if (isset($_REQUEST['a'])) {
214 $action = strtolower($_REQUEST['a']);
216 $page['action'] = $action;
218 if ($action === 'index') {
219 $template = 'index';
220 $page['title'] = 'List of projects - ViewGit';
222 foreach (array_keys($conf['projects']) as $p) {
223 $page['projects'][] = get_project_info($p);
226 elseif ($action === 'archive') {
227 $project = validate_project($_REQUEST['p']);
228 $tree = validate_hash($_REQUEST['h']);
229 $type = $_REQUEST['t'];
231 // TODO check that the data passed is really valid
232 if ($type === 'targz') {
233 header("Content-Type: application/x-tar-gz");
234 header("Content-Transfer-Encoding: binary");
235 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.tar.gz\";");
237 $data = join("\n", run_git($project, "git archive --format=tar $tree |gzip"));
238 header("Content-Length: ". strlen($data));
239 echo $data;
241 run_git_passthru($project, "git archive --format=tar $tree |gzip");
243 elseif ($type === 'zip') {
244 header("Content-Type: application/x-zip");
245 header("Content-Transfer-Encoding: binary");
246 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.zip\";");
248 $data = join("\n", run_git($project, "git archive --format=zip $tree"));
249 header("Content-Length: ". strlen($data));
250 echo $data;
252 run_git_passthru($project, "git archive --format=zip $tree");
254 else {
255 die('Invalid archive type requested');
258 die();
260 elseif ($action === 'blob') {
261 $page['project'] = validate_project($_REQUEST['p']);
262 $page['hash'] = validate_hash($_REQUEST['h']);
264 header('Content-type: text/plain');
266 run_git_passthru($page['project'], "git cat-file blob $page[hash]");
267 die();
269 elseif ($action === 'commit') {
270 $template = 'commit';
271 $page['project'] = validate_project($_REQUEST['p']);
272 $page['commit_id'] = validate_hash($_REQUEST['h']);
274 $info = git_get_commit_info($page['project'], $page['commit_id']);
276 $page['author_name'] = $info['author_name'];
277 $page['author_mail'] = $info['author_mail'];
278 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
279 $page['author_datetime_local'] = strftime($conf['datetime'], $info['author_stamp']) .' '. $info['author_timezone'];
280 $page['committer_name'] = $info['committer_name'];
281 $page['committer_mail'] = $info['committer_mail'];
282 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
283 $page['committer_datetime_local'] = strftime($conf['datetime'], $info['committer_stamp']) .' '. $info['committer_timezone'];
284 $page['tree'] = $info['tree'];
285 $page['parent'] = $info['parent'];
286 $page['message'] = $info['message'];
287 $page['message_full'] = $info['message_full'];
290 elseif ($action === 'commitdiff') {
291 $template = 'commitdiff';
292 $page['project'] = validate_project($_REQUEST['p']);
293 $hash = validate_hash($_REQUEST['h']);
294 $page['commit_id'] = $hash;
296 $info = git_get_commit_info($page['project'], $hash);
298 $page['message'] = $info['message'];
299 $page['message_full'] = $info['message_full'];
300 $page['author_name'] = $info['author_name'];
301 $page['author_mail'] = $info['author_mail'];
302 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
304 $text = join("\n", run_git($page['project'], "git diff $hash^..$hash"));
305 $page['diffdata'] = format_diff($text);
307 elseif ($action === 'shortlog') {
308 $template = 'shortlog';
309 $page['project'] = validate_project($_REQUEST['p']);
310 $page['ref'] = $_REQUEST['h']; // TODO validate
312 // TODO merge the logic with 'summary' below
313 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog'], $page['ref']); // TODO pass first rev as parameter
314 foreach ($revs as $rev) {
315 $info = git_get_commit_info($page['project'], $rev);
316 $page['shortlog'][] = array(
317 'author' => $info['author_name'],
318 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
319 'message' => $info['message'],
320 'commit_id' => $rev,
321 'tree' => $info['tree'],
325 elseif ($action === 'summary') {
326 $template = 'summary';
327 $page['project'] = validate_project($_REQUEST['p']);
328 $page['title'] = "$page[project] - Summary - ViewGit";
330 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
331 foreach ($revs as $rev) {
332 $info = git_get_commit_info($page['project'], $rev);
333 $page['shortlog'][] = array(
334 'author' => $info['author_name'],
335 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
336 'message' => $info['message'],
337 'commit_id' => $rev,
338 'tree' => $info['tree'],
342 $tags = git_get_tags($page['project']);
343 $page['tags'] = array();
344 foreach ($tags as $tag) {
345 $info = git_get_commit_info($page['project'], $tag['h']);
346 $page['tags'][] = array(
347 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
348 'h' => $tag['h'],
349 'fullname' => $tag['fullname'],
350 'name' => $tag['name'],
354 $heads = git_get_heads($page['project']);
355 $page['heads'] = array();
356 foreach ($heads as $h) {
357 $info = git_get_commit_info($page['project'], $h['h']);
358 $page['heads'][] = array(
359 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
360 'h' => $h['h'],
361 'fullname' => $h['fullname'],
362 'name' => $h['name'],
366 elseif ($action === 'tree') {
367 $template = 'tree';
368 $page['project'] = validate_project($_REQUEST['p']);
369 $page['tree'] = validate_hash($_REQUEST['h']);
370 $page['title'] = "$page[project] - Tree - ViewGit";
372 $page['entries'] = git_ls_tree($page['project'], $page['tree']);
374 else {
375 die('Invalid action');
378 require 'templates/header.php';
379 require "templates/$template.php";
380 require 'templates/footer.php';