Fixed search to search in the current branch.
[viewgit.git] / index.php
blobbd818e095bf43b4a060485caea698083363ce4af
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_utcstamp']),
275 require('templates/rss.php');
276 die();
280 * search - search project history
281 * @param p project
282 * @param h branch
283 * @param st search type: commit,grep,author,committer,pickaxe
284 * @param s string to search for
286 elseif ($action === 'search') {
287 $template = 'shortlog';
289 $page['project'] = validate_project($_REQUEST['p']);
291 $info = git_get_commit_info($page['project']);
292 $page['commit_id'] = $info['h'];
293 $page['tree_id'] = $info['tree'];
295 $branch = validate_hash($_REQUEST['h']);
296 $type = $_REQUEST['st'];
297 $string = $_REQUEST['s'];
299 $page['search_t'] = $type;
300 $page['search_s'] = $string;
302 $commits = git_search_commits($page['project'], $branch, $type, $string);
303 $shortlog = array();
304 foreach ($commits as $c) {
305 $info = git_get_commit_info($page['project'], $c);
306 $shortlog[] = array(
307 'author' => $info['author_name'],
308 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
309 'message' => $info['message'],
310 'commit_id' => $info['h'],
311 'tree' => $info['tree'],
312 'refs' => array(),
315 $page['shortlog'] = $shortlog;
319 * shortlog - project shortlog entries
320 * @param p project
321 * @param h OPTIONAL commit id to start showing log from
323 elseif ($action === 'shortlog') {
324 $template = 'shortlog';
325 $page['project'] = validate_project($_REQUEST['p']);
326 $page['title'] = "$page[project] - Shortlog - ViewGit";
327 $page['subtitle'] = "Shortlog";
328 if (isset($_REQUEST['h'])) {
329 $page['ref'] = validate_hash($_REQUEST['h']);
330 } else {
331 $page['ref'] = 'HEAD';
334 $info = git_get_commit_info($page['project'], $page['ref']);
335 $page['commit_id'] = $info['h'];
336 $page['tree_id'] = $info['tree'];
338 $page['shortlog'] = handle_shortlog($page['project'], $page['ref']);
340 elseif ($action === 'summary') {
341 $template = 'summary';
342 $page['project'] = validate_project($_REQUEST['p']);
343 $page['title'] = "$page[project] - Summary - ViewGit";
344 $page['subtitle'] = "Summary";
346 $info = git_get_commit_info($page['project']);
347 $page['commit_id'] = $info['h'];
348 $page['tree_id'] = $info['tree'];
350 $page['shortlog'] = handle_shortlog($page['project']);
352 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
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' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
360 'h' => $h['h'],
361 'fullname' => $h['fullname'],
362 'name' => $h['name'],
366 elseif ($action === 'tags') {
367 $template = 'tags';
368 $page['project'] = validate_project($_REQUEST['p']);
369 $page['title'] = "$page[project] - Tags - ViewGit";
371 $info = git_get_commit_info($page['project']);
372 $page['commit_id'] = $info['h'];
373 $page['tree_id'] = $info['tree'];
375 $page['tags'] = handle_tags($page['project']);
378 * Shows a tree, with list of directories/files, links to them and download
379 * links to archives.
381 * @param p project
382 * @param h tree hash
383 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
384 * one denotes which commit the user navigated from)
385 * @param f OPTIONAL path the user has followed to view this tree
387 elseif ($action === 'tree') {
388 $template = 'tree';
389 $page['project'] = validate_project($_REQUEST['p']);
390 if (isset($_REQUEST['h'])) {
391 $page['tree_id'] = validate_hash($_REQUEST['h']);
394 else {
395 // TODO walk the tree
396 $page['tree_id'] = 'HEAD';
399 $page['title'] = "$page[project] - Tree - ViewGit";
401 // 'hb' optionally contains the commit_id this tree is related to
402 if (isset($_REQUEST['hb'])) {
403 $page['commit_id'] = validate_hash($_REQUEST['hb']);
405 else {
406 // for the header
407 $info = git_get_commit_info($page['project']);
408 $page['commit_id'] = $info['h'];
411 $page['path'] = '';
412 if (isset($_REQUEST['f'])) {
413 $page['path'] = $_REQUEST['f']; // TODO validate?
416 // get path info for the header
417 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
418 if (!isset($page['tree_id'])) {
419 // Take the last hash from the tree
420 if (count($page['pathinfo']) > 0) {
421 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
422 } else {
423 $page['tree_id'] = 'HEAD';
427 $page['subtitle'] = "Tree ". substr($page['tree_id'], 0, 6);
428 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
431 * View a blob as inline, embedded on the page.
432 * @param p project
433 * @param h blob hash
434 * @param hb OPTIONAL base commit
436 elseif ($action === 'viewblob') {
437 $template = 'blob';
438 $page['project'] = validate_project($_REQUEST['p']);
439 $page['hash'] = validate_hash($_REQUEST['h']);
440 $page['title'] = "$page[project] - Blob - ViewGit";
441 if (isset($_REQUEST['hb'])) {
442 $page['commit_id'] = validate_hash($_REQUEST['hb']);
444 else {
445 $page['commit_id'] = 'HEAD';
447 $page['subtitle'] = "Blob ". substr($page['hash'], 0, 6);
449 $page['path'] = '';
450 if (isset($_REQUEST['f'])) {
451 $page['path'] = $_REQUEST['f']; // TODO validate?
454 // For the header's pagenav
455 $info = git_get_commit_info($page['project'], $page['commit_id']);
456 $page['commit_id'] = $info['h'];
457 $page['tree_id'] = $info['tree'];
459 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
461 $page['data'] = join("\n", run_git($page['project'], "cat-file blob $page[hash]"));
463 // GeSHi support
464 if ($conf['geshi'] && strpos($page['path'], '.')) {
465 $old_mask = error_reporting(E_ALL ^ E_NOTICE);
466 require_once($conf['geshi_path']);
467 $ext = array_pop(explode('.', $page['path']));
468 $lang = Geshi::get_language_name_from_extension($ext);
469 if (strlen($lang) > 0) {
470 $geshi =& new Geshi($page['data'], $lang);
471 if (is_int($conf['geshi_line_numbers'])) {
472 if ($conf['geshi_line_numbers'] == 0) {
473 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
475 else {
476 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $conf['geshi_line_numbers']);
479 $page['html_data'] = $geshi->parse_code();
481 error_reporting($old_mask);
484 elseif (in_array($action, array_keys(VGPlugin::$plugin_actions))) {
485 VGPlugin::$plugin_actions[$action]->action($action);
486 die();
488 else {
489 die('Invalid action');
492 require 'templates/header.php';
493 require "templates/$template.php";
494 require 'templates/footer.php';