2 error_reporting(E_ALL
);
4 require_once('inc/config.php');
7 * Formats "git diff" output into xhtml.
9 function format_diff($text)
11 $text = htmlentities($text);
21 '<span class="add">$1</span>',
22 '<span class="del">$1</span>',
23 '<span class="pos">$1</span>',
24 '<span class="etc">$1</span>',
31 function get_project_info($name)
35 $info = $conf['projects'][$name];
36 $info['name'] = $name;
37 $info['description'] = file_get_contents($info['repo'] .'/description');
39 $headinfo = git_get_commit_info($name, 'HEAD');
40 $info['head_stamp'] = $headinfo['author_utcstamp'];
41 $info['head_datetime'] = strftime($conf['datetime'], $headinfo['author_utcstamp']);
42 $info['head_hash'] = $headinfo['h'];
43 $info['head_tree'] = $headinfo['tree'];
49 * Get details of a commit: tree, parent, author/committer (name, mail, date), message
51 function git_get_commit_info($project, $hash)
55 $info['message_full'] = '';
57 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
60 // author <name> "<"<mail>">" <stamp> <timezone>
64 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
65 foreach ($output as $line) {
66 if (substr($line, 0, 4) === 'tree') {
67 $info['tree'] = substr($line, 5);
69 elseif (substr($line, 0, 6) === 'parent') {
70 $info['parent'] = substr($line, 7);
72 elseif (preg_match($pattern, $line, $matches) > 0) {
73 $info[$matches[1] .'_name'] = $matches[2];
74 $info[$matches[1] .'_mail'] = $matches[3];
75 $info[$matches[1] .'_stamp'] = $matches[4];
76 $info[$matches[1] .'_timezone'] = $matches[5];
77 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
79 elseif (substr($line, 0, 4) === ' ') {
80 $info['message_full'] .= substr($line, 4) ."\n";
81 if (!isset($info['message'])) {
82 $info['message'] = substr($line, 4, 40);
90 function git_get_heads($project)
94 $output = run_git($project, 'git show-ref --heads');
95 foreach ($output as $line) {
96 $fullname = substr($line, 41);
97 $name = array_pop(explode('/', $fullname));
98 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
104 function git_get_rev_list($project, $max_count = null, $start = 'HEAD')
106 $cmd = "git rev-list $start";
107 if (!is_null($max_count)) {
108 $cmd = "git rev-list --max-count=$max_count $start";
111 return run_git($project, $cmd);
114 function git_get_tags($project)
118 $output = run_git($project, 'git show-ref --tags');
119 foreach ($output as $line) {
120 $fullname = substr($line, 41);
121 $name = array_pop(explode('/', $fullname));
122 $tags[] = array('h' => substr($line, 0, 40), 'fullname' => $fullname, 'name' => $name);
127 function git_ls_tree($project, $tree)
130 $output = run_git($project, "git ls-tree $tree");
131 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
132 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
133 foreach ($output as $line) {
134 $parts = preg_split('/\s+/', $line, 4);
135 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
141 function makelink($dict)
144 foreach ($dict as $k => $v) {
145 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
147 if (count($params) > 0) {
148 return '?'. htmlentities(join('&', $params));
154 * Executes a git command in the project repo.
155 * @return array of output lines
157 function run_git($project, $command)
162 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
163 exec($cmd, &$output);
168 * Executes a git command in the project repo, sending output directly to the
171 function run_git_passthru($project, $command)
175 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
177 passthru($cmd, &$result);
182 * Makes sure the given project is valid. If it's not, this function will
184 * @return the project
186 function validate_project($project)
190 if (!in_array($project, array_keys($conf['projects']))) {
191 die('Invalid project');
197 * Makes sure the given hash is valid. If it's not, this function will die().
200 function validate_hash($hash)
202 if (strlen($hash) != 40 ||
!preg_match('/^[0-9a-z]*$/', $hash)) {
211 $page['title'] = 'ViewGit';
213 if (isset($_REQUEST['a'])) {
214 $action = strtolower($_REQUEST['a']);
216 $page['action'] = $action;
218 if ($action === 'index') {
220 $page['title'] = 'List of projects - ViewGit';
222 foreach (array_keys($conf['projects']) as $p) {
223 $page['projects'][] = get_project_info($p);
226 elseif ($action === 'archive') {
227 $project = validate_project($_REQUEST['p']);
228 $tree = validate_hash($_REQUEST['h']);
229 $type = $_REQUEST['t'];
231 $basename = "$project-tree-$tree";
232 if (isset($_REQUEST['n'])) {
233 $basename = "$project-$_REQUEST[n]-". substr($tree, 0, 6);
236 if ($type === 'targz') {
237 header("Content-Type: application/x-tar-gz");
238 header("Content-Transfer-Encoding: binary");
239 header("Content-Disposition: attachment; filename=\"$basename.tar.gz\";");
240 run_git_passthru($project, "git archive --format=tar $tree |gzip");
242 elseif ($type === 'zip') {
243 header("Content-Type: application/x-zip");
244 header("Content-Transfer-Encoding: binary");
245 header("Content-Disposition: attachment; filename=\"$basename.zip\";");
246 run_git_passthru($project, "git archive --format=zip $tree");
249 die('Invalid archive type requested');
254 // blob: send a blob to browser with filename suggestion
255 elseif ($action === 'blob') {
256 $project = validate_project($_REQUEST['p']);
257 $hash = validate_hash($_REQUEST['h']);
258 $name = $_REQUEST['n'];
260 header('Content-type: application/octet-stream');
261 header("Content-Disposition: attachment; filename=$name"); // FIXME needs quotation
263 run_git_passthru($project, "git cat-file blob $hash");
266 elseif ($action === 'commit') {
267 $template = 'commit';
268 $page['project'] = validate_project($_REQUEST['p']);
269 $page['commit_id'] = validate_hash($_REQUEST['h']);
271 $info = git_get_commit_info($page['project'], $page['commit_id']);
273 $page['author_name'] = $info['author_name'];
274 $page['author_mail'] = $info['author_mail'];
275 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
276 $page['author_datetime_local'] = strftime($conf['datetime'], $info['author_stamp']) .' '. $info['author_timezone'];
277 $page['committer_name'] = $info['committer_name'];
278 $page['committer_mail'] = $info['committer_mail'];
279 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
280 $page['committer_datetime_local'] = strftime($conf['datetime'], $info['committer_stamp']) .' '. $info['committer_timezone'];
281 $page['tree'] = $info['tree'];
282 $page['parent'] = $info['parent'];
283 $page['message'] = $info['message'];
284 $page['message_full'] = $info['message_full'];
287 elseif ($action === 'commitdiff') {
288 $template = 'commitdiff';
289 $page['project'] = validate_project($_REQUEST['p']);
290 $hash = validate_hash($_REQUEST['h']);
291 $page['commit_id'] = $hash;
293 $info = git_get_commit_info($page['project'], $hash);
295 $page['message'] = $info['message'];
296 $page['message_full'] = $info['message_full'];
297 $page['author_name'] = $info['author_name'];
298 $page['author_mail'] = $info['author_mail'];
299 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
301 $text = join("\n", run_git($page['project'], "git diff $hash^..$hash"));
302 $page['diffdata'] = format_diff($text);
304 elseif ($action === 'shortlog') {
305 $template = 'shortlog';
306 $page['project'] = validate_project($_REQUEST['p']);
307 $page['ref'] = $_REQUEST['h']; // TODO validate
309 // TODO merge the logic with 'summary' below
310 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog'], $page['ref']); // TODO pass first rev as parameter
311 foreach ($revs as $rev) {
312 $info = git_get_commit_info($page['project'], $rev);
313 $page['shortlog'][] = array(
314 'author' => $info['author_name'],
315 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
316 'message' => $info['message'],
318 'tree' => $info['tree'],
322 elseif ($action === 'summary') {
323 $template = 'summary';
324 $page['project'] = validate_project($_REQUEST['p']);
325 $page['title'] = "$page[project] - Summary - ViewGit";
327 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
328 foreach ($revs as $rev) {
329 $info = git_get_commit_info($page['project'], $rev);
330 $page['shortlog'][] = array(
331 'author' => $info['author_name'],
332 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
333 'message' => $info['message'],
335 'tree' => $info['tree'],
339 $tags = git_get_tags($page['project']);
340 $page['tags'] = array();
341 foreach ($tags as $tag) {
342 $info = git_get_commit_info($page['project'], $tag['h']);
343 $page['tags'][] = array(
344 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
346 'fullname' => $tag['fullname'],
347 'name' => $tag['name'],
351 $heads = git_get_heads($page['project']);
352 $page['heads'] = array();
353 foreach ($heads as $h) {
354 $info = git_get_commit_info($page['project'], $h['h']);
355 $page['heads'][] = array(
356 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
358 'fullname' => $h['fullname'],
359 'name' => $h['name'],
363 elseif ($action === 'tree') {
365 $page['project'] = validate_project($_REQUEST['p']);
366 $page['tree'] = validate_hash($_REQUEST['h']);
367 $page['title'] = "$page[project] - Tree - ViewGit";
369 $page['entries'] = git_ls_tree($page['project'], $page['tree']);
371 elseif ($action === 'viewblob') {
373 $page['project'] = validate_project($_REQUEST['p']);
374 $page['hash'] = validate_hash($_REQUEST['h']);
375 $page['title'] = "$page[project] - Blob - ViewGit";
377 $page['data'] = join("\n", run_git($page['project'], "git cat-file blob $page[hash]"));
380 die('Invalid action');
383 require 'templates/header.php';
384 require "templates/$template.php";
385 require 'templates/footer.php';