tree: added a "browse at HEAD" link.
[viewgit.git] / index.php
blobe2dab2a55d6d5c4fa0ecfa8d05c2392e6622e6e4
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);
12 require_once('inc/config.php');
13 require_once('inc/functions.php');
15 if (isset($conf['projects_glob'])) {
16 foreach ($conf['projects_glob'] as $glob) {
17 foreach (glob($glob) as $path) {
18 // Get the last part of the path before .git
19 $name = preg_replace(array('#/?\.git$#', '#^.*/#'), array('', ''), $path);
21 // Workaround against name collisions; proj, proj1, proj2, ...
22 $i = '';
23 while (in_array($name . $i, array_keys($conf['projects']))) {
24 @$i++;
26 $name = $name . $i;
27 $conf['projects'][$name] = array('repo' => $path);
32 $action = 'index';
33 $template = 'index';
34 $page['title'] = 'ViewGit';
36 if (isset($_REQUEST['a'])) {
37 $action = strtolower($_REQUEST['a']);
39 $page['action'] = $action;
41 if ($action === 'index') {
42 $template = 'index';
43 $page['title'] = 'List of projects - ViewGit';
45 foreach (array_keys($conf['projects']) as $p) {
46 $page['projects'][] = get_project_info($p);
49 elseif ($action === 'archive') {
50 $project = validate_project($_REQUEST['p']);
51 $tree = validate_hash($_REQUEST['h']);
52 $type = $_REQUEST['t'];
54 $basename = "$project-tree-". substr($tree, 0, 7);
55 if (isset($_REQUEST['n'])) {
56 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
59 if ($type === 'targz') {
60 header("Content-Type: application/x-tar-gz");
61 header("Content-Transfer-Encoding: binary");
62 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
63 run_git_passthru($project, "git archive --format=tar $tree |gzip");
65 elseif ($type === 'zip') {
66 header("Content-Type: application/x-zip");
67 header("Content-Transfer-Encoding: binary");
68 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
69 run_git_passthru($project, "git archive --format=zip $tree");
71 else {
72 die('Invalid archive type requested');
75 die();
77 // blob: send a blob to browser with filename suggestion
78 elseif ($action === 'blob') {
79 $project = validate_project($_REQUEST['p']);
80 $hash = validate_hash($_REQUEST['h']);
81 $name = $_REQUEST['n'];
83 header('Content-type: application/octet-stream');
84 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
86 run_git_passthru($project, "git cat-file blob $hash");
87 die();
89 /**
90 * git checkout.
92 elseif ($action === 'co') {
93 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
95 // For debugging
96 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
98 // eg. info/refs, HEAD
99 $p = validate_project($_REQUEST['p']); // project
100 $r = $_REQUEST['r']; // path
102 $gitdir = $conf['projects'][$p]['repo'];
103 $filename = $gitdir .'/'. $r;
105 // make sure the request is legit (no reading of other files besides those under git projects)
106 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) {
107 if (file_exists($filename)) {
108 debug('OK, sending');
109 readfile($filename);
110 } else {
111 debug('Not found');
112 header('404');
114 } else {
115 debug("Denied");
118 die();
120 elseif ($action === 'commit') {
121 $template = 'commit';
122 $page['project'] = validate_project($_REQUEST['p']);
123 $page['title'] = "$page[project] - Commit - ViewGit";
124 $page['commit_id'] = validate_hash($_REQUEST['h']);
126 $info = git_get_commit_info($page['project'], $page['commit_id']);
128 $page['author_name'] = $info['author_name'];
129 $page['author_mail'] = $info['author_mail'];
130 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
131 $page['author_datetime_local'] = strftime($conf['datetime'], $info['author_stamp']) .' '. $info['author_timezone'];
132 $page['committer_name'] = $info['committer_name'];
133 $page['committer_mail'] = $info['committer_mail'];
134 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
135 $page['committer_datetime_local'] = strftime($conf['datetime'], $info['committer_stamp']) .' '. $info['committer_timezone'];
136 $page['tree_id'] = $info['tree'];
137 $page['parents'] = $info['parents'];
138 $page['message'] = $info['message'];
139 $page['message_firstline'] = $info['message_firstline'];
140 $page['message_full'] = $info['message_full'];
143 elseif ($action === 'commitdiff') {
144 $template = 'commitdiff';
145 $page['project'] = validate_project($_REQUEST['p']);
146 $page['title'] = "$page[project] - Commitdiff - ViewGit";
147 $hash = validate_hash($_REQUEST['h']);
148 $page['commit_id'] = $hash;
150 $info = git_get_commit_info($page['project'], $hash);
152 $page['tree_id'] = $info['tree'];
154 $page['message'] = $info['message'];
155 $page['message_firstline'] = $info['message_firstline'];
156 $page['message_full'] = $info['message_full'];
157 $page['author_name'] = $info['author_name'];
158 $page['author_mail'] = $info['author_mail'];
159 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
161 $text = join("\n", run_git($page['project'], "git diff $hash^..$hash"));
162 list($page['files'], $page['diffdata']) = format_diff($text);
163 //$page['diffdata'] = format_diff($text);
165 elseif ($action === 'rss-log') {
166 $page['project'] = validate_project($_REQUEST['p']);
168 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
170 $page['rss_title'] = "Log for $page[project]";
171 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
172 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
173 $page['rss_pubDate'] = rss_pubdate(time());
174 $page['rss_ttl'] = $conf['rss_ttl'];
176 $page['rss_items'] = array();
178 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
180 $revs = git_get_rev_list($page['project'], $conf['rss_max_items']);
181 foreach ($revs as $rev) {
182 $info = git_get_commit_info($page['project'], $rev);
183 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
184 if ($diffstat) {
185 $info['diffstat'] = git_diffstat($page['project'], $rev);
188 $page['rss_items'][] = array(
189 'title' => rss_item_format($conf['rss_item_title'], $info),
190 'guid' => $link,
191 'link' => $link,
192 'description' => rss_item_format($conf['rss_item_description'], $info),
193 'pubdate' => rss_pubdate($info['author_stamp']),
197 require('templates/rss.php');
198 die();
200 elseif ($action === 'shortlog') {
201 $template = 'shortlog';
202 $page['project'] = validate_project($_REQUEST['p']);
203 $page['title'] = "$page[project] - Shortlog - ViewGit";
204 if (isset($_REQUEST['h'])) {
205 $page['ref'] = validate_hash($_REQUEST['h']);
206 } else {
207 $page['ref'] = 'HEAD';
210 $info = git_get_commit_info($page['project'], $page['ref']);
211 $page['commit_id'] = $info['h'];
212 $page['tree_id'] = $info['tree'];
214 // TODO merge the logic with 'summary' below
215 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog'], $page['ref']); // TODO pass first rev as parameter
216 foreach ($revs as $rev) {
217 $info = git_get_commit_info($page['project'], $rev);
218 $page['shortlog'][] = array(
219 'author' => $info['author_name'],
220 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
221 'message' => $info['message'],
222 'commit_id' => $rev,
223 'tree' => $info['tree'],
227 elseif ($action === 'summary') {
228 $template = 'summary';
229 $page['project'] = validate_project($_REQUEST['p']);
230 $page['title'] = "$page[project] - Summary - ViewGit";
232 $info = git_get_commit_info($page['project']);
233 $page['commit_id'] = $info['h'];
234 $page['tree_id'] = $info['tree'];
236 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
237 foreach ($revs as $rev) {
238 $info = git_get_commit_info($page['project'], $rev);
239 $page['shortlog'][] = array(
240 'author' => $info['author_name'],
241 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
242 'message' => $info['message'],
243 'commit_id' => $rev,
244 'tree' => $info['tree'],
248 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
250 $heads = git_get_heads($page['project']);
251 $page['heads'] = array();
252 foreach ($heads as $h) {
253 $info = git_get_commit_info($page['project'], $h['h']);
254 $page['heads'][] = array(
255 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
256 'h' => $h['h'],
257 'fullname' => $h['fullname'],
258 'name' => $h['name'],
262 elseif ($action === 'tags') {
263 $template = 'tags';
264 $page['project'] = validate_project($_REQUEST['p']);
265 $page['title'] = "$page[project] - Tags - ViewGit";
267 $info = git_get_commit_info($page['project']);
268 $page['commit_id'] = $info['h'];
269 $page['tree_id'] = $info['tree'];
271 $page['tags'] = handle_tags($page['project']);
274 * Shows a tree, with list of directories/files, links to them and download
275 * links to archives.
277 * @param p project
278 * @param h tree hash
279 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
280 * one denotes which commit the user navigated from)
281 * @param f OPTIONAL path the user has followed to view this tree
283 elseif ($action === 'tree') {
284 $template = 'tree';
285 $page['project'] = validate_project($_REQUEST['p']);
286 if (isset($_REQUEST['h'])) {
287 $page['tree_id'] = validate_hash($_REQUEST['h']);
290 else {
291 // TODO walk the tree
292 $page['tree_id'] = 'HEAD';
295 $page['title'] = "$page[project] - Tree - ViewGit";
297 // 'hb' optionally contains the commit_id this tree is related to
298 if (isset($_REQUEST['hb'])) {
299 $page['commit_id'] = validate_hash($_REQUEST['hb']);
301 else {
302 // for the header
303 $info = git_get_commit_info($page['project']);
304 $page['commit_id'] = $info['h'];
307 $page['path'] = '';
308 if (isset($_REQUEST['f'])) {
309 $page['path'] = $_REQUEST['f']; // TODO validate?
312 // get path info for the header
313 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
314 if (!isset($page['tree_id'])) {
315 // Take the last hash from the tree
316 if (count($page['pathinfo']) > 0) {
317 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
318 } else {
319 $page['tree_id'] = 'HEAD';
323 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
326 * View a blob as inline, embedded on the page.
327 * @param p project
328 * @param h blob hash
329 * @param hb OPTIONAL base commit
331 elseif ($action === 'viewblob') {
332 $template = 'blob';
333 $page['project'] = validate_project($_REQUEST['p']);
334 $page['hash'] = validate_hash($_REQUEST['h']);
335 $page['title'] = "$page[project] - Blob - ViewGit";
336 if (isset($_REQUEST['hb'])) {
337 $page['commit_id'] = validate_hash($_REQUEST['hb']);
339 else {
340 $page['commit_id'] = 'HEAD';
343 $page['path'] = '';
344 if (isset($_REQUEST['f'])) {
345 $page['path'] = $_REQUEST['f']; // TODO validate?
348 // For the header's pagenav
349 $info = git_get_commit_info($page['project'], $page['commit_id']);
350 $page['commit_id'] = $info['h'];
351 $page['tree_id'] = $info['tree'];
353 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
355 $page['data'] = join("\n", run_git($page['project'], "git cat-file blob $page[hash]"));
357 // GeSHi support
358 if ($conf['geshi']) {
359 require_once($conf['geshi_path']);
360 $ext = array_pop(explode('.', $page['path']));
361 $lang = Geshi::get_language_name_from_extension($ext);
362 $geshi =& new Geshi($page['data'], $lang);
363 $page['html_data'] = $geshi->parse_code();
366 else {
367 die('Invalid action');
370 require 'templates/header.php';
371 require "templates/$template.php";
372 require 'templates/footer.php';