2 error_reporting(E_ALL
);
4 require_once('inc/config.php');
7 * Formats "git diff" output into xhtml.
8 * @return array(array of filenames, xhtml)
10 function format_diff($text)
14 // match every "^diff --git a/<path> b/<path>$" line
15 foreach (explode("\n", $text) as $line) {
16 if (preg_match('#^diff --git a/(.*) b/(.*)$#', $line, $matches) > 0) {
17 $files[$matches[1]] = urlencode($matches[1]);
21 $text = htmlentities($text);
31 '<span class="add">$1</span>',
32 '<span class="del">$1</span>',
33 '<span class="pos">$1</span>',
34 '<span class="etc">$1</span>',
37 $text = preg_replace_callback('#^diff --git a/(.*) b/(.*)$#m',
40 'return "<span class=\"diffline\"><a name=\"". urlencode($m[1]) ."\">diff --git a/$m[1] b/$m[2]</a></span>";'
44 return array($files, $text);
47 function get_project_info($name)
51 $info = $conf['projects'][$name];
52 $info['name'] = $name;
53 $info['description'] = file_get_contents($info['repo'] .'/description');
55 $headinfo = git_get_commit_info($name, 'HEAD');
56 $info['head_stamp'] = $headinfo['author_utcstamp'];
57 $info['head_datetime'] = strftime($conf['datetime'], $headinfo['author_utcstamp']);
58 $info['head_hash'] = $headinfo['h'];
59 $info['head_tree'] = $headinfo['tree'];
65 * Get details of a commit: tree, parent, author/committer (name, mail, date), message
67 function git_get_commit_info($project, $hash = 'HEAD')
73 $info['message_full'] = '';
75 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
78 // author <name> "<"<mail>">" <stamp> <timezone>
82 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
83 foreach ($output as $line) {
84 if (substr($line, 0, 4) === 'tree') {
85 $info['tree'] = substr($line, 5);
87 elseif (substr($line, 0, 6) === 'parent') {
88 $info['parent'] = substr($line, 7);
90 elseif (preg_match($pattern, $line, $matches) > 0) {
91 $info[$matches[1] .'_name'] = $matches[2];
92 $info[$matches[1] .'_mail'] = $matches[3];
93 $info[$matches[1] .'_stamp'] = $matches[4];
94 $info[$matches[1] .'_timezone'] = $matches[5];
95 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
97 elseif (substr($line, 0, 4) === ' ') {
98 $info['message_full'] .= substr($line, 4) ."\n";
99 if (!isset($info['message'])) {
100 $info['message'] = substr($line, 4, $conf['commit_message_maxlen']);
101 $info['message_firstline'] = substr($line, 4);
104 elseif (preg_match('/^[0-9a-f]{40}$/', $line) > 0) {
112 function git_get_heads($project)
116 $output = run_git($project, 'git show-ref --heads');
117 foreach ($output as $line) {
118 $fullname = substr($line, 41);
119 $name = array_pop(explode('/', $fullname));
120 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
126 function git_get_rev_list($project, $max_count = null, $start = 'HEAD')
128 $cmd = "git rev-list $start";
129 if (!is_null($max_count)) {
130 $cmd = "git rev-list --max-count=$max_count $start";
133 return run_git($project, $cmd);
136 function git_get_tags($project)
140 $output = run_git($project, 'git show-ref --tags');
141 foreach ($output as $line) {
142 $fullname = substr($line, 41);
143 $name = array_pop(explode('/', $fullname));
144 $tags[] = array('h' => substr($line, 0, 40), 'fullname' => $fullname, 'name' => $name);
149 function git_ls_tree($project, $tree)
152 $output = run_git($project, "git ls-tree $tree");
153 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
154 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
155 foreach ($output as $line) {
156 $parts = preg_split('/\s+/', $line, 4);
157 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
163 function makelink($dict)
166 foreach ($dict as $k => $v) {
167 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
169 if (count($params) > 0) {
170 return '?'. htmlentities(join('&', $params));
176 * Executes a git command in the project repo.
177 * @return array of output lines
179 function run_git($project, $command)
184 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
185 exec($cmd, &$output);
190 * Executes a git command in the project repo, sending output directly to the
193 function run_git_passthru($project, $command)
197 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
199 passthru($cmd, &$result);
204 * Makes sure the given project is valid. If it's not, this function will
206 * @return the project
208 function validate_project($project)
212 if (!in_array($project, array_keys($conf['projects']))) {
213 die('Invalid project');
219 * Makes sure the given hash is valid. If it's not, this function will die().
222 function validate_hash($hash)
224 if (strlen($hash) != 40 ||
!preg_match('/^[0-9a-z]*$/', $hash)) {
233 $page['title'] = 'ViewGit';
235 if (isset($_REQUEST['a'])) {
236 $action = strtolower($_REQUEST['a']);
238 $page['action'] = $action;
240 if ($action === 'index') {
242 $page['title'] = 'List of projects - ViewGit';
244 foreach (array_keys($conf['projects']) as $p) {
245 $page['projects'][] = get_project_info($p);
248 elseif ($action === 'archive') {
249 $project = validate_project($_REQUEST['p']);
250 $tree = validate_hash($_REQUEST['h']);
251 $type = $_REQUEST['t'];
253 $basename = "$project-tree-$tree";
254 if (isset($_REQUEST['n'])) {
255 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
258 if ($type === 'targz') {
259 header("Content-Type: application/x-tar-gz");
260 header("Content-Transfer-Encoding: binary");
261 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
262 run_git_passthru($project, "git archive --format=tar $tree |gzip");
264 elseif ($type === 'zip') {
265 header("Content-Type: application/x-zip");
266 header("Content-Transfer-Encoding: binary");
267 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
268 run_git_passthru($project, "git archive --format=zip $tree");
271 die('Invalid archive type requested');
276 // blob: send a blob to browser with filename suggestion
277 elseif ($action === 'blob') {
278 $project = validate_project($_REQUEST['p']);
279 $hash = validate_hash($_REQUEST['h']);
280 $name = $_REQUEST['n'];
282 header('Content-type: application/octet-stream');
283 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
285 run_git_passthru($project, "git cat-file blob $hash");
288 elseif ($action === 'commit') {
289 $template = 'commit';
290 $page['project'] = validate_project($_REQUEST['p']);
291 $page['title'] = "$page[project] - Commit - ViewGit";
292 $page['commit_id'] = validate_hash($_REQUEST['h']);
294 $info = git_get_commit_info($page['project'], $page['commit_id']);
296 $page['author_name'] = $info['author_name'];
297 $page['author_mail'] = $info['author_mail'];
298 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
299 $page['author_datetime_local'] = strftime($conf['datetime'], $info['author_stamp']) .' '. $info['author_timezone'];
300 $page['committer_name'] = $info['committer_name'];
301 $page['committer_mail'] = $info['committer_mail'];
302 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
303 $page['committer_datetime_local'] = strftime($conf['datetime'], $info['committer_stamp']) .' '. $info['committer_timezone'];
304 $page['tree_id'] = $info['tree'];
305 $page['parent'] = $info['parent'];
306 $page['message'] = $info['message'];
307 $page['message_firstline'] = $info['message_firstline'];
308 $page['message_full'] = $info['message_full'];
311 elseif ($action === 'commitdiff') {
312 $template = 'commitdiff';
313 $page['project'] = validate_project($_REQUEST['p']);
314 $page['title'] = "$page[project] - Commitdiff - ViewGit";
315 $hash = validate_hash($_REQUEST['h']);
316 $page['commit_id'] = $hash;
318 $info = git_get_commit_info($page['project'], $hash);
320 $page['tree_id'] = $info['tree'];
322 $page['message'] = $info['message'];
323 $page['message_firstline'] = $info['message_firstline'];
324 $page['message_full'] = $info['message_full'];
325 $page['author_name'] = $info['author_name'];
326 $page['author_mail'] = $info['author_mail'];
327 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
329 $text = join("\n", run_git($page['project'], "git diff $hash^..$hash"));
330 list($page['files'], $page['diffdata']) = format_diff($text);
331 //$page['diffdata'] = format_diff($text);
333 elseif ($action === 'shortlog') {
334 $template = 'shortlog';
335 $page['project'] = validate_project($_REQUEST['p']);
336 $page['title'] = "$page[project] - Shortlog - ViewGit";
337 if (isset($_REQUEST['h'])) {
338 $page['ref'] = validate_hash($_REQUEST['h']);
340 $page['ref'] = 'HEAD';
343 $info = git_get_commit_info($page['project']);
344 $page['commit_id'] = $info['h'];
345 $page['tree_id'] = $info['tree'];
347 // TODO merge the logic with 'summary' below
348 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog'], $page['ref']); // TODO pass first rev as parameter
349 foreach ($revs as $rev) {
350 $info = git_get_commit_info($page['project'], $rev);
351 $page['shortlog'][] = array(
352 'author' => $info['author_name'],
353 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
354 'message' => $info['message'],
356 'tree' => $info['tree'],
360 elseif ($action === 'summary') {
361 $template = 'summary';
362 $page['project'] = validate_project($_REQUEST['p']);
363 $page['title'] = "$page[project] - Summary - ViewGit";
365 $info = git_get_commit_info($page['project']);
366 $page['commit_id'] = $info['h'];
367 $page['tree_id'] = $info['tree'];
369 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
370 foreach ($revs as $rev) {
371 $info = git_get_commit_info($page['project'], $rev);
372 $page['shortlog'][] = array(
373 'author' => $info['author_name'],
374 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
375 'message' => $info['message'],
377 'tree' => $info['tree'],
381 $tags = git_get_tags($page['project']);
382 $page['tags'] = array();
383 foreach ($tags as $tag) {
384 $info = git_get_commit_info($page['project'], $tag['h']);
385 $page['tags'][] = array(
386 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
388 'fullname' => $tag['fullname'],
389 'name' => $tag['name'],
393 $heads = git_get_heads($page['project']);
394 $page['heads'] = array();
395 foreach ($heads as $h) {
396 $info = git_get_commit_info($page['project'], $h['h']);
397 $page['heads'][] = array(
398 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
400 'fullname' => $h['fullname'],
401 'name' => $h['name'],
406 * Shows a tree, with list of directories/files, links to them and download
411 * @param hb OPTIONAL base commit (trees can be part of multiple commits, this
412 * one denotes which commit the user navigated from)
414 elseif ($action === 'tree') {
416 $page['project'] = validate_project($_REQUEST['p']);
417 $page['tree_id'] = validate_hash($_REQUEST['h']);
418 $page['title'] = "$page[project] - Tree - ViewGit";
420 // 'hb' optionally contains the commit_id this tree is related to
421 if (isset($_REQUEST['hb'])) {
422 $page['commit_id'] = validate_hash($_REQUEST['hb']);
426 $info = git_get_commit_info($page['project']);
427 $page['commit_id'] = $info['h'];
431 if (isset($_REQUEST['f'])) {
432 $page['path'] = $_REQUEST['f']; // TODO validate?
435 $page['entries'] = git_ls_tree($page['project'], $page['tree_id']);
438 * View a blob as inline, embedded on the page.
441 * @param hb OPTIONAL base commit
443 elseif ($action === 'viewblob') {
445 $page['project'] = validate_project($_REQUEST['p']);
446 $page['hash'] = validate_hash($_REQUEST['h']);
447 $page['title'] = "$page[project] - Blob - ViewGit";
448 if (isset($_REQUEST['hb'])) {
449 $page['commit_id'] = validate_hash($_REQUEST['hb']);
452 $page['commit_id'] = 'HEAD';
456 if (isset($_REQUEST['f'])) {
457 $page['path'] = $_REQUEST['f']; // TODO validate?
460 // For the header's pagenav
461 $info = git_get_commit_info($page['project'], $page['commit_id']);
462 $page['commit_id'] = $info['h'];
463 $page['tree_id'] = $info['tree'];
465 $page['data'] = join("\n", run_git($page['project'], "git cat-file blob $page[hash]"));
468 die('Invalid action');
471 require 'templates/header.php';
472 require "templates/$template.php";
473 require 'templates/footer.php';