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.
10 error_reporting(E_ALL | E_STRICT
);
12 require_once('inc/config.php');
13 require_once('inc/functions.php');
15 $old_error_handler = set_error_handler('vg_error_handler');
17 // Adjust error_reporting based on config.
18 if (!$conf['debug']) {
19 error_reporting(E_ALL ^ E_NOTICE
);
22 if (isset($conf['auth_lib'])){
23 require_once("inc/auth_{$conf['auth_lib']}.php");
27 if (isset($conf['projects_glob'])) {
28 foreach ($conf['projects_glob'] as $glob) {
29 foreach (glob($glob) as $path) {
30 // Get the last part of the path before .git
31 $name = preg_replace(array('#/?\.git$#', '#^.*/#'), array('', ''), $path);
33 // Workaround against name collisions; proj, proj1, proj2, ...
35 while (in_array($name . $i, array_keys($conf['projects']))) {
39 $conf['projects'][$name] = array('repo' => $path);
46 $page['title'] = 'ViewGit';
48 if (isset($_REQUEST['a'])) {
49 $action = strtolower($_REQUEST['a']);
51 $page['action'] = $action;
54 * index - list of projects
56 if ($action === 'index') {
58 $page['title'] = 'List of projects - ViewGit';
60 foreach (array_keys($conf['projects']) as $p) {
61 $page['projects'][] = get_project_info($p);
66 * archive - send a tree as an archive to client
69 * @param t type, "targz" or "zip"
70 * @param n OPTIONAL name suggestion
72 elseif ($action === 'archive') {
73 $project = validate_project($_REQUEST['p']);
74 $tree = validate_hash($_REQUEST['h']);
75 $type = $_REQUEST['t'];
77 $basename = "$project-tree-". substr($tree, 0, 7);
78 if (isset($_REQUEST['n'])) {
79 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
82 if ($type === 'targz') {
83 header("Content-Type: application/x-tar-gz");
84 header("Content-Transfer-Encoding: binary");
85 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
86 run_git_passthru($project, "archive --format=tar $tree |gzip");
88 elseif ($type === 'zip') {
89 header("Content-Type: application/x-zip");
90 header("Content-Transfer-Encoding: binary");
91 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
92 run_git_passthru($project, "archive --format=zip $tree");
95 die('Invalid archive type requested');
102 * blob - send a blob to browser with filename suggestion
107 elseif ($action === 'blob') {
108 $project = validate_project($_REQUEST['p']);
109 $hash = validate_hash($_REQUEST['h']);
110 $name = $_REQUEST['n'];
112 header('Content-type: application/octet-stream');
113 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
115 run_git_passthru($project, "cat-file blob $hash");
120 * co - git checkout. These requests come from mod_rewrite, see the .htaccess file.
124 elseif ($action === 'co') {
125 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
128 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
130 // eg. info/refs, HEAD
131 $p = validate_project($_REQUEST['p']); // project
132 $r = $_REQUEST['r']; // path
134 $gitdir = $conf['projects'][$p]['repo'];
135 $filename = $gitdir .'/'. $r;
137 // make sure the request is legit (no reading of other files besides those under git projects)
138 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) {
139 if (file_exists($filename)) {
140 debug('OK, sending');
144 header('HTTP/1.0 404 Not Found');
154 * commit - view commit information
156 * @param h commit hash
158 elseif ($action === 'commit') {
159 $template = 'commit';
160 $page['project'] = validate_project($_REQUEST['p']);
161 $page['title'] = "$page[project] - Commit - ViewGit";
162 $page['commit_id'] = validate_hash($_REQUEST['h']);
164 $info = git_get_commit_info($page['project'], $page['commit_id']);
166 $page['author_name'] = $info['author_name'];
167 $page['author_mail'] = $info['author_mail'];
168 $page['author_datetime'] = gmstrftime($conf['datetime_full'], $info['author_utcstamp']);
169 $page['author_datetime_local'] = gmstrftime($conf['datetime_full'], $info['author_stamp']) .' '. $info['author_timezone'];
170 $page['committer_name'] = $info['committer_name'];
171 $page['committer_mail'] = $info['committer_mail'];
172 $page['committer_datetime'] = gmstrftime($conf['datetime_full'], $info['committer_utcstamp']);
173 $page['committer_datetime_local'] = gmstrftime($conf['datetime_full'], $info['committer_stamp']) .' '. $info['committer_timezone'];
174 $page['tree_id'] = $info['tree'];
175 $page['parents'] = $info['parents'];
176 $page['message'] = $info['message'];
177 $page['message_firstline'] = $info['message_firstline'];
178 $page['message_full'] = $info['message_full'];
183 * commitdiff - view diff of a commit
185 * @param h commit hash
187 elseif ($action === 'commitdiff') {
188 $template = 'commitdiff';
189 $page['project'] = validate_project($_REQUEST['p']);
190 $page['title'] = "$page[project] - Commitdiff - ViewGit";
191 $hash = validate_hash($_REQUEST['h']);
192 $page['commit_id'] = $hash;
194 $info = git_get_commit_info($page['project'], $hash);
196 $page['tree_id'] = $info['tree'];
198 $page['message'] = $info['message'];
199 $page['message_firstline'] = $info['message_firstline'];
200 $page['message_full'] = $info['message_full'];
201 $page['author_name'] = $info['author_name'];
202 $page['author_mail'] = $info['author_mail'];
203 $page['author_datetime'] = gmstrftime($conf['datetime'], $info['author_utcstamp']);
205 $text = git_diff($page['project'], "$hash^", $hash);
206 list($page['files'], $page['diffdata']) = format_diff($text);
207 //$page['diffdata'] = format_diff($text);
210 elseif ($action === 'patch') {
211 $project = validate_project($_REQUEST['p']);
212 $hash = validate_hash($_REQUEST['h']);
213 $filename = "$project-". substr($hash, 0, 7) .".patch";
215 //header("Content-Type: text/x-diff");
216 header("Content-Type: application/octet-stream");
217 header("Content-Transfer-Encoding: binary");
218 // TODO git-style filename
219 header("Content-Disposition: attachment; filename=\"$filename\";");
221 run_git_passthru($project, "format-patch --stdout $hash^..$hash");
226 * rss-log - RSS feed of project changes
229 elseif ($action === 'rss-log') {
230 $page['project'] = validate_project($_REQUEST['p']);
232 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
234 $page['rss_title'] = "Log for $page[project]";
235 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
236 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
237 $page['rss_pubDate'] = rss_pubdate(time());
238 $page['rss_ttl'] = $conf['rss_ttl'];
240 $page['rss_items'] = array();
242 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
244 $revs = git_get_rev_list($page['project'], $conf['rss_max_items']);
245 foreach ($revs as $rev) {
246 $info = git_get_commit_info($page['project'], $rev);
247 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
249 $info['diffstat'] = git_diffstat($page['project'], $rev);
252 $page['rss_items'][] = array(
253 'title' => rss_item_format($conf['rss_item_title'], $info),
256 'description' => rss_item_format($conf['rss_item_description'], $info),
257 'pubdate' => rss_pubdate($info['author_stamp']),
261 require('templates/rss.php');
266 * search - search project history
268 * @param st search type: commit,grep,author,committer,pickaxe
269 * @param s string to search for
271 elseif ($action === 'search') {
272 $template = 'shortlog';
274 $page['project'] = validate_project($_REQUEST['p']);
276 $info = git_get_commit_info($page['project']);
277 $page['commit_id'] = $info['h'];
278 $page['tree_id'] = $info['tree'];
280 $type = $_REQUEST['st'];
281 $string = $_REQUEST['s'];
283 $page['search_t'] = $type;
284 $page['search_s'] = $string;
286 $commits = git_search_commits($page['project'], $type, $string);
288 foreach ($commits as $c) {
289 $info = git_get_commit_info($page['project'], $c);
291 'author' => $info['author_name'],
292 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
293 'message' => $info['message'],
294 'commit_id' => $info['h'],
295 'tree' => $info['tree'],
299 $page['shortlog'] = $shortlog;
303 * shortlog - project shortlog entries
305 * @param h OPTIONAL commit id to start showing log from
307 elseif ($action === 'shortlog') {
308 $template = 'shortlog';
309 $page['project'] = validate_project($_REQUEST['p']);
310 $page['title'] = "$page[project] - Shortlog - ViewGit";
311 if (isset($_REQUEST['h'])) {
312 $page['ref'] = validate_hash($_REQUEST['h']);
314 $page['ref'] = 'HEAD';
317 $info = git_get_commit_info($page['project'], $page['ref']);
318 $page['commit_id'] = $info['h'];
319 $page['tree_id'] = $info['tree'];
321 $page['shortlog'] = handle_shortlog($page['project'], $page['ref']);
323 elseif ($action === 'summary') {
324 $template = 'summary';
325 $page['project'] = validate_project($_REQUEST['p']);
326 $page['title'] = "$page[project] - Summary - ViewGit";
328 $info = git_get_commit_info($page['project']);
329 $page['commit_id'] = $info['h'];
330 $page['tree_id'] = $info['tree'];
332 $page['shortlog'] = handle_shortlog($page['project']);
334 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
336 $heads = git_get_heads($page['project']);
337 $page['heads'] = array();
338 foreach ($heads as $h) {
339 $info = git_get_commit_info($page['project'], $h['h']);
340 $page['heads'][] = array(
341 'date' => gmstrftime($conf['datetime'], $info['author_utcstamp']),
343 'fullname' => $h['fullname'],
344 'name' => $h['name'],
348 elseif ($action === 'tags') {
350 $page['project'] = validate_project($_REQUEST['p']);
351 $page['title'] = "$page[project] - Tags - ViewGit";
353 $info = git_get_commit_info($page['project']);
354 $page['commit_id'] = $info['h'];
355 $page['tree_id'] = $info['tree'];
357 $page['tags'] = handle_tags($page['project']);
360 * Shows a tree, with list of directories/files, links to them and download
365 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
366 * one denotes which commit the user navigated from)
367 * @param f OPTIONAL path the user has followed to view this tree
369 elseif ($action === 'tree') {
371 $page['project'] = validate_project($_REQUEST['p']);
372 if (isset($_REQUEST['h'])) {
373 $page['tree_id'] = validate_hash($_REQUEST['h']);
377 // TODO walk the tree
378 $page['tree_id'] = 'HEAD';
381 $page['title'] = "$page[project] - Tree - ViewGit";
383 // 'hb' optionally contains the commit_id this tree is related to
384 if (isset($_REQUEST['hb'])) {
385 $page['commit_id'] = validate_hash($_REQUEST['hb']);
389 $info = git_get_commit_info($page['project']);
390 $page['commit_id'] = $info['h'];
394 if (isset($_REQUEST['f'])) {
395 $page['path'] = $_REQUEST['f']; // TODO validate?
398 // get path info for the header
399 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
400 if (!isset($page['tree_id'])) {
401 // Take the last hash from the tree
402 if (count($page['pathinfo']) > 0) {
403 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
405 $page['tree_id'] = 'HEAD';
409 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
412 * View a blob as inline, embedded on the page.
415 * @param hb OPTIONAL base commit
417 elseif ($action === 'viewblob') {
419 $page['project'] = validate_project($_REQUEST['p']);
420 $page['hash'] = validate_hash($_REQUEST['h']);
421 $page['title'] = "$page[project] - Blob - ViewGit";
422 if (isset($_REQUEST['hb'])) {
423 $page['commit_id'] = validate_hash($_REQUEST['hb']);
426 $page['commit_id'] = 'HEAD';
430 if (isset($_REQUEST['f'])) {
431 $page['path'] = $_REQUEST['f']; // TODO validate?
434 // For the header's pagenav
435 $info = git_get_commit_info($page['project'], $page['commit_id']);
436 $page['commit_id'] = $info['h'];
437 $page['tree_id'] = $info['tree'];
439 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
441 $page['data'] = join("\n", run_git($page['project'], "cat-file blob $page[hash]"));
444 if ($conf['geshi'] && strpos($page['path'], '.')) {
445 $old_mask = error_reporting(E_ALL ^ E_NOTICE
);
446 require_once($conf['geshi_path']);
447 $ext = array_pop(explode('.', $page['path']));
448 $lang = Geshi
::get_language_name_from_extension($ext);
449 if (strlen($lang) > 0) {
450 $geshi =& new Geshi($page['data'], $lang);
451 if (is_int($conf['geshi_line_numbers'])) {
452 if ($conf['geshi_line_numbers'] == 0) {
453 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS
);
456 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS
, $conf['geshi_line_numbers']);
459 $page['html_data'] = $geshi->parse_code();
461 error_reporting($old_mask);
465 die('Invalid action');
468 require 'templates/header.php';
469 require "templates/$template.php";
470 require 'templates/footer.php';