Use passthru() instead of exec() for (binary) data.
[viewgit.git] / index.php
blob457763d9ae90d193a22bfc30aa632c48433b574e
1 <?php
2 error_reporting(E_ALL);
4 require_once('inc/config.php');
6 function get_project_info($name)
8 global $conf;
10 $info = $conf['projects'][$name];
11 $info['name'] = $name;
12 $info['description'] = file_get_contents($info['repo'] .'/description');
14 return $info;
17 /**
18 * Get details of a commit: tree, parent, author/committer (name, mail, date), message
20 function git_get_commit_info($project, $hash)
22 $info = array();
23 $info['h'] = $hash;
24 $info['message_full'] = '';
26 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
27 // tree <h>
28 // parent <h>
29 // author <name> "<"<mail>">" <stamp> <timezone>
30 // committer
31 // <empty>
32 // <message>
33 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
34 foreach ($output as $line) {
35 if (substr($line, 0, 4) === 'tree') {
36 $info['tree'] = substr($line, 5);
38 elseif (substr($line, 0, 6) === 'parent') {
39 $info['parent'] = substr($line, 7);
41 elseif (preg_match($pattern, $line, $matches) > 0) {
42 $info[$matches[1] .'_name'] = $matches[2];
43 $info[$matches[1] .'_mail'] = $matches[3];
44 $info[$matches[1] .'_stamp'] = $matches[4];
45 $info[$matches[1] .'_timezone'] = $matches[5];
46 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
48 elseif (substr($line, 0, 4) === ' ') {
49 $info['message_full'] .= substr($line, 4) ."\n";
50 if (!isset($info['message'])) {
51 $info['message'] = substr($line, 4, 40);
56 return $info;
59 function git_get_heads($project)
61 $heads = array();
63 $output = run_git($project, 'git show-ref --heads');
64 foreach ($output as $line) {
65 $fullname = substr($line, 41);
66 $name = array_pop(explode('/', $fullname));
67 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
70 return $heads;
73 function git_get_rev_list($project, $max_count = null)
75 $cmd = 'git rev-list HEAD';
76 if (!is_null($max_count)) {
77 $cmd = "git rev-list --max-count=$max_count HEAD";
80 return run_git($project, $cmd);
83 function git_ls_tree($project, $tree)
85 $entries = array();
86 $output = run_git($project, "git ls-tree $tree");
87 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
88 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
89 foreach ($output as $line) {
90 $parts = preg_split('/\s+/', $line, 4);
91 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
94 return $entries;
97 function makelink($dict)
99 $params = array();
100 foreach ($dict as $k => $v) {
101 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
103 if (count($params) > 0) {
104 return '?'. htmlentities(join('&', $params));
106 return '';
110 * Executes a git command in the project repo.
111 * @return array of output lines
113 function run_git($project, $command)
115 global $conf;
117 $output = array();
118 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
119 exec($cmd, &$output);
120 return $output;
124 * Executes a git command in the project repo, sending output directly to the
125 * client.
127 function run_git_passthru($project, $command)
129 global $conf;
131 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
132 $result = 0;
133 passthru($cmd, &$result);
134 return $result;
138 * Makes sure the given project is valid. If it's not, this function will
139 * die().
140 * @return the project
142 function validate_project($project)
144 global $conf;
146 if (!in_array($project, array_keys($conf['projects']))) {
147 die('Invalid project');
149 return $project;
153 * Makes sure the given hash is valid. If it's not, this function will die().
154 * @return the hash
156 function validate_hash($hash)
158 if (strlen($hash) != 40 || !preg_match('/^[0-9a-z]*$/', $hash)) {
159 die('Invalid hash');
162 return $hash;
165 $action = 'index';
166 $template = '';
167 $page['title'] = 'ViewGit';
169 if (isset($_REQUEST['a'])) {
170 $action = strtolower($_REQUEST['a']);
172 $page['action'] = $action;
174 if ($action === 'index') {
175 $template = 'index';
176 $page['title'] = 'List of projects - ViewGit';
178 foreach (array_keys($conf['projects']) as $p) {
179 $page['projects'][] = get_project_info($p);
182 elseif ($action === 'archive') {
183 $project = validate_project($_REQUEST['p']);
184 $tree = validate_hash($_REQUEST['h']);
185 $type = $_REQUEST['t'];
187 // TODO check that the data passed is really valid
188 if ($type === 'targz') {
189 header("Content-Type: application/x-tar-gz");
190 header("Content-Transfer-Encoding: binary");
191 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.tar.gz\";");
193 $data = join("\n", run_git($project, "git archive --format=tar $tree |gzip"));
194 header("Content-Length: ". strlen($data));
195 echo $data;
197 run_git_passthru($project, "git archive --format=tar $tree |gzip");
199 elseif ($type === 'zip') {
200 header("Content-Type: application/x-zip");
201 header("Content-Transfer-Encoding: binary");
202 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.zip\";");
204 $data = join("\n", run_git($project, "git archive --format=zip $tree"));
205 header("Content-Length: ". strlen($data));
206 echo $data;
208 run_git_passthru($project, "git archive --format=zip $tree");
210 else {
211 die('Invalid archive type requested');
214 die();
216 elseif ($action === 'blob') {
217 $page['project'] = validate_project($_REQUEST['p']);
218 $page['hash'] = validate_hash($_REQUEST['h']);
220 header('Content-type: text/plain');
222 run_git_passthru($page['project'], "git cat-file blob $page[hash]");
223 die();
225 elseif ($action === 'commit') {
226 $template = 'commit';
227 $page['project'] = validate_project($_REQUEST['p']);
228 $page['commit_id'] = validate_hash($_REQUEST['h']);
230 $info = git_get_commit_info($page['project'], $page['commit_id']);
232 $page['author_name'] = $info['author_name'];
233 $page['author_mail'] = $info['author_mail'];
234 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
235 $page['committer_name'] = $info['committer_name'];
236 $page['committer_mail'] = $info['committer_mail'];
237 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
238 $page['tree'] = $info['tree'];
239 $page['parent'] = $info['parent'];
240 $page['message'] = $info['message'];
241 $page['message_full'] = $info['message_full'];
244 elseif ($action === 'summary') {
245 $template = 'summary';
246 $page['project'] = validate_project($_REQUEST['p']);
247 $page['title'] = "$page[project] - Summary - ViewGit";
249 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
250 foreach ($revs as $rev) {
251 $info = git_get_commit_info($page['project'], $rev);
252 $page['shortlog'][] = array(
253 'author' => $info['author_name'],
254 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
255 'message' => $info['message'],
256 'commit_id' => $rev,
257 'tree' => $info['tree'],
261 $heads = git_get_heads($page['project']);
262 $page['heads'] = array();
263 foreach ($heads as $h) {
264 $info = git_get_commit_info($page['project'], $h['h']);
265 $page['heads'][] = array(
266 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
267 'h' => $h['h'],
268 'fullname' => $h['fullname'],
269 'name' => $h['name'],
273 elseif ($action === 'tree') {
274 $template = 'tree';
275 $page['project'] = validate_project($_REQUEST['p']);
276 $page['tree'] = validate_hash($_REQUEST['h']);
277 $page['title'] = "$page[project] - Tree - ViewGit";
279 $page['entries'] = git_ls_tree($page['project'], $page['tree']);
281 else {
282 die('Invalid action');
285 require 'templates/header.php';
286 require "templates/$template.php";
287 require 'templates/footer.php';