Preliminary checkout support.
[viewgit.git] / index.php
blobed3021bed225b4ce88c712832e9ec86534f7bf7e
1 <?php
2 error_reporting(E_ALL);
4 require_once('inc/config.php');
6 function debug($msg)
8 file_put_contents('/tmp/viewgit.log', strftime('%H:%M:%S') ." $_SERVER[REMOTE_ADDR]:$_SERVER[REMOTE_PORT] $msg\n", FILE_APPEND);
11 /**
12 * Formats "git diff" output into xhtml.
13 * @return array(array of filenames, xhtml)
15 function format_diff($text)
17 $files = array();
19 // match every "^diff --git a/<path> b/<path>$" line
20 foreach (explode("\n", $text) as $line) {
21 if (preg_match('#^diff --git a/(.*) b/(.*)$#', $line, $matches) > 0) {
22 $files[$matches[1]] = urlencode($matches[1]);
26 $text = htmlentities($text);
28 $text = preg_replace(
29 array(
30 '/^(\+.*)$/m',
31 '/^(-.*)$/m',
32 '/^(@.*)$/m',
33 '/^([^d\+-@].*)$/m',
35 array(
36 '<span class="add">$1</span>',
37 '<span class="del">$1</span>',
38 '<span class="pos">$1</span>',
39 '<span class="etc">$1</span>',
41 $text);
42 $text = preg_replace_callback('#^diff --git a/(.*) b/(.*)$#m',
43 create_function(
44 '$m',
45 'return "<span class=\"diffline\"><a name=\"". urlencode($m[1]) ."\">diff --git a/$m[1] b/$m[2]</a></span>";'
47 $text);
49 return array($files, $text);
52 function get_project_info($name)
54 global $conf;
56 $info = $conf['projects'][$name];
57 $info['name'] = $name;
58 $info['description'] = file_get_contents($info['repo'] .'/description');
60 $headinfo = git_get_commit_info($name, 'HEAD');
61 $info['head_stamp'] = $headinfo['author_utcstamp'];
62 $info['head_datetime'] = strftime($conf['datetime'], $headinfo['author_utcstamp']);
63 $info['head_hash'] = $headinfo['h'];
64 $info['head_tree'] = $headinfo['tree'];
66 return $info;
69 /**
70 * Get details of a commit: tree, parent, author/committer (name, mail, date), message
72 function git_get_commit_info($project, $hash = 'HEAD')
74 global $conf;
76 $info = array();
77 $info['h'] = $hash;
78 $info['message_full'] = '';
80 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
81 // tree <h>
82 // parent <h>
83 // author <name> "<"<mail>">" <stamp> <timezone>
84 // committer
85 // <empty>
86 // <message>
87 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
88 foreach ($output as $line) {
89 if (substr($line, 0, 4) === 'tree') {
90 $info['tree'] = substr($line, 5);
92 elseif (substr($line, 0, 6) === 'parent') {
93 $info['parent'] = substr($line, 7);
95 elseif (preg_match($pattern, $line, $matches) > 0) {
96 $info[$matches[1] .'_name'] = $matches[2];
97 $info[$matches[1] .'_mail'] = $matches[3];
98 $info[$matches[1] .'_stamp'] = $matches[4];
99 $info[$matches[1] .'_timezone'] = $matches[5];
100 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
102 elseif (substr($line, 0, 4) === ' ') {
103 $info['message_full'] .= substr($line, 4) ."\n";
104 if (!isset($info['message'])) {
105 $info['message'] = substr($line, 4, $conf['commit_message_maxlen']);
106 $info['message_firstline'] = substr($line, 4);
109 elseif (preg_match('/^[0-9a-f]{40}$/', $line) > 0) {
110 $info['h'] = $line;
114 return $info;
117 function git_get_heads($project)
119 $heads = array();
121 $output = run_git($project, 'git show-ref --heads');
122 foreach ($output as $line) {
123 $fullname = substr($line, 41);
124 $name = array_pop(explode('/', $fullname));
125 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
128 return $heads;
131 function git_get_rev_list($project, $max_count = null, $start = 'HEAD')
133 $cmd = "git rev-list $start";
134 if (!is_null($max_count)) {
135 $cmd = "git rev-list --max-count=$max_count $start";
138 return run_git($project, $cmd);
141 function git_get_tags($project)
143 $tags = array();
145 $output = run_git($project, 'git show-ref --tags');
146 foreach ($output as $line) {
147 $fullname = substr($line, 41);
148 $name = array_pop(explode('/', $fullname));
149 $tags[] = array('h' => substr($line, 0, 40), 'fullname' => $fullname, 'name' => $name);
151 return $tags;
154 function git_ls_tree($project, $tree)
156 $entries = array();
157 $output = run_git($project, "git ls-tree $tree");
158 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
159 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
160 foreach ($output as $line) {
161 $parts = preg_split('/\s+/', $line, 4);
162 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
165 return $entries;
168 function makelink($dict)
170 $params = array();
171 foreach ($dict as $k => $v) {
172 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
174 if (count($params) > 0) {
175 return '?'. htmlentities(join('&', $params));
177 return '';
181 * Executes a git command in the project repo.
182 * @return array of output lines
184 function run_git($project, $command)
186 global $conf;
188 $output = array();
189 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
190 exec($cmd, &$output);
191 return $output;
195 * Executes a git command in the project repo, sending output directly to the
196 * client.
198 function run_git_passthru($project, $command)
200 global $conf;
202 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
203 $result = 0;
204 passthru($cmd, &$result);
205 return $result;
209 * Makes sure the given project is valid. If it's not, this function will
210 * die().
211 * @return the project
213 function validate_project($project)
215 global $conf;
217 if (!in_array($project, array_keys($conf['projects']))) {
218 die('Invalid project');
220 return $project;
224 * Makes sure the given hash is valid. If it's not, this function will die().
225 * @return the hash
227 function validate_hash($hash)
229 if (strlen($hash) != 40 || !preg_match('/^[0-9a-z]*$/', $hash)) {
230 die('Invalid hash');
233 return $hash;
236 $action = 'index';
237 $template = 'index';
238 $page['title'] = 'ViewGit';
240 if (isset($_REQUEST['a'])) {
241 $action = strtolower($_REQUEST['a']);
243 $page['action'] = $action;
245 if ($action === 'index') {
246 $template = 'index';
247 $page['title'] = 'List of projects - ViewGit';
249 foreach (array_keys($conf['projects']) as $p) {
250 $page['projects'][] = get_project_info($p);
253 elseif ($action === 'archive') {
254 $project = validate_project($_REQUEST['p']);
255 $tree = validate_hash($_REQUEST['h']);
256 $type = $_REQUEST['t'];
258 $basename = "$project-tree-$tree";
259 if (isset($_REQUEST['n'])) {
260 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
263 if ($type === 'targz') {
264 header("Content-Type: application/x-tar-gz");
265 header("Content-Transfer-Encoding: binary");
266 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
267 run_git_passthru($project, "git archive --format=tar $tree |gzip");
269 elseif ($type === 'zip') {
270 header("Content-Type: application/x-zip");
271 header("Content-Transfer-Encoding: binary");
272 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
273 run_git_passthru($project, "git archive --format=zip $tree");
275 else {
276 die('Invalid archive type requested');
279 die();
281 // blob: send a blob to browser with filename suggestion
282 elseif ($action === 'blob') {
283 $project = validate_project($_REQUEST['p']);
284 $hash = validate_hash($_REQUEST['h']);
285 $name = $_REQUEST['n'];
287 header('Content-type: application/octet-stream');
288 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
290 run_git_passthru($project, "git cat-file blob $hash");
291 die();
294 * git checkout.
296 elseif ($action === 'co') {
297 // For debugging
298 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
300 // eg. info/refs, HEAD
301 $p = validate_project($_REQUEST['p']); // project
302 $r = $_REQUEST['r']; // path
304 $gitdir = $conf['projects'][$p]['repo'];
305 $filename = $gitdir .'/'. $r;
307 // make sure the request is legit (no reading of other files besides those under git projects)
308 if ($r === 'HEAD' || $r === 'info/refs' || preg_match('!^objects/info/(packs|http-alternates|alternates)$!', $r) > 0 || preg_match('!^objects/[0-9a-f]{2}/[0-9a-f]{38}$!', $r) > 0) {
309 if (file_exists($filename)) {
310 debug('OK, sending');
311 readfile($filename);
312 } else {
313 debug('Not found');
314 header('404');
316 } else {
317 debug("Denied");
320 die();
322 elseif ($action === 'commit') {
323 $template = 'commit';
324 $page['project'] = validate_project($_REQUEST['p']);
325 $page['title'] = "$page[project] - Commit - ViewGit";
326 $page['commit_id'] = validate_hash($_REQUEST['h']);
328 $info = git_get_commit_info($page['project'], $page['commit_id']);
330 $page['author_name'] = $info['author_name'];
331 $page['author_mail'] = $info['author_mail'];
332 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
333 $page['author_datetime_local'] = strftime($conf['datetime'], $info['author_stamp']) .' '. $info['author_timezone'];
334 $page['committer_name'] = $info['committer_name'];
335 $page['committer_mail'] = $info['committer_mail'];
336 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
337 $page['committer_datetime_local'] = strftime($conf['datetime'], $info['committer_stamp']) .' '. $info['committer_timezone'];
338 $page['tree_id'] = $info['tree'];
339 $page['parent'] = $info['parent'];
340 $page['message'] = $info['message'];
341 $page['message_firstline'] = $info['message_firstline'];
342 $page['message_full'] = $info['message_full'];
345 elseif ($action === 'commitdiff') {
346 $template = 'commitdiff';
347 $page['project'] = validate_project($_REQUEST['p']);
348 $page['title'] = "$page[project] - Commitdiff - ViewGit";
349 $hash = validate_hash($_REQUEST['h']);
350 $page['commit_id'] = $hash;
352 $info = git_get_commit_info($page['project'], $hash);
354 $page['tree_id'] = $info['tree'];
356 $page['message'] = $info['message'];
357 $page['message_firstline'] = $info['message_firstline'];
358 $page['message_full'] = $info['message_full'];
359 $page['author_name'] = $info['author_name'];
360 $page['author_mail'] = $info['author_mail'];
361 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
363 $text = join("\n", run_git($page['project'], "git diff $hash^..$hash"));
364 list($page['files'], $page['diffdata']) = format_diff($text);
365 //$page['diffdata'] = format_diff($text);
367 elseif ($action === 'shortlog') {
368 $template = 'shortlog';
369 $page['project'] = validate_project($_REQUEST['p']);
370 $page['title'] = "$page[project] - Shortlog - ViewGit";
371 if (isset($_REQUEST['h'])) {
372 $page['ref'] = validate_hash($_REQUEST['h']);
373 } else {
374 $page['ref'] = 'HEAD';
377 $info = git_get_commit_info($page['project']);
378 $page['commit_id'] = $info['h'];
379 $page['tree_id'] = $info['tree'];
381 // TODO merge the logic with 'summary' below
382 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog'], $page['ref']); // TODO pass first rev as parameter
383 foreach ($revs as $rev) {
384 $info = git_get_commit_info($page['project'], $rev);
385 $page['shortlog'][] = array(
386 'author' => $info['author_name'],
387 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
388 'message' => $info['message'],
389 'commit_id' => $rev,
390 'tree' => $info['tree'],
394 elseif ($action === 'summary') {
395 $template = 'summary';
396 $page['project'] = validate_project($_REQUEST['p']);
397 $page['title'] = "$page[project] - Summary - ViewGit";
399 $info = git_get_commit_info($page['project']);
400 $page['commit_id'] = $info['h'];
401 $page['tree_id'] = $info['tree'];
403 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
404 foreach ($revs as $rev) {
405 $info = git_get_commit_info($page['project'], $rev);
406 $page['shortlog'][] = array(
407 'author' => $info['author_name'],
408 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
409 'message' => $info['message'],
410 'commit_id' => $rev,
411 'tree' => $info['tree'],
415 $tags = git_get_tags($page['project']);
416 $page['tags'] = array();
417 foreach ($tags as $tag) {
418 $info = git_get_commit_info($page['project'], $tag['h']);
419 $page['tags'][] = array(
420 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
421 'h' => $tag['h'],
422 'fullname' => $tag['fullname'],
423 'name' => $tag['name'],
427 $heads = git_get_heads($page['project']);
428 $page['heads'] = array();
429 foreach ($heads as $h) {
430 $info = git_get_commit_info($page['project'], $h['h']);
431 $page['heads'][] = array(
432 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
433 'h' => $h['h'],
434 'fullname' => $h['fullname'],
435 'name' => $h['name'],
440 * Shows a tree, with list of directories/files, links to them and download
441 * links to archives.
443 * @param p project
444 * @param h tree hash
445 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
446 * one denotes which commit the user navigated from)
448 elseif ($action === 'tree') {
449 $template = 'tree';
450 $page['project'] = validate_project($_REQUEST['p']);
451 $page['tree_id'] = validate_hash($_REQUEST['h']);
452 $page['title'] = "$page[project] - Tree - ViewGit";
454 // 'hb' optionally contains the commit_id this tree is related to
455 if (isset($_REQUEST['hb'])) {
456 $page['commit_id'] = validate_hash($_REQUEST['hb']);
458 else {
459 // for the header
460 $info = git_get_commit_info($page['project']);
461 $page['commit_id'] = $info['h'];
464 $page['path'] = '';
465 if (isset($_REQUEST['f'])) {
466 $page['path'] = $_REQUEST['f']; // TODO validate?
469 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
472 * View a blob as inline, embedded on the page.
473 * @param p project
474 * @param h blob hash
475 * @param hb OPTIONAL base commit
477 elseif ($action === 'viewblob') {
478 $template = 'blob';
479 $page['project'] = validate_project($_REQUEST['p']);
480 $page['hash'] = validate_hash($_REQUEST['h']);
481 $page['title'] = "$page[project] - Blob - ViewGit";
482 if (isset($_REQUEST['hb'])) {
483 $page['commit_id'] = validate_hash($_REQUEST['hb']);
485 else {
486 $page['commit_id'] = 'HEAD';
489 $page['path'] = '';
490 if (isset($_REQUEST['f'])) {
491 $page['path'] = $_REQUEST['f']; // TODO validate?
494 // For the header's pagenav
495 $info = git_get_commit_info($page['project'], $page['commit_id']);
496 $page['commit_id'] = $info['h'];
497 $page['tree_id'] = $info['tree'];
499 $page['data'] = join("\n", run_git($page['project'], "git cat-file blob $page[hash]"));
501 else {
502 die('Invalid action');
505 require 'templates/header.php';
506 require "templates/$template.php";
507 require 'templates/footer.php';