summary: added tar/gz & zip download links.
[viewgit.git] / index.php
blob833ecfe29a333b995de4c94d63af96776954155f
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 * Makes sure the given project is valid. If it's not, this function will
125 * die().
126 * @return the project
128 function validate_project($project)
130 global $conf;
132 if (!in_array($project, array_keys($conf['projects']))) {
133 die('Invalid project');
135 return $project;
139 * Makes sure the given hash is valid. If it's not, this function will die().
140 * @return the hash
142 function validate_hash($hash)
144 if (strlen($hash) != 40 || !preg_match('/^[0-9a-z]*$/', $hash)) {
145 die('Invalid hash');
148 return $hash;
151 $action = 'index';
152 $template = '';
153 $page['title'] = 'ViewGit';
155 if (isset($_REQUEST['a'])) {
156 $action = strtolower($_REQUEST['a']);
158 $page['action'] = $action;
160 if ($action === 'index') {
161 $template = 'index';
162 $page['title'] = 'List of projects - ViewGit';
164 foreach (array_keys($conf['projects']) as $p) {
165 $page['projects'][] = get_project_info($p);
168 elseif ($action === 'archive') {
169 $project = validate_project($_REQUEST['p']);
170 $tree = validate_hash($_REQUEST['h']);
171 $type = $_REQUEST['t'];
173 // TODO check that the data passed is really valid
174 if ($type === 'targz') {
175 header("Content-Type: application/x-tar-gz");
176 header("Content-Transfer-Encoding: binary");
177 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.tar.gz\";");
178 $data = join("\n", run_git($project, "git archive --format=tar $tree |gzip"));
179 header("Content-Length: ". strlen($data));
180 echo $data;
182 elseif ($type === 'zip') {
183 header("Content-Type: application/x-zip");
184 header("Content-Transfer-Encoding: binary");
185 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.zip\";");
186 $data = join("\n", run_git($project, "git archive --format=zip $tree"));
187 header("Content-Length: ". strlen($data));
188 echo $data;
190 else {
191 die('Invalid archive type requested');
194 die();
196 elseif ($action === 'commit') {
197 $template = 'commit';
198 $page['project'] = validate_project($_REQUEST['p']);
199 $page['commit_id'] = validate_hash($_REQUEST['h']);
201 $info = git_get_commit_info($page['project'], $page['commit_id']);
203 $page['author_name'] = $info['author_name'];
204 $page['author_mail'] = $info['author_mail'];
205 $page['author_datetime'] = strftime($conf['datetime'], $info['author_utcstamp']);
206 $page['committer_name'] = $info['committer_name'];
207 $page['committer_mail'] = $info['committer_mail'];
208 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_utcstamp']);
209 $page['tree'] = $info['tree'];
210 $page['parent'] = $info['parent'];
211 $page['message'] = $info['message'];
212 $page['message_full'] = $info['message_full'];
215 elseif ($action === 'summary') {
216 $template = 'summary';
217 $page['project'] = validate_project($_REQUEST['p']);
218 $page['title'] = "$page[project] - Summary - ViewGit";
220 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
221 foreach ($revs as $rev) {
222 $info = git_get_commit_info($page['project'], $rev);
223 $page['shortlog'][] = array(
224 'author' => $info['author_name'],
225 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
226 'message' => $info['message'],
227 'commit_id' => $rev,
228 'tree' => $info['tree'],
232 $heads = git_get_heads($page['project']);
233 $page['heads'] = array();
234 foreach ($heads as $h) {
235 $info = git_get_commit_info($page['project'], $h['h']);
236 $page['heads'][] = array(
237 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
238 'h' => $h['h'],
239 'fullname' => $h['fullname'],
240 'name' => $h['name'],
244 elseif ($action === 'tree') {
245 $template = 'tree';
246 $page['project'] = validate_project($_REQUEST['p']);
247 $page['tree'] = validate_hash($_REQUEST['h']);
248 $page['title'] = "$page[project] - Tree - ViewGit";
250 $page['entries'] = git_ls_tree($page['project'], $page['tree']);
252 else {
253 die('Invalid action');
256 require 'templates/header.php';
257 require "templates/$template.php";
258 require 'templates/footer.php';