Populate commit view with repo data.
[viewgit.git] / index.php
blobff77a9364afeaa6483ccaeb69dda82179d96ae6a
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 makelink($dict)
79 $params = array();
80 foreach ($dict as $k => $v) {
81 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
83 if (count($params) > 0) {
84 return '?'. htmlentities(join('&', $params));
86 return '';
89 /**
90 * Executes a git command in the project repo.
91 * @return array of output lines
93 function run_git($project, $command)
95 global $conf;
97 $output = array();
98 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
99 exec($cmd, &$output);
100 return $output;
103 $action = 'index';
104 $template = '';
105 $page['title'] = 'ViewGit';
107 if (isset($_REQUEST['a'])) {
108 $action = strtolower($_REQUEST['a']);
111 if ($action === 'index') {
112 $template = 'index';
114 foreach (array_keys($conf['projects']) as $p) {
115 $page['projects'][] = get_project_info($p);
118 elseif ($action === 'commit') {
119 $template = 'commit';
120 $page['project'] = strtolower($_REQUEST['p']); // TODO validate
121 $page['commit_id'] = strtolower($_REQUEST['h']);
123 $info = git_get_commit_info($page['project'], $page['commit_id']);
125 $page['author_name'] = $info['author_name'];
126 $page['author_mail'] = $info['author_mail'];
127 $page['author_datetime'] = strftime($conf['datetime'], $info['author_stamp']);
128 $page['committer_name'] = $info['committer_name'];
129 $page['committer_mail'] = $info['committer_mail'];
130 $page['committer_datetime'] = strftime($conf['datetime'], $info['committer_stamp']);
131 $page['tree'] = $info['tree'];
132 $page['parent'] = $info['parent'];
133 $page['message'] = $info['message'];
134 $page['message_full'] = $info['message_full'];
137 elseif ($action === 'summary') {
138 $template = 'summary';
139 $page['project'] = strtolower($_REQUEST['p']);
140 // TODO: validate project
142 $revs = git_get_rev_list($page['project']);
143 foreach ($revs as $rev) {
144 $info = git_get_commit_info($page['project'], $rev);
145 $page['shortlog'][] = array(
146 'author' => $info['author_name'],
147 'date' => strftime($conf['datetime'], $info['author_stamp']),
148 'message' => $info['message'],
149 'commit_id' => $rev,
153 $heads = git_get_heads($page['project']);
154 $page['heads'] = array();
155 foreach ($heads as $h) {
156 $info = git_get_commit_info($page['project'], $h['h']);
157 $page['heads'][] = array(
158 'date' => strftime($conf['datetime'], $info['author_stamp']),
159 'h' => $h['h'],
160 'fullname' => $h['fullname'],
161 'name' => $h['name'],
165 else {
166 die('Invalid action');
169 require 'templates/header.php';
170 require "templates/$template.php";
171 require 'templates/footer.php';