Show list of affected files in commit view.
[viewgit.git] / index.php
blob394ce35cb3050f81ef403ddc538c058af4d6c14c
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 $info = get_project_info($project);
87 $tree = validate_hash($_REQUEST['h']);
88 $type = $_REQUEST['t'];
90 $basename = "$project-tree-". substr($tree, 0, 7);
91 if (isset($_REQUEST['n'])) {
92 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
94 $prefix_option = '';
95 if (isset($info['archive_prefix'])) {
96 $prefix_option = "--prefix={$info['archive_prefix']}/";
98 elseif (isset($conf['archive_prefix'])) {
99 $prefix_option = "--prefix={$conf['archive_prefix']}/";
101 $prefix_option = str_replace(array('{PROJECT}'), array($project), $prefix_option);
103 if ($type === 'targz') {
104 header("Content-Type: application/x-tar-gz");
105 header("Content-Transfer-Encoding: binary");
106 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
107 run_git_passthru($project, "archive --format=tar $prefix_option $tree |gzip");
109 elseif ($type === 'zip') {
110 header("Content-Type: application/x-zip");
111 header("Content-Transfer-Encoding: binary");
112 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
113 run_git_passthru($project, "archive --format=zip $prefix_option $tree");
115 else {
116 die('Invalid archive type requested');
119 die();
123 * blob - send a blob to browser with filename suggestion
124 * @param p project
125 * @param h blob hash
126 * @param n filename
128 elseif ($action === 'blob') {
129 $project = validate_project($_REQUEST['p']);
130 $hash = validate_hash($_REQUEST['h']);
131 $name = $_REQUEST['n'];
133 header('Content-type: application/octet-stream');
134 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
136 run_git_passthru($project, "cat-file blob $hash");
137 die();
141 * co - git checkout. These requests come from mod_rewrite, see the .htaccess file.
142 * @param p project
143 * @param r path
145 elseif ($action === 'co') {
146 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
148 // For debugging
149 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
151 // eg. info/refs, HEAD
152 $p = validate_project($_REQUEST['p']); // project
153 $r = $_REQUEST['r']; // path
155 $gitdir = $conf['projects'][$p]['repo'];
156 $filename = $gitdir .'/'. $r;
158 // make sure the request is legit (no reading of other files besides those under git projects)
159 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 || preg_match('!^objects/pack/pack-[0-9a-f]{40}\.(idx|pack)$!', $r)) {
160 if (file_exists($filename)) {
161 debug('OK, sending');
162 readfile($filename);
163 } else {
164 debug('Not found');
165 header('HTTP/1.0 404 Not Found');
167 } else {
168 debug("Denied");
171 die();
175 * commit - view commit information
176 * @param p project
177 * @param h commit hash
179 elseif ($action === 'commit') {
180 $template = 'commit';
181 $page['project'] = validate_project($_REQUEST['p']);
182 $page['title'] = "$page[project] - Commit - ViewGit";
183 $page['commit_id'] = validate_hash($_REQUEST['h']);
184 $page['subtitle'] = "Commit ". substr($page['commit_id'], 0, 6);
186 $info = git_get_commit_info($page['project'], $page['commit_id']);
188 $page['author_name'] = $info['author_name'];
189 $page['author_mail'] = $info['author_mail'];
190 $page['author_datetime'] = $info['author_datetime'];
191 $page['author_datetime_local'] = $info['author_datetime_local'];
192 $page['committer_name'] = $info['committer_name'];
193 $page['committer_mail'] = $info['committer_mail'];
194 $page['committer_datetime'] = $info['committer_datetime'];
195 $page['committer_datetime_local'] = $info['committer_datetime_local'];
196 $page['tree_id'] = $info['tree'];
197 $page['parents'] = $info['parents'];
198 $page['message'] = $info['message'];
199 $page['message_firstline'] = $info['message_firstline'];
200 $page['message_full'] = $info['message_full'];
201 $page['affected_files'] = $info['affected_files'];
206 * commitdiff - view diff of a commit
207 * @param p project
208 * @param h commit hash
210 elseif ($action === 'commitdiff') {
211 $template = 'commitdiff';
212 $page['project'] = validate_project($_REQUEST['p']);
213 $page['title'] = "$page[project] - Commitdiff - ViewGit";
214 $hash = validate_hash($_REQUEST['h']);
215 $page['commit_id'] = $hash;
216 $page['subtitle'] = "Commitdiff ". substr($page['commit_id'], 0, 6);
218 $info = git_get_commit_info($page['project'], $hash);
220 $page['tree_id'] = $info['tree'];
222 $page['message'] = $info['message'];
223 $page['message_firstline'] = $info['message_firstline'];
224 $page['message_full'] = $info['message_full'];
225 $page['author_name'] = $info['author_name'];
226 $page['author_mail'] = $info['author_mail'];
227 $page['author_datetime'] = $info['author_datetime'];
229 $text = git_diff($page['project'], "$hash^", $hash);
230 list($page['files'], $page['diffdata']) = format_diff($text);
231 //$page['diffdata'] = format_diff($text);
234 elseif ($action === 'patch') {
235 $project = validate_project($_REQUEST['p']);
236 $hash = validate_hash($_REQUEST['h']);
237 $filename = "$project-". substr($hash, 0, 7) .".patch";
239 //header("Content-Type: text/x-diff");
240 header("Content-Type: application/octet-stream");
241 header("Content-Transfer-Encoding: binary");
242 // TODO git-style filename
243 header("Content-Disposition: attachment; filename=\"$filename\";");
245 run_git_passthru($project, "format-patch --stdout $hash^..$hash");
246 die();
250 * rss-log - RSS feed of project changes
251 * @param p project
253 elseif ($action === 'rss-log') {
254 $page['project'] = validate_project($_REQUEST['p']);
256 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
258 $page['rss_title'] = "Log for $page[project]";
259 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
260 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
261 $page['rss_pubDate'] = rss_pubdate(time());
262 $page['rss_ttl'] = $conf['rss_ttl'];
264 $page['rss_items'] = array();
266 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
268 $revs = git_get_rev_list($page['project'], 0, $conf['rss_max_items']);
269 foreach ($revs as $rev) {
270 $info = git_get_commit_info($page['project'], $rev);
271 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
272 if ($diffstat) {
273 $info['diffstat'] = git_diffstat($page['project'], $rev);
276 $page['rss_items'][] = array(
277 'title' => rss_item_format($conf['rss_item_title'], $info),
278 'guid' => $link,
279 'link' => $link,
280 'description' => rss_item_format($conf['rss_item_description'], $info),
281 'pubdate' => rss_pubdate($info['author_utcstamp']),
285 require('templates/rss.php');
286 die();
290 * search - search project history
291 * @param p project
292 * @param h branch
293 * @param st search type: commit,grep,author,committer,pickaxe
294 * @param s string to search for
296 elseif ($action === 'search') {
297 $template = 'shortlog';
299 $page['project'] = validate_project($_REQUEST['p']);
301 $info = git_get_commit_info($page['project']);
302 $page['commit_id'] = $info['h'];
303 $page['tree_id'] = $info['tree'];
305 $branch = validate_hash($_REQUEST['h']);
306 $type = $_REQUEST['st'];
307 $string = $_REQUEST['s'];
309 $page['search_t'] = $type;
310 $page['search_s'] = $string;
312 $commits = git_search_commits($page['project'], $branch, $type, $string);
313 $shortlog = array();
314 foreach ($commits as $c) {
315 $info = git_get_commit_info($page['project'], $c);
316 $shortlog[] = array(
317 'author' => $info['author_name'],
318 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
319 'message' => $info['message'],
320 'commit_id' => $info['h'],
321 'tree' => $info['tree'],
322 'refs' => array(),
325 $page['shortlog_no_more'] = true;
326 $page['shortlog'] = $shortlog;
330 * shortlog - project shortlog entries
331 * @param p project
332 * @param h OPTIONAL commit id to start showing log from
334 elseif ($action === 'shortlog') {
335 $template = 'shortlog';
336 $page['project'] = validate_project($_REQUEST['p']);
337 $page['title'] = "$page[project] - Shortlog - ViewGit";
338 $page['subtitle'] = "Shortlog";
339 if (isset($_REQUEST['h'])) {
340 $page['ref'] = validate_hash($_REQUEST['h']);
341 } else {
342 $page['ref'] = 'HEAD';
344 if (isset($_REQUEST['pg'])) {
345 $page['pg'] = intval($_REQUEST['pg']);
346 } else {
347 $page['pg'] = 0;
350 $info = git_get_commit_info($page['project'], $page['ref']);
351 $page['commit_id'] = $info['h'];
352 $page['tree_id'] = $info['tree'];
354 $page['shortlog'] = handle_shortlog($page['project'], $page['ref'], $page['pg']);
356 elseif ($action === 'summary') {
357 $template = 'summary';
358 $page['project'] = validate_project($_REQUEST['p']);
359 $page['title'] = "$page[project] - Summary - ViewGit";
360 $page['subtitle'] = "Summary";
362 $info = git_get_commit_info($page['project']);
363 $page['commit_id'] = $info['h'];
364 $page['tree_id'] = $info['tree'];
366 $page['shortlog'] = handle_shortlog($page['project']);
368 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
369 $page['ref'] = 'HEAD';
371 $heads = git_get_heads($page['project']);
372 $page['heads'] = array();
373 foreach ($heads as $h) {
374 $info = git_get_commit_info($page['project'], $h['h']);
375 $page['heads'][] = array(
376 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
377 'h' => $h['h'],
378 'fullname' => $h['fullname'],
379 'name' => $h['name'],
383 elseif ($action === 'tags') {
384 $template = 'tags';
385 $page['project'] = validate_project($_REQUEST['p']);
386 $page['title'] = "$page[project] - Tags - ViewGit";
388 $info = git_get_commit_info($page['project']);
389 $page['commit_id'] = $info['h'];
390 $page['tree_id'] = $info['tree'];
392 $page['tags'] = handle_tags($page['project']);
395 * Shows a tree, with list of directories/files, links to them and download
396 * links to archives.
398 * @param p project
399 * @param h tree hash
400 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
401 * one denotes which commit the user navigated from)
402 * @param f OPTIONAL path the user has followed to view this tree
404 elseif ($action === 'tree') {
405 $template = 'tree';
406 $page['project'] = validate_project($_REQUEST['p']);
407 if (isset($_REQUEST['h'])) {
408 $page['tree_id'] = validate_hash($_REQUEST['h']);
411 else {
412 // TODO walk the tree
413 $page['tree_id'] = 'HEAD';
416 $page['title'] = "$page[project] - Tree - ViewGit";
418 // 'hb' optionally contains the commit_id this tree is related to
419 if (isset($_REQUEST['hb'])) {
420 $page['commit_id'] = validate_hash($_REQUEST['hb']);
422 else {
423 // for the header
424 $info = git_get_commit_info($page['project']);
425 $page['commit_id'] = $info['h'];
428 $page['path'] = '';
429 if (isset($_REQUEST['f'])) {
430 $page['path'] = $_REQUEST['f']; // TODO validate?
433 // get path info for the header
434 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
435 if (!isset($page['tree_id'])) {
436 // Take the last hash from the tree
437 if (count($page['pathinfo']) > 0) {
438 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
439 } else {
440 $page['tree_id'] = 'HEAD';
444 $page['subtitle'] = "Tree ". substr($page['tree_id'], 0, 6);
445 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
448 * View a blob as inline, embedded on the page.
449 * @param p project
450 * @param h blob hash
451 * @param hb OPTIONAL base commit
453 elseif ($action === 'viewblob') {
454 $template = 'blob';
455 $page['project'] = validate_project($_REQUEST['p']);
456 $page['hash'] = validate_hash($_REQUEST['h']);
457 $page['title'] = "$page[project] - Blob - ViewGit";
458 if (isset($_REQUEST['hb'])) {
459 $page['commit_id'] = validate_hash($_REQUEST['hb']);
461 else {
462 $page['commit_id'] = 'HEAD';
464 $page['subtitle'] = "Blob ". substr($page['hash'], 0, 6);
466 $page['path'] = '';
467 if (isset($_REQUEST['f'])) {
468 $page['path'] = $_REQUEST['f']; // TODO validate?
471 // For the header's pagenav
472 $info = git_get_commit_info($page['project'], $page['commit_id']);
473 $page['commit_id'] = $info['h'];
474 $page['tree_id'] = $info['tree'];
476 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
478 $page['data'] = join("\n", run_git($page['project'], "cat-file blob $page[hash]"));
479 $page['lastlog'] = git_get_commit_info($page['project'], 'HEAD', $page['path']);
481 // GeSHi support
482 if ($conf['geshi'] && strpos($page['path'], '.')) {
483 $old_mask = error_reporting(E_ALL ^ E_NOTICE);
484 require_once($conf['geshi_path']);
485 $parts = explode('.', $page['path']);
486 $ext = array_pop($parts);
487 $geshi = new Geshi($page['data']);
488 $lang = $geshi->get_language_name_from_extension($ext);
489 if (strlen($lang) > 0) {
490 $geshi->set_language($lang);
491 if (is_int($conf['geshi_line_numbers'])) {
492 if ($conf['geshi_line_numbers'] == 0) {
493 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
495 else {
496 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $conf['geshi_line_numbers']);
499 $page['html_data'] = $geshi->parse_code();
501 error_reporting($old_mask);
504 elseif (in_array($action, array_keys(VGPlugin::$plugin_actions))) {
505 VGPlugin::$plugin_actions[$action]->action($action);
506 die();
508 else {
509 die('Invalid action');
512 require 'templates/header.php';
513 require "templates/$template.php";
514 require 'templates/footer.php';