Show ref labels in shortlog.
[viewgit.git] / index.php
blob2b87baa40edfb2806795851e209b29ef15002a37
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;
42 * index - list of projects
44 if ($action === 'index') {
45 $template = 'index';
46 $page['title'] = 'List of projects - ViewGit';
48 foreach (array_keys($conf['projects']) as $p) {
49 $page['projects'][] = get_project_info($p);
54 * archive - send a tree as an archive to client
55 * @param p project
56 * @param h tree hash
57 * @param t type, "targz" or "zip"
58 * @param n OPTIONAL name suggestion
60 elseif ($action === 'archive') {
61 $project = validate_project($_REQUEST['p']);
62 $tree = validate_hash($_REQUEST['h']);
63 $type = $_REQUEST['t'];
65 $basename = "$project-tree-". substr($tree, 0, 7);
66 if (isset($_REQUEST['n'])) {
67 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
70 if ($type === 'targz') {
71 header("Content-Type: application/x-tar-gz");
72 header("Content-Transfer-Encoding: binary");
73 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
74 run_git_passthru($project, "git archive --format=tar $tree |gzip");
76 elseif ($type === 'zip') {
77 header("Content-Type: application/x-zip");
78 header("Content-Transfer-Encoding: binary");
79 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
80 run_git_passthru($project, "git archive --format=zip $tree");
82 else {
83 die('Invalid archive type requested');
86 die();
90 * blob - send a blob to browser with filename suggestion
91 * @param p project
92 * @param h blob hash
93 * @param n filename
95 elseif ($action === 'blob') {
96 $project = validate_project($_REQUEST['p']);
97 $hash = validate_hash($_REQUEST['h']);
98 $name = $_REQUEST['n'];
100 header('Content-type: application/octet-stream');
101 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
103 run_git_passthru($project, "git cat-file blob $hash");
104 die();
108 * co - git checkout. These requests come from mod_rewrite, see the .htaccess file.
109 * @param p project
110 * @param r path
112 elseif ($action === 'co') {
113 if (!$conf['allow_checkout']) { die('Checkout not allowed'); }
115 // For debugging
116 debug("Project: $_REQUEST[p] Request: $_REQUEST[r]");
118 // eg. info/refs, HEAD
119 $p = validate_project($_REQUEST['p']); // project
120 $r = $_REQUEST['r']; // path
122 $gitdir = $conf['projects'][$p]['repo'];
123 $filename = $gitdir .'/'. $r;
125 // make sure the request is legit (no reading of other files besides those under git projects)
126 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) {
127 if (file_exists($filename)) {
128 debug('OK, sending');
129 readfile($filename);
130 } else {
131 debug('Not found');
132 header('HTTP/1.0 404 Not Found');
134 } else {
135 debug("Denied");
138 die();
142 * commit - view commit information
143 * @param p project
144 * @param h commit hash
146 elseif ($action === 'commit') {
147 $template = 'commit';
148 $page['project'] = validate_project($_REQUEST['p']);
149 $page['title'] = "$page[project] - Commit - ViewGit";
150 $page['commit_id'] = validate_hash($_REQUEST['h']);
152 $info = git_get_commit_info($page['project'], $page['commit_id']);
154 $page['author_name'] = $info['author_name'];
155 $page['author_mail'] = $info['author_mail'];
156 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
157 $page['author_datetime_local'] = strftime($conf['datetime'], $info['author_stamp']) .' '. $info['author_timezone'];
158 $page['committer_name'] = $info['committer_name'];
159 $page['committer_mail'] = $info['committer_mail'];
160 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
161 $page['committer_datetime_local'] = strftime($conf['datetime'], $info['committer_stamp']) .' '. $info['committer_timezone'];
162 $page['tree_id'] = $info['tree'];
163 $page['parents'] = $info['parents'];
164 $page['message'] = $info['message'];
165 $page['message_firstline'] = $info['message_firstline'];
166 $page['message_full'] = $info['message_full'];
171 * commitdiff - view diff of a commit
172 * @param p project
173 * @param h commit hash
175 elseif ($action === 'commitdiff') {
176 $template = 'commitdiff';
177 $page['project'] = validate_project($_REQUEST['p']);
178 $page['title'] = "$page[project] - Commitdiff - ViewGit";
179 $hash = validate_hash($_REQUEST['h']);
180 $page['commit_id'] = $hash;
182 $info = git_get_commit_info($page['project'], $hash);
184 $page['tree_id'] = $info['tree'];
186 $page['message'] = $info['message'];
187 $page['message_firstline'] = $info['message_firstline'];
188 $page['message_full'] = $info['message_full'];
189 $page['author_name'] = $info['author_name'];
190 $page['author_mail'] = $info['author_mail'];
191 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
193 $text = join("\n", run_git($page['project'], "git diff $hash^..$hash"));
194 list($page['files'], $page['diffdata']) = format_diff($text);
195 //$page['diffdata'] = format_diff($text);
199 * rss-log - RSS feed of project changes
200 * @param p project
202 elseif ($action === 'rss-log') {
203 $page['project'] = validate_project($_REQUEST['p']);
205 $ext_url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) .'/';
207 $page['rss_title'] = "Log for $page[project]";
208 $page['rss_link'] = $ext_url . makelink(array('a' => 'summary', 'p' => $page['project']));
209 $page['rss_description'] = "Git log for project $page[project], generated by ViewGit.";
210 $page['rss_pubDate'] = rss_pubdate(time());
211 $page['rss_ttl'] = $conf['rss_ttl'];
213 $page['rss_items'] = array();
215 $diffstat = strstr($conf['rss_item_description'], '{DIFFSTAT}');
217 $revs = git_get_rev_list($page['project'], $conf['rss_max_items']);
218 foreach ($revs as $rev) {
219 $info = git_get_commit_info($page['project'], $rev);
220 $link = $ext_url . makelink(array('a' => 'commit', 'p' => $page['project'], 'h' => $rev));
221 if ($diffstat) {
222 $info['diffstat'] = git_diffstat($page['project'], $rev);
225 $page['rss_items'][] = array(
226 'title' => rss_item_format($conf['rss_item_title'], $info),
227 'guid' => $link,
228 'link' => $link,
229 'description' => rss_item_format($conf['rss_item_description'], $info),
230 'pubdate' => rss_pubdate($info['author_stamp']),
234 require('templates/rss.php');
235 die();
239 * shortlog - project shortlog entries
240 * @param p project
241 * @param h OPTIONAL commit id to start showing log from
243 elseif ($action === 'shortlog') {
244 $template = 'shortlog';
245 $page['project'] = validate_project($_REQUEST['p']);
246 $page['title'] = "$page[project] - Shortlog - ViewGit";
247 if (isset($_REQUEST['h'])) {
248 $page['ref'] = validate_hash($_REQUEST['h']);
249 } else {
250 $page['ref'] = 'HEAD';
253 $info = git_get_commit_info($page['project'], $page['ref']);
254 $page['commit_id'] = $info['h'];
255 $page['tree_id'] = $info['tree'];
257 $page['shortlog'] = handle_shortlog($page['project'], $page['ref']);
259 elseif ($action === 'summary') {
260 $template = 'summary';
261 $page['project'] = validate_project($_REQUEST['p']);
262 $page['title'] = "$page[project] - Summary - ViewGit";
264 $info = git_get_commit_info($page['project']);
265 $page['commit_id'] = $info['h'];
266 $page['tree_id'] = $info['tree'];
268 $page['shortlog'] = handle_shortlog($page['project']);
270 $page['tags'] = handle_tags($page['project'], $conf['summary_tags']);
272 $heads = git_get_heads($page['project']);
273 $page['heads'] = array();
274 foreach ($heads as $h) {
275 $info = git_get_commit_info($page['project'], $h['h']);
276 $page['heads'][] = array(
277 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
278 'h' => $h['h'],
279 'fullname' => $h['fullname'],
280 'name' => $h['name'],
284 elseif ($action === 'tags') {
285 $template = 'tags';
286 $page['project'] = validate_project($_REQUEST['p']);
287 $page['title'] = "$page[project] - Tags - ViewGit";
289 $info = git_get_commit_info($page['project']);
290 $page['commit_id'] = $info['h'];
291 $page['tree_id'] = $info['tree'];
293 $page['tags'] = handle_tags($page['project']);
296 * Shows a tree, with list of directories/files, links to them and download
297 * links to archives.
299 * @param p project
300 * @param h tree hash
301 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
302 * one denotes which commit the user navigated from)
303 * @param f OPTIONAL path the user has followed to view this tree
305 elseif ($action === 'tree') {
306 $template = 'tree';
307 $page['project'] = validate_project($_REQUEST['p']);
308 if (isset($_REQUEST['h'])) {
309 $page['tree_id'] = validate_hash($_REQUEST['h']);
312 else {
313 // TODO walk the tree
314 $page['tree_id'] = 'HEAD';
317 $page['title'] = "$page[project] - Tree - ViewGit";
319 // 'hb' optionally contains the commit_id this tree is related to
320 if (isset($_REQUEST['hb'])) {
321 $page['commit_id'] = validate_hash($_REQUEST['hb']);
323 else {
324 // for the header
325 $info = git_get_commit_info($page['project']);
326 $page['commit_id'] = $info['h'];
329 $page['path'] = '';
330 if (isset($_REQUEST['f'])) {
331 $page['path'] = $_REQUEST['f']; // TODO validate?
334 // get path info for the header
335 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
336 if (!isset($page['tree_id'])) {
337 // Take the last hash from the tree
338 if (count($page['pathinfo']) > 0) {
339 $page['tree_id'] = $page['pathinfo'][count($page['pathinfo']) - 1]['hash'];
340 } else {
341 $page['tree_id'] = 'HEAD';
345 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
348 * View a blob as inline, embedded on the page.
349 * @param p project
350 * @param h blob hash
351 * @param hb OPTIONAL base commit
353 elseif ($action === 'viewblob') {
354 $template = 'blob';
355 $page['project'] = validate_project($_REQUEST['p']);
356 $page['hash'] = validate_hash($_REQUEST['h']);
357 $page['title'] = "$page[project] - Blob - ViewGit";
358 if (isset($_REQUEST['hb'])) {
359 $page['commit_id'] = validate_hash($_REQUEST['hb']);
361 else {
362 $page['commit_id'] = 'HEAD';
365 $page['path'] = '';
366 if (isset($_REQUEST['f'])) {
367 $page['path'] = $_REQUEST['f']; // TODO validate?
370 // For the header's pagenav
371 $info = git_get_commit_info($page['project'], $page['commit_id']);
372 $page['commit_id'] = $info['h'];
373 $page['tree_id'] = $info['tree'];
375 $page['pathinfo'] = git_get_path_info($page['project'], $page['commit_id'], $page['path']);
377 $page['data'] = join("\n", run_git($page['project'], "git cat-file blob $page[hash]"));
379 // GeSHi support
380 if ($conf['geshi'] && strpos($page['path'], '.')) {
381 error_reporting(E_ALL ^ E_NOTICE);
382 require_once($conf['geshi_path']);
383 $ext = array_pop(explode('.', $page['path']));
384 $lang = Geshi::get_language_name_from_extension($ext);
385 $geshi =& new Geshi($page['data'], $lang);
386 if (is_int($conf['geshi_line_numbers'])) {
387 if ($conf['geshi_line_numbers'] == 0) {
388 $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
390 else {
391 $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, $conf['geshi_line_numbers']);
394 $page['html_data'] = $geshi->parse_code();
395 error_reporting(E_ALL);
398 else {
399 die('Invalid action');
402 require 'templates/header.php';
403 require "templates/$template.php";
404 require 'templates/footer.php';