Allow project configuration using globs/wildcards.
[viewgit.git] / inc / functions.php
blobf859a905d71ac4563999aff11c2e0ad9628788c6
1 <?php
2 /** @file
3 * Functions used by ViewGit.
4 */
6 function debug($msg)
8 file_put_contents('/tmp/viewgit.log', strftime('%H:%M:%S') ." $_SERVER[REMOTE_ADDR]:$_SERVER[REMOTE_PORT] $msg\n", FILE_APPEND);
11 /**
12 * Formats "git diff" output into xhtml.
13 * @return array(array of filenames, xhtml)
15 function format_diff($text)
17 $files = array();
19 // match every "^diff --git a/<path> b/<path>$" line
20 foreach (explode("\n", $text) as $line) {
21 if (preg_match('#^diff --git a/(.*) b/(.*)$#', $line, $matches) > 0) {
22 $files[$matches[1]] = urlencode($matches[1]);
26 $text = htmlentities($text);
28 $text = preg_replace(
29 array(
30 '/^(\+.*)$/m',
31 '/^(-.*)$/m',
32 '/^(@.*)$/m',
33 '/^([^d\+-@].*)$/m',
35 array(
36 '<span class="add">$1</span>',
37 '<span class="del">$1</span>',
38 '<span class="pos">$1</span>',
39 '<span class="etc">$1</span>',
41 $text);
42 $text = preg_replace_callback('#^diff --git a/(.*) b/(.*)$#m',
43 create_function(
44 '$m',
45 'return "<span class=\"diffline\"><a name=\"". urlencode($m[1]) ."\">diff --git a/$m[1] b/$m[2]</a></span>";'
47 $text);
49 return array($files, $text);
52 /**
53 * Get project information from config and git, name/description and HEAD
54 * commit info are returned in an array.
56 function get_project_info($name)
58 global $conf;
60 $info = $conf['projects'][$name];
61 $info['name'] = $name;
62 $info['description'] = file_get_contents($info['repo'] .'/description');
64 $headinfo = git_get_commit_info($name, 'HEAD');
65 $info['head_stamp'] = $headinfo['author_utcstamp'];
66 $info['head_datetime'] = strftime($conf['datetime'], $headinfo['author_utcstamp']);
67 $info['head_hash'] = $headinfo['h'];
68 $info['head_tree'] = $headinfo['tree'];
70 return $info;
73 /**
74 * Get details of a commit: tree, parents, author/committer (name, mail, date), message
76 function git_get_commit_info($project, $hash = 'HEAD')
78 global $conf;
80 $info = array();
81 $info['h_name'] = $hash;
82 $info['message_full'] = '';
83 $info['parents'] = array();
85 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
86 // tree <h>
87 // parent <h>
88 // author <name> "<"<mail>">" <stamp> <timezone>
89 // committer
90 // <empty>
91 // <message>
92 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
93 foreach ($output as $line) {
94 if (substr($line, 0, 4) === 'tree') {
95 $info['tree'] = substr($line, 5);
97 // may be repeated multiple times for merge/octopus
98 elseif (substr($line, 0, 6) === 'parent') {
99 $info['parents'][] = substr($line, 7);
101 elseif (preg_match($pattern, $line, $matches) > 0) {
102 $info[$matches[1] .'_name'] = $matches[2];
103 $info[$matches[1] .'_mail'] = $matches[3];
104 $info[$matches[1] .'_stamp'] = $matches[4];
105 $info[$matches[1] .'_timezone'] = $matches[5];
106 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
108 elseif (substr($line, 0, 4) === ' ') {
109 $info['message_full'] .= substr($line, 4) ."\n";
110 if (!isset($info['message'])) {
111 $info['message'] = substr($line, 4, $conf['commit_message_maxlen']);
112 $info['message_firstline'] = substr($line, 4);
115 elseif (preg_match('/^[0-9a-f]{40}$/', $line) > 0) {
116 $info['h'] = $line;
120 return $info;
124 * Get list of heads (branches) for a project.
126 function git_get_heads($project)
128 $heads = array();
130 $output = run_git($project, 'git show-ref --heads');
131 foreach ($output as $line) {
132 $fullname = substr($line, 41);
133 $name = array_pop(explode('/', $fullname));
134 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
137 return $heads;
141 * Get array containing path information for parts, starting from root_hash.
143 * @param root_hash commit/tree hash for the root tree
144 * @param parts array of path fragments
146 function git_get_path_info($project, $root_hash, $parts)
148 $pathinfo = array();
150 $tid = $root_hash;
151 $pathinfo = array();
152 foreach ($parts as $p) {
153 $entry = git_ls_tree_part($project, $tid, $p);
154 $pathinfo[] = $entry;
155 $tid = $entry['hash'];
158 return $pathinfo;
162 * Get revision list starting from given commit.
163 * @param max_count number of commit hashes to return, or all if not given
164 * @param start revision to start from, or HEAD if not given
166 function git_get_rev_list($project, $max_count = null, $start = 'HEAD')
168 $cmd = "git rev-list $start";
169 if (!is_null($max_count)) {
170 $cmd = "git rev-list --max-count=$max_count $start";
173 return run_git($project, $cmd);
177 * Get list of tags for a project.
179 function git_get_tags($project)
181 $tags = array();
183 $output = run_git($project, 'git show-ref --tags');
184 foreach ($output as $line) {
185 $fullname = substr($line, 41);
186 $name = array_pop(explode('/', $fullname));
187 $tags[] = array('h' => substr($line, 0, 40), 'fullname' => $fullname, 'name' => $name);
189 return $tags;
193 * Get information about objects in a tree.
194 * @param tree tree or commit hash
195 * @return list of arrays containing name, mode, type, hash
197 function git_ls_tree($project, $tree)
199 $entries = array();
200 $output = run_git($project, "git ls-tree $tree");
201 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
202 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
203 foreach ($output as $line) {
204 $parts = preg_split('/\s+/', $line, 4);
205 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
208 return $entries;
212 * Get information about the given object in a tree, or null if not in the tree.
214 function git_ls_tree_part($project, $tree, $name)
216 $entries = git_ls_tree($project, $tree);
217 foreach ($entries as $entry) {
218 if ($entry['name'] === $name) {
219 return $entry;
222 return null;
226 * Return a URL that contains the given parameters.
228 function makelink($dict)
230 $params = array();
231 foreach ($dict as $k => $v) {
232 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
234 if (count($params) > 0) {
235 return '?'. htmlentities(join('&', $params));
237 return '';
241 * Used to format RSS item title and description.
243 * @param info commit info from git_get_commit_info()
245 function rss_item_format($format, $info)
247 return preg_replace(array(
248 '/{AUTHOR}/',
249 '/{AUTHOR_MAIL}/',
250 '/{SHORTLOG}/',
251 '/{LOG}/',
252 '/{COMMITTER}/',
253 '/{COMMITTER_MAIL}/',
254 ), array(
255 $info['author_name'],
256 $info['author_mail'],
257 $info['message_firstline'],
258 $info['message'],
259 $info['committer_name'],
260 $info['committer_mail'],
261 ), $format);
264 function rss_pubdate($secs)
266 return date('D, d M Y H:i:s O', $secs);
270 * Executes a git command in the project repo.
271 * @return array of output lines
273 function run_git($project, $command)
275 global $conf;
277 $output = array();
278 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
279 exec($cmd, &$output);
280 return $output;
284 * Executes a git command in the project repo, sending output directly to the
285 * client.
287 function run_git_passthru($project, $command)
289 global $conf;
291 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
292 $result = 0;
293 passthru($cmd, &$result);
294 return $result;
298 * Makes sure the given project is valid. If it's not, this function will
299 * die().
300 * @return the project
302 function validate_project($project)
304 global $conf;
306 if (!in_array($project, array_keys($conf['projects']))) {
307 die('Invalid project');
309 return $project;
313 * Makes sure the given hash is valid. If it's not, this function will die().
314 * @return the hash
316 function validate_hash($hash)
318 if (!preg_match('/^[0-9a-z]{40}$/', $hash) && !preg_match('!^refs/(heads|tags)/[-.0-9a-z]+$!', $hash)) {
319 die('Invalid hash');
322 return $hash;