New option 'summary_shortlog'.
[viewgit.git] / index.php
blob294f233e120a37d8afaf0006a166f484e383988f
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];
47 elseif (substr($line, 0, 4) === ' ') {
48 $info['message_full'] .= substr($line, 4) ."\n";
49 if (!isset($info['message'])) {
50 $info['message'] = substr($line, 4, 40);
55 return $info;
58 function git_get_heads($project)
60 $heads = array();
62 $output = run_git($project, 'git show-ref --heads');
63 foreach ($output as $line) {
64 $fullname = substr($line, 41);
65 $name = array_pop(explode('/', $fullname));
66 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
69 return $heads;
72 function git_get_rev_list($project, $max_count = null)
74 $cmd = 'git rev-list HEAD';
75 if (!is_null($max_count)) {
76 $cmd = "git rev-list --max-count=$max_count HEAD";
79 return run_git($project, $cmd);
82 function git_ls_tree($project, $tree)
84 $entries = array();
85 $output = run_git($project, "git ls-tree $tree");
86 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
87 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
88 foreach ($output as $line) {
89 $parts = preg_split('/\s+/', $line, 4);
90 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
93 return $entries;
96 function makelink($dict)
98 $params = array();
99 foreach ($dict as $k => $v) {
100 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
102 if (count($params) > 0) {
103 return '?'. htmlentities(join('&', $params));
105 return '';
109 * Executes a git command in the project repo.
110 * @return array of output lines
112 function run_git($project, $command)
114 global $conf;
116 $output = array();
117 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
118 exec($cmd, &$output);
119 return $output;
122 $action = 'index';
123 $template = '';
124 $page['title'] = 'ViewGit';
126 if (isset($_REQUEST['a'])) {
127 $action = strtolower($_REQUEST['a']);
130 if ($action === 'index') {
131 $template = 'index';
133 foreach (array_keys($conf['projects']) as $p) {
134 $page['projects'][] = get_project_info($p);
137 elseif ($action === 'archive') {
138 $project = $_REQUEST['p']; // TODO validate
139 $tree = $_REQUEST['h']; // TODO validate
140 $type = $_REQUEST['t'];
142 // TODO check that the data passed is really valid
143 if ($type === 'targz') {
144 header("Content-Type: application/x-tar-gz");
145 header("Content-Transfer-Encoding: binary");
146 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.tar.gz\";");
147 $data = join("\n", run_git($project, "git archive --format=tar $tree |gzip"));
148 header("Content-Length: ". strlen($data));
149 echo $data;
151 elseif ($type === 'zip') {
152 header("Content-Type: application/x-zip");
153 header("Content-Transfer-Encoding: binary");
154 header("Content-Disposition: attachment; filename=\"$project-tree-$tree.zip\";");
155 $data = join("\n", run_git($project, "git archive --format=zip $tree"));
156 header("Content-Length: ". strlen($data));
157 echo $data;
159 else {
160 die('Invalid archive type requested');
163 die();
165 elseif ($action === 'commit') {
166 $template = 'commit';
167 $page['project'] = strtolower($_REQUEST['p']); // TODO validate
168 $page['commit_id'] = strtolower($_REQUEST['h']); // TODO validate
170 $info = git_get_commit_info($page['project'], $page['commit_id']);
172 $page['author_name'] = $info['author_name'];
173 $page['author_mail'] = $info['author_mail'];
174 $page['author_datetime'] = strftime($conf['datetime'], $info['author_stamp']);
175 $page['committer_name'] = $info['committer_name'];
176 $page['committer_mail'] = $info['committer_mail'];
177 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_stamp']);
178 $page['tree'] = $info['tree'];
179 $page['parent'] = $info['parent'];
180 $page['message'] = $info['message'];
181 $page['message_full'] = $info['message_full'];
184 elseif ($action === 'summary') {
185 $template = 'summary';
186 $page['project'] = strtolower($_REQUEST['p']);
187 // TODO: validate project
189 $revs = git_get_rev_list($page['project'], $conf['summary_shortlog']);
190 foreach ($revs as $rev) {
191 $info = git_get_commit_info($page['project'], $rev);
192 $page['shortlog'][] = array(
193 'author' => $info['author_name'],
194 'date' => strftime($conf['datetime'], $info['author_stamp']),
195 'message' => $info['message'],
196 'commit_id' => $rev,
200 $heads = git_get_heads($page['project']);
201 $page['heads'] = array();
202 foreach ($heads as $h) {
203 $info = git_get_commit_info($page['project'], $h['h']);
204 $page['heads'][] = array(
205 'date' => strftime($conf['datetime'], $info['author_stamp']),
206 'h' => $h['h'],
207 'fullname' => $h['fullname'],
208 'name' => $h['name'],
212 elseif ($action === 'tree') {
213 $template = 'tree';
214 $page['project'] = strtolower($_REQUEST['p']); // TODO validate
215 $page['tree'] = $_REQUEST['h']; // TODO validate
217 $page['entries'] = git_ls_tree($page['project'], $page['tree']);
219 else {
220 die('Invalid action');
223 require 'templates/header.php';
224 require "templates/$template.php";
225 require 'templates/footer.php';