More todo items.
[viewgit.git] / index.php
blob6cdf758c2babc277062182822547548bf88871b2
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)
74 return run_git($project, 'git rev-list HEAD');
77 function git_ls_tree($project, $tree)
79 $entries = array();
80 $output = run_git($project, "git ls-tree $tree");
81 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
82 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
83 foreach ($output as $line) {
84 $parts = preg_split('/\s+/', $line, 4);
85 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
88 return $entries;
91 function makelink($dict)
93 $params = array();
94 foreach ($dict as $k => $v) {
95 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
97 if (count($params) > 0) {
98 return '?'. htmlentities(join('&', $params));
100 return '';
104 * Executes a git command in the project repo.
105 * @return array of output lines
107 function run_git($project, $command)
109 global $conf;
111 $output = array();
112 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
113 exec($cmd, &$output);
114 return $output;
117 $action = 'index';
118 $template = '';
119 $page['title'] = 'ViewGit';
121 if (isset($_REQUEST['a'])) {
122 $action = strtolower($_REQUEST['a']);
125 if ($action === 'index') {
126 $template = 'index';
128 foreach (array_keys($conf['projects']) as $p) {
129 $page['projects'][] = get_project_info($p);
132 elseif ($action === 'commit') {
133 $template = 'commit';
134 $page['project'] = strtolower($_REQUEST['p']); // TODO validate
135 $page['commit_id'] = strtolower($_REQUEST['h']); // TODO validate
137 $info = git_get_commit_info($page['project'], $page['commit_id']);
139 $page['author_name'] = $info['author_name'];
140 $page['author_mail'] = $info['author_mail'];
141 $page['author_datetime'] = strftime($conf['datetime'], $info['author_stamp']);
142 $page['committer_name'] = $info['committer_name'];
143 $page['committer_mail'] = $info['committer_mail'];
144 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_stamp']);
145 $page['tree'] = $info['tree'];
146 $page['parent'] = $info['parent'];
147 $page['message'] = $info['message'];
148 $page['message_full'] = $info['message_full'];
151 elseif ($action === 'summary') {
152 $template = 'summary';
153 $page['project'] = strtolower($_REQUEST['p']);
154 // TODO: validate project
156 $revs = git_get_rev_list($page['project']);
157 foreach ($revs as $rev) {
158 $info = git_get_commit_info($page['project'], $rev);
159 $page['shortlog'][] = array(
160 'author' => $info['author_name'],
161 'date' => strftime($conf['datetime'], $info['author_stamp']),
162 'message' => $info['message'],
163 'commit_id' => $rev,
167 $heads = git_get_heads($page['project']);
168 $page['heads'] = array();
169 foreach ($heads as $h) {
170 $info = git_get_commit_info($page['project'], $h['h']);
171 $page['heads'][] = array(
172 'date' => strftime($conf['datetime'], $info['author_stamp']),
173 'h' => $h['h'],
174 'fullname' => $h['fullname'],
175 'name' => $h['name'],
179 elseif ($action === 'tree') {
180 $template = 'tree';
181 $page['project'] = strtolower($_REQUEST['p']); // TODO validate
182 $page['tree'] = $_REQUEST['h']; // TODO validate
184 $page['entries'] = git_ls_tree($page['project'], $page['tree']);
186 else {
187 die('Invalid action');
190 require 'templates/header.php';
191 require "templates/$template.php";
192 require 'templates/footer.php';