Use --git-dir for passthru as well.
[viewgit.git] / index.php
blob5f40ef7283f3b428871700a82124953eed8039f9
1 <?php
2 /** @file
3 * The main "controller" file of ViewGit.
5 * All requests come to this file. You can think of it as the controller in the
6 * Model-View-Controller pattern. It reads config, processes user input,
7 * fetches required data using git commandline, and finally passes the data to
8 * templates to be shown to the user.
9 */
10 error_reporting(E_ALL | E_STRICT);
12 require_once('inc/config.php');
13 require_once('inc/functions.php');
14 require_once('inc/plugins.php');
16 // Include all plugins
17 foreach (glob('plugins/*/main.php') as $plugin) {
18 require_once($plugin);
20 $parts = explode('/', $plugin);
21 $name = $parts[1];
23 $classname = "${name}plugin";
24 $inst = new $classname;
27 $old_error_handler = set_error_handler('vg_error_handler');
29 // Adjust error_reporting based on config.
30 if (!$conf['debug']) {
31 error_reporting(E_ALL ^ E_NOTICE);
34 if (isset($conf['auth_lib'])){
35 require_once("inc/auth_{$conf['auth_lib']}.php");
36 auth_check();
39 if (isset($conf['projects_glob'])) {
40 foreach ($conf['projects_glob'] as $glob) {
41 foreach (glob($glob) as $path) {
42 // Get the last part of the path before .git
43 $name = preg_replace(array('#/?\.git$#', '#^.*/#'), array('', ''), $path);
45 // Workaround against name collisions; proj, proj1, proj2, ...
46 $i = '';
47 while (in_array($name . $i, array_keys($conf['projects']))) {
48 @$i++;
50 $name = $name . $i;
51 $conf['projects'][$name] = array('repo' => $path);
56 $action = 'index';
57 $template = 'index';
58 $page['title'] = 'ViewGit';
60 if (isset($_REQUEST['a'])) {
61 $action = strtolower($_REQUEST['a']);
63 $page['action'] = $action;
66 * index - list of projects
68 if ($action === 'index') {
69 $template = 'index';
70 $page['title'] = 'List of projects - ViewGit';
72 foreach (array_keys($conf['projects']) as $p) {
73 $page['projects'][] = get_project_info($p);
78 * archive - send a tree as an archive to client
79 * @param p project
80 * @param h tree hash
81 * @param t type, "targz" or "zip"
82 * @param n OPTIONAL name suggestion
84 elseif ($action === 'archive') {
85 $project = validate_project($_REQUEST['p']);
86 $tree = validate_hash($_REQUEST['h']);
87 $type = $_REQUEST['t'];
89 $basename = "$project-tree-". substr($tree, 0, 7);
90 if (isset($_REQUEST['n'])) {
91 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
94 if ($type === 'targz') {
95 header("Content-Type: application/x-tar-gz");
96 header("Content-Transfer-Encoding: binary");
97 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
98 run_git_passthru($project, "archive --format=tar $tree |gzip");
100 elseif ($type === 'zip') {
101 header("Content-Type: application/x-zip");
102 header("Content-Transfer-Encoding: binary");
103 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
104 run_git_passthru($project, "archive --format=zip $tree");
106 else {
107 die('Invalid archive type requested');
110 die();
114 * blob - send a blob to browser with filename suggestion
115 * @param p project
116 * @param h blob hash
117 * @param n filename
119 elseif ($action === 'blob') {
120 $project = validate_project($_REQUEST['p']);
121 $hash = validate_hash($_REQUEST['h']);
122 $name = $_REQUEST['n'];
124 header('Content-type: application/octet-stream');
125 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
127 run_git_passthru($project, "cat-file blob $hash");
128 die();
132 * co - git checkout. These requests come from mod_rewrite, see the .htaccess file.
133 * @param p project
134 * @param r path
136 elseif ($action === 'co') {
137 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
139 // For debugging
140 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
142 // eg. info/refs, HEAD
143 $p = validate_project($_REQUEST['p']); // project
144 $r = $_REQUEST['r']; // path
146 $gitdir = $conf['projects'][$p]['repo'];
147 $filename = $gitdir .'/'. $r;
149 // make sure the request is legit (no reading of other files besides those under git projects)
150 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) {
151 if (file_exists($filename)) {
152 debug('OK, sending');
153 readfile($filename);
154 } else {
155 debug('Not found');
156 header('HTTP/1.0 404 Not Found');
158 } else {
159 debug("Denied");
162 die();
166 * commit - view commit information
167 * @param p project
168 * @param h commit hash
170 elseif ($action === 'commit') {
171 $template = 'commit';
172 $page['project'] = validate_project($_REQUEST['p']);
173 $page['title'] = "$page[project] - Commit - ViewGit";
174 $page['commit_id'] = validate_hash($_REQUEST['h']);
175 $page['subtitle'] = "Commit ". substr($page['commit_id'], 0, 6);
177 $info = git_get_commit_info($page['project'], $page['commit_id']);
179 $page['author_name'] = $info['author_name'];
180 $page['author_mail'] = $info['author_mail'];
181 $page['author_datetime'] = gmstrftime($conf['datetime_full'], $info['author_utcstamp']);
182 $page['author_datetime_local'] = gmstrftime($conf['datetime_full'], $info['author_stamp']) .' '. $info['author_timezone'];
183 $page['committer_name'] = $info['committer_name'];
184 $page['committer_mail'] = $info['committer_mail'];
185 $page['committer_datetime'] = gmstrftime($conf['datetime_full'], $info['committer_utcstamp']);
186 $page['committer_datetime_local'] = gmstrftime($conf['datetime_full'], $info['committer_stamp']) .' '. $info['committer_timezone'];
187 $page['tree_id'] = $info['tree'];
188 $page['parents'] = $info['parents'];
189 $page['message'] = $info['message'];
190 $page['message_firstline'] = $info['message_firstline'];
191 $page['message_full'] = $info['message_full'];
196 * commitdiff - view diff of a commit
197 * @param p project
198 * @param h commit hash
200 elseif ($action === 'commitdiff') {
201 $template = 'commitdiff';
202 $page['project'] = validate_project($_REQUEST['p']);
203 $page['title'] = "$page[project] - Commitdiff - ViewGit";
204 $hash = validate_hash($_REQUEST['h']);
205 $page['commit_id'] = $hash;
206 $page['subtitle'] = "Commitdiff ". substr($page['commit_id'], 0, 6);
208 $info = git_get_commit_info($page['project'], $hash);
210 $page['tree_id'] = $info['tree'];
212 $page['message'] = $info['message'];
213 $page['message_firstline'] = $info['message_firstline'];
214 $page['message_full'] = $info['message_full'];
215 $page['author_name'] = $info['author_name'];
216 $page['author_mail'] = $info['author_mail'];
217 $page['author_datetime'] = gmstrftime($conf['datetime'], $info['author_utcstamp']);
219 $text = git_diff($page['project'], "$hash^", $hash);
220 list($page['files'], $page['diffdata']) = format_diff($text);
221 //$page['diffdata'] = format_diff($text);
224 elseif ($action === 'patch') {
225 $project = validate_project($_REQUEST['p']);
226 $hash = validate_hash($_REQUEST['h']);
227 $filename = "$project-". substr($hash, 0, 7) .".patch";
229 //header("Content-Type: text/x-diff");
230 header("Content-Type: application/octet-stream");
231 header("Content-Transfer-Encoding: binary");
232 // TODO git-style filename
233 header("Content-Disposition: attachment; filename=\"$filename\";");
235 run_git_passthru($project, "format-patch --stdout $hash^..$hash");
236 die();
240 * rss-log - RSS feed of project changes
241 * @param p project
243 elseif ($action === 'rss-log') {
244 $page['project'] = validate_project($_REQUEST['p']);
246 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
248 $page['rss_title'] = "Log for $page[project]";
249 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
250 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
251 $page['rss_pubDate'] = rss_pubdate(time());
252 $page['rss_ttl'] = $conf['rss_ttl'];
254 $page['rss_items'] = array();
256 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
258 $revs = git_get_rev_list($page['project'], $conf['rss_max_items']);
259 foreach ($revs as $rev) {
260 $info = git_get_commit_info($page['project'], $rev);
261 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
262 if ($diffstat) {
263 $info['diffstat'] = git_diffstat($page['project'], $rev);
266 $page['rss_items'][] = array(
267 'title' => rss_item_format($conf['rss_item_title'], $info),
268 'guid' => $link,
269 'link' => $link,
270 'description' => rss_item_format($conf['rss_item_description'], $info),
271 'pubdate' => rss_pubdate($info['author_stamp']),
275 require('templates/rss.php');
276 die();
280 * search - search project history
281 * @param p project
282 * @param st search type: commit,grep,author,committer,pickaxe
283 * @param s string to search for
285 elseif ($action === 'search') {
286 $template = 'shortlog';
288 $page['project'] = validate_project($_REQUEST['p']);
290 $info = git_get_commit_info($page['project']);
291 $page['commit_id'] = $info['h'];
292 $page['tree_id'] = $info['tree'];
294 $type = $_REQUEST['st'];
295 $string = $_REQUEST['s'];
297 $page['search_t'] = $type;
298 $page['search_s'] = $string;
300 $commits = git_search_commits($page['project'], $type, $string);
301 $shortlog = array();
302 foreach ($commits as $c) {
303 $info = git_get_commit_info($page['project'], $c);
304 $shortlog[] = array(
305 'author' => $info['author_name'],
306 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
307 'message' => $info['message'],
308 'commit_id' => $info['h'],
309 'tree' => $info['tree'],
310 'refs' => array(),
313 $page['shortlog'] = $shortlog;
317 * shortlog - project shortlog entries
318 * @param p project
319 * @param h OPTIONAL commit id to start showing log from
321 elseif ($action === 'shortlog') {
322 $template = 'shortlog';
323 $page['project'] = validate_project($_REQUEST['p']);
324 $page['title'] = "$page[project] - Shortlog - ViewGit";
325 $page['subtitle'] = "Shortlog";
326 if (isset($_REQUEST['h'])) {
327 $page['ref'] = validate_hash($_REQUEST['h']);
328 } else {
329 $page['ref'] = 'HEAD';
332 $info = git_get_commit_info($page['project'], $page['ref']);
333 $page['commit_id'] = $info['h'];
334 $page['tree_id'] = $info['tree'];
336 $page['shortlog'] = handle_shortlog($page['project'], $page['ref']);
338 elseif ($action === 'summary') {
339 $template = 'summary';
340 $page['project'] = validate_project($_REQUEST['p']);
341 $page['title'] = "$page[project] - Summary - ViewGit";
342 $page['subtitle'] = "Summary";
344 $info = git_get_commit_info($page['project']);
345 $page['commit_id'] = $info['h'];
346 $page['tree_id'] = $info['tree'];
348 $page['shortlog'] = handle_shortlog($page['project']);
350 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
352 $heads = git_get_heads($page['project']);
353 $page['heads'] = array();
354 foreach ($heads as $h) {
355 $info = git_get_commit_info($page['project'], $h['h']);
356 $page['heads'][] = array(
357 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
358 'h' => $h['h'],
359 'fullname' => $h['fullname'],
360 'name' => $h['name'],
364 elseif ($action === 'tags') {
365 $template = 'tags';
366 $page['project'] = validate_project($_REQUEST['p']);
367 $page['title'] = "$page[project] - Tags - ViewGit";
369 $info = git_get_commit_info($page['project']);
370 $page['commit_id'] = $info['h'];
371 $page['tree_id'] = $info['tree'];
373 $page['tags'] = handle_tags($page['project']);
376 * Shows a tree, with list of directories/files, links to them and download
377 * links to archives.
379 * @param p project
380 * @param h tree hash
381 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
382 * one denotes which commit the user navigated from)
383 * @param f OPTIONAL path the user has followed to view this tree
385 elseif ($action === 'tree') {
386 $template = 'tree';
387 $page['project'] = validate_project($_REQUEST['p']);
388 if (isset($_REQUEST['h'])) {
389 $page['tree_id'] = validate_hash($_REQUEST['h']);
392 else {
393 // TODO walk the tree
394 $page['tree_id'] = 'HEAD';
397 $page['title'] = "$page[project] - Tree - ViewGit";
399 // 'hb' optionally contains the commit_id this tree is related to
400 if (isset($_REQUEST['hb'])) {
401 $page['commit_id'] = validate_hash($_REQUEST['hb']);
403 else {
404 // for the header
405 $info = git_get_commit_info($page['project']);
406 $page['commit_id'] = $info['h'];
409 $page['path'] = '';
410 if (isset($_REQUEST['f'])) {
411 $page['path'] = $_REQUEST['f']; // TODO validate?
414 // get path info for the header
415 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
416 if (!isset($page['tree_id'])) {
417 // Take the last hash from the tree
418 if (count($page['pathinfo']) > 0) {
419 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
420 } else {
421 $page['tree_id'] = 'HEAD';
425 $page['subtitle'] = "Tree ". substr($page['tree_id'], 0, 6);
426 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
429 * View a blob as inline, embedded on the page.
430 * @param p project
431 * @param h blob hash
432 * @param hb OPTIONAL base commit
434 elseif ($action === 'viewblob') {
435 $template = 'blob';
436 $page['project'] = validate_project($_REQUEST['p']);
437 $page['hash'] = validate_hash($_REQUEST['h']);
438 $page['title'] = "$page[project] - Blob - ViewGit";
439 if (isset($_REQUEST['hb'])) {
440 $page['commit_id'] = validate_hash($_REQUEST['hb']);
442 else {
443 $page['commit_id'] = 'HEAD';
445 $page['subtitle'] = "Blob ". substr($page['hash'], 0, 6);
447 $page['path'] = '';
448 if (isset($_REQUEST['f'])) {
449 $page['path'] = $_REQUEST['f']; // TODO validate?
452 // For the header's pagenav
453 $info = git_get_commit_info($page['project'], $page['commit_id']);
454 $page['commit_id'] = $info['h'];
455 $page['tree_id'] = $info['tree'];
457 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
459 $page['data'] = join("\n", run_git($page['project'], "cat-file blob $page[hash]"));
461 // GeSHi support
462 if ($conf['geshi'] && strpos($page['path'], '.')) {
463 $old_mask = error_reporting(E_ALL ^ E_NOTICE);
464 require_once($conf['geshi_path']);
465 $ext = array_pop(explode('.', $page['path']));
466 $lang = Geshi::get_language_name_from_extension($ext);
467 if (strlen($lang) > 0) {
468 $geshi =& new Geshi($page['data'], $lang);
469 if (is_int($conf['geshi_line_numbers'])) {
470 if ($conf['geshi_line_numbers'] == 0) {
471 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
473 else {
474 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $conf['geshi_line_numbers']);
477 $page['html_data'] = $geshi->parse_code();
479 error_reporting($old_mask);
482 elseif (in_array($action, array_keys(VGPlugin::$plugin_actions))) {
483 VGPlugin::$plugin_actions[$action]->action($action);
484 die();
486 else {
487 die('Invalid action');
490 require 'templates/header.php';
491 require "templates/$template.php";
492 require 'templates/footer.php';