tree page: better name suggestion for downloads.
[viewgit.git] / index.php
blob564783357fe5f2e566a0d77b9445f9fac8eafb56
1 <?php
2 error_reporting(E_ALL);
4 require_once('inc/config.php');
6 /**
7 * Formats "git diff" output into xhtml.
8 */
9 function format_diff($text)
11 $text = htmlentities($text);
13 $text = preg_replace(
14 array(
15 '/^(\+.*)$/m',
16 '/^(-.*)$/m',
17 '/^(@.*)$/m',
18 '/^([^\+-@].*)$/m',
20 array(
21 '<span class="add">$1</span>',
22 '<span class="del">$1</span>',
23 '<span class="pos">$1</span>',
24 '<span class="etc">$1</span>',
26 $text);
28 return $text;
31 function get_project_info($name)
33 global $conf;
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'];
45 return $info;
48 /**
49 * Get details of a commit: tree, parent, author/committer (name, mail, date), message
51 function git_get_commit_info($project, $hash)
53 $info = array();
54 $info['h'] = $hash;
55 $info['message_full'] = '';
57 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
58 // tree <h>
59 // parent <h>
60 // author <name> "<"<mail>">" <stamp> <timezone>
61 // committer
62 // <empty>
63 // <message>
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);
87 return $info;
90 function git_get_heads($project)
92 $heads = array();
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");
101 return $heads;
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)
116 $tags = array();
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);
124 return $tags;
127 function git_ls_tree($project, $tree)
129 $entries = array();
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]);
138 return $entries;
141 function makelink($dict)
143 $params = array();
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));
150 return '';
154 * Executes a git command in the project repo.
155 * @return array of output lines
157 function run_git($project, $command)
159 global $conf;
161 $output = array();
162 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
163 exec($cmd, &$output);
164 return $output;
168 * Executes a git command in the project repo, sending output directly to the
169 * client.
171 function run_git_passthru($project, $command)
173 global $conf;
175 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
176 $result = 0;
177 passthru($cmd, &$result);
178 return $result;
182 * Makes sure the given project is valid. If it's not, this function will
183 * die().
184 * @return the project
186 function validate_project($project)
188 global $conf;
190 if (!in_array($project, array_keys($conf['projects']))) {
191 die('Invalid project');
193 return $project;
197 * Makes sure the given hash is valid. If it's not, this function will die().
198 * @return the hash
200 function validate_hash($hash)
202 if (strlen($hash) != 40 || !preg_match('/^[0-9a-z]*$/', $hash)) {
203 die('Invalid hash');
206 return $hash;
209 $action = 'index';
210 $template = 'index';
211 $page['title'] = 'ViewGit';
213 if (isset($_REQUEST['a'])) {
214 $action = strtolower($_REQUEST['a']);
216 $page['action'] = $action;
218 if ($action === 'index') {
219 $template = '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");
248 else {
249 die('Invalid archive type requested');
252 die();
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");
264 die();
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'],
317 'commit_id' => $rev,
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'],
334 'commit_id' => $rev,
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']),
345 'h' => $tag['h'],
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']),
357 'h' => $h['h'],
358 'fullname' => $h['fullname'],
359 'name' => $h['name'],
363 elseif ($action === 'tree') {
364 $template = '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') {
372 $template = 'blob';
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]"));
379 else {
380 die('Invalid action');
383 require 'templates/header.php';
384 require "templates/$template.php";
385 require 'templates/footer.php';