3 * Functions used by ViewGit.
8 file_put_contents('/tmp/viewgit.log', strftime('%H:%M:%S') ." $_SERVER[REMOTE_ADDR]:$_SERVER[REMOTE_PORT] $msg\n", FILE_APPEND
);
12 * Formats "git diff" output into xhtml.
13 * @return array(array of filenames, xhtml)
15 function format_diff($text)
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);
36 '<span class="add">$1</span>',
37 '<span class="del">$1</span>',
38 '<span class="pos">$1</span>',
39 '<span class="etc">$1</span>',
42 $text = preg_replace_callback('#^diff --git a/(.*) b/(.*)$#m',
45 'return "<span class=\"diffline\"><a name=\"". urlencode($m[1]) ."\">diff --git a/$m[1] b/$m[2]</a></span>";'
49 return array($files, $text);
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)
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'];
73 function git_diffstat($project, $commit, $commit_base = null)
75 if (is_null($commit_base)) {
76 $commit_base = "$commit^";
78 return join("\n", run_git($project, "git diff --stat $commit_base..$commit"));
82 * Get details of a commit: tree, parents, author/committer (name, mail, date), message
84 function git_get_commit_info($project, $hash = 'HEAD')
89 $info['h_name'] = $hash;
90 $info['message_full'] = '';
91 $info['parents'] = array();
93 $output = run_git($project, "git rev-list --header --max-count=1 $hash");
96 // author <name> "<"<mail>">" <stamp> <timezone>
100 $pattern = '/^(author|committer) ([^<]+) <([^>]*)> ([0-9]+) (.*)$/';
101 foreach ($output as $line) {
102 if (substr($line, 0, 4) === 'tree') {
103 $info['tree'] = substr($line, 5);
105 // may be repeated multiple times for merge/octopus
106 elseif (substr($line, 0, 6) === 'parent') {
107 $info['parents'][] = substr($line, 7);
109 elseif (preg_match($pattern, $line, $matches) > 0) {
110 $info[$matches[1] .'_name'] = $matches[2];
111 $info[$matches[1] .'_mail'] = $matches[3];
112 $info[$matches[1] .'_stamp'] = $matches[4];
113 $info[$matches[1] .'_timezone'] = $matches[5];
114 $info[$matches[1] .'_utcstamp'] = $matches[4] - ((intval($matches[5]) / 100.0) * 3600);
116 if (isset($conf['mail_filter'])) {
117 $info[$matches[1] .'_mail'] = $conf['mail_filter']($info[$matches[1] .'_mail']);
120 elseif (substr($line, 0, 4) === ' ') {
121 $info['message_full'] .= substr($line, 4) ."\n";
122 if (!isset($info['message'])) {
123 $info['message'] = substr($line, 4, $conf['commit_message_maxlen']);
124 $info['message_firstline'] = substr($line, 4);
127 elseif (preg_match('/^[0-9a-f]{40}$/', $line) > 0) {
136 * Get list of heads (branches) for a project.
138 function git_get_heads($project)
142 $output = run_git($project, 'git show-ref --heads');
143 foreach ($output as $line) {
144 $fullname = substr($line, 41);
145 $name = array_pop(explode('/', $fullname));
146 $heads[] = array('h' => substr($line, 0, 40), 'fullname' => "$fullname", 'name' => "$name");
153 * Get array containing path information for parts, starting from root_hash.
155 * @param root_hash commit/tree hash for the root tree
156 * @param parts array of path fragments
158 function git_get_path_info($project, $root_hash, $parts)
164 foreach ($parts as $p) {
165 $entry = git_ls_tree_part($project, $tid, $p);
166 $pathinfo[] = $entry;
167 $tid = $entry['hash'];
174 * Get revision list starting from given commit.
175 * @param max_count number of commit hashes to return, or all if not given
176 * @param start revision to start from, or HEAD if not given
178 function git_get_rev_list($project, $max_count = null, $start = 'HEAD')
180 $cmd = "git rev-list $start";
181 if (!is_null($max_count)) {
182 $cmd = "git rev-list --max-count=$max_count $start";
185 return run_git($project, $cmd);
189 * Get list of tags for a project.
191 function git_get_tags($project)
195 $output = run_git($project, 'git show-ref --tags');
196 foreach ($output as $line) {
197 $fullname = substr($line, 41);
198 $name = array_pop(explode('/', $fullname));
199 $tags[] = array('h' => substr($line, 0, 40), 'fullname' => $fullname, 'name' => $name);
205 * Get information about objects in a tree.
206 * @param tree tree or commit hash
207 * @return list of arrays containing name, mode, type, hash
209 function git_ls_tree($project, $tree)
212 $output = run_git($project, "git ls-tree $tree");
213 // 100644 blob 493b7fc4296d64af45dac64bceac2d9a96c958c1 .gitignore
214 // 040000 tree 715c78b1011dc58106da2a1af2fe0aa4c829542f doc
215 foreach ($output as $line) {
216 $parts = preg_split('/\s+/', $line, 4);
217 $entries[] = array('name' => $parts[3], 'mode' => $parts[0], 'type' => $parts[1], 'hash' => $parts[2]);
224 * Get information about the given object in a tree, or null if not in the tree.
226 function git_ls_tree_part($project, $tree, $name)
228 $entries = git_ls_tree($project, $tree);
229 foreach ($entries as $entry) {
230 if ($entry['name'] === $name) {
240 function handle_tags($project)
244 $tags = git_get_tags($project);
246 foreach ($tags as $tag) {
247 $info = git_get_commit_info($project, $tag['h']);
249 'stamp' => $info['author_utcstamp'],
250 'date' => strftime($conf['datetime'], $info['author_utcstamp']),
252 'fullname' => $tag['fullname'],
253 'name' => $tag['name'],
257 // sort tags newest first
258 // aka. two more reasons to hate PHP (figuring those out is your homework:)
259 usort($result, create_function(
261 '$a = $x["stamp"]; $b = $y["stamp"]; return ($a == $b ? 0 : ($a > $b ? -1 : 1));'
264 //$result = array_splice($page['tags'], 0, $conf['summary_tags']);
270 * Return a URL that contains the given parameters.
272 function makelink($dict)
275 foreach ($dict as $k => $v) {
276 $params[] = rawurlencode($k) .'='. str_replace('%2F', '/', rawurlencode($v));
278 if (count($params) > 0) {
279 return '?'. htmlentities(join('&', $params));
285 * Obfuscate the e-mail address.
287 function obfuscate_mail($mail)
289 return str_replace(array('@', '.'), array(' at ', ' dot '), $mail);
293 * Used to format RSS item title and description.
295 * @param info commit info from git_get_commit_info()
297 function rss_item_format($format, $info)
299 return preg_replace(array(
305 '/{COMMITTER_MAIL}/',
308 $info['author_name'],
309 $info['author_mail'],
310 $info['message_firstline'],
311 $info['message_full'],
312 $info['committer_name'],
313 $info['committer_mail'],
314 isset($info['diffstat']) ?
$info['diffstat'] : '',
318 function rss_pubdate($secs)
320 return date('D, d M Y H:i:s O', $secs);
324 * Executes a git command in the project repo.
325 * @return array of output lines
327 function run_git($project, $command)
332 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
333 exec($cmd, &$output);
338 * Executes a git command in the project repo, sending output directly to the
341 function run_git_passthru($project, $command)
345 $cmd = "GIT_DIR=". $conf['projects'][$project]['repo'] ." $command";
347 passthru($cmd, &$result);
352 * Makes sure the given project is valid. If it's not, this function will
354 * @return the project
356 function validate_project($project)
360 if (!in_array($project, array_keys($conf['projects']))) {
361 die('Invalid project');
367 * Makes sure the given hash is valid. If it's not, this function will die().
370 function validate_hash($hash)
372 if (!preg_match('/^[0-9a-z]{40}$/', $hash) && !preg_match('!^refs/(heads|tags)/[-.0-9a-z]+$!', $hash)) {