Added the changelog and fixed the source browser.

The source browser is normally a little bit more robust.
This commit is contained in:
Loic d'Anterroches 2008-07-27 01:31:10 +02:00
parent f57a0ec08b
commit c915f8fccf
8 changed files with 237 additions and 39 deletions

View File

@ -41,18 +41,19 @@ class IDF_Git
* *
* 'perm', 'type', 'size', 'hash', 'file' * 'perm', 'type', 'size', 'hash', 'file'
* *
* @param string Commit/Branch ('HEAD') * @param string Tree ('HEAD')
* @param string Base folder ('') * @param string Base folder ('')
* @return array * @return array
*/ */
public function filesInTree($commit='HEAD', $basefolder='') public function filesInTree($tree='HEAD', $basefolder='')
{ {
if (is_object($basefolder)) { if (is_object($basefolder)) {
$base = $basefolder; $base = $basefolder;
} else if ($basefolder != '' } else if (
$basefolder != ''
and and
( (
(false === ($base=$this->getFileInfo($basefolder, $commit))) (false === ($base=$this->getFileInfo($basefolder, $tree)))
or or
($base->type != 'tree') ($base->type != 'tree')
)) { )) {
@ -60,25 +61,35 @@ class IDF_Git
} else { } else {
// no base // no base
$base = (object) array('file' => '', $base = (object) array('file' => '',
'hash' => $commit); 'hash' => $tree);
} }
$res = array(); $res = array();
$out = array(); $out = array();
$cmd = sprintf('GIT_DIR=%s git-ls-tree -t -l %s', $this->repo, $base->hash); $cmd = sprintf('GIT_DIR=%s git-ls-tree -t -l %s', $this->repo, $base->hash);
exec($cmd, &$out); exec($cmd, &$out);
$rawlog = array();
$cmd = sprintf('GIT_DIR=%s git log --raw --abbrev=40 --pretty=oneline',
$this->repo);
exec($cmd, &$rawlog);
$rawlog = implode("\n", array_reverse($rawlog));
$current_dir = getcwd(); $current_dir = getcwd();
chdir(substr($this->repo, 0, -5)); chdir(substr($this->repo, 0, -5));
foreach ($out as $line) { foreach ($out as $line) {
list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY); list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY);
$cm = array(); $matches = array();
$cmd = sprintf('GIT_DIR=%s git log -1 --pretty=format:\'%%H %%at %%s\' %s -- %s', $this->repo, $commit, ($base->file) ? $base->file.'/'.$file : $file); $date = '1970-01-01 12:00:00';
exec($cmd, &$cm); $log = '';
list($h, $time, $log) = explode(' ', $cm[0], 3); if ($type == 'blob' and preg_match('/^\:\d{6} \d{6} [0-9a-f]{40} '.$hash.' .*^([0-9a-f]{40})/msU',
$rawlog, &$matches)) {
$_c = $this->getCommit($matches[1]);
$date = $_c->date;
$log = $_c->title;
}
$res[] = (object) array('perm' => $perm, 'type' => $type, $res[] = (object) array('perm' => $perm, 'type' => $type,
'size' => $size, 'hash' => $hash, 'size' => $size, 'hash' => $hash,
'fullpath' => ($base->file) ? $base->file.'/'.$file : $file, 'fullpath' => ($base->file) ? $base->file.'/'.$file : $file,
'log' => $log, 'time' => $time, 'log' => $log, 'date' => $date,
'file' => $file); 'file' => $file);
} }
chdir($current_dir); chdir($current_dir);
@ -88,14 +99,14 @@ class IDF_Git
/** /**
* Get the file info. * Get the file info.
* *
* @param string Tree to test * @param string File
* @param string Commit/Branch ('HEAD') * @param string Tree ('HEAD')
* @return false or Tree information * @return false Information
*/ */
public function getFileInfo($totest, $commit='HEAD') public function getFileInfo($totest, $tree='HEAD')
{ {
$cmd_tmpl = 'GIT_DIR=%s git-ls-tree -r -t -l %s'; $cmd_tmpl = 'GIT_DIR=%s git-ls-tree -r -t -l %s';
$cmd = sprintf($cmd_tmpl, $this->repo, $commit); $cmd = sprintf($cmd_tmpl, $this->repo, $tree);
$out = array(); $out = array();
exec($cmd, &$out); exec($cmd, &$out);
foreach ($out as $line) { foreach ($out as $line) {
@ -134,4 +145,104 @@ class IDF_Git
} }
return $res; return $res;
} }
/**
* Get commit details.
*
* @param string Commit ('HEAD').
* @return array Changes.
*/
public function getCommit($commit='HEAD')
{
$cmd = sprintf('GIT_DIR=%s git show --date=iso --pretty=medium %s',
escapeshellarg($this->repo), $commit);
$out = array();
exec($cmd, &$out);
$log = array();
$change = array();
$inchange = false;
foreach ($out as $line) {
if (!$inchange and 0 === strpos($line, 'diff --git a')) {
$inchange = true;
}
if ($inchange) {
$change[] = $line;
} else {
$log[] = $line;
}
}
$out = self::parseLog($log);
$out[0]->changes = $change;
return $out[0];
}
/**
* Get latest changes.
*
* @param string Tree ('HEAD').
* @param int Number of changes (10).
* @return array Changes.
*/
public function getChangeLog($tree='HEAD', $n=10)
{
$format = 'commit %H%nAuthor: %an <%ae>%nTree: %T%nDate: %ai%n%n%s%n%n%b';
if ($n === null) $n = '';
else $n = ' -'.$n;
$cmd = sprintf('GIT_DIR=%s git log%s --date=iso --pretty=format:\'%s\' %s',
escapeshellarg($this->repo), $n, $format, $tree);
$out = array();
exec($cmd, &$out);
return self::parseLog($out, 4);
}
/**
* Parse the log lines of a --pretty=medium log output.
*
* @param array Lines.
* @param int Number of lines in the headers (3)
* @return array Change log.
*/
public static function parseLog($lines, $hdrs=3)
{
$res = array();
$c = array();
$i = 0;
$hdrs += 2;
foreach ($lines as $line) {
$i++;
if (0 === strpos($line, 'commit')) {
if (count($c) > 0) {
$c['full_message'] = trim($c['full_message']);
$res[] = (object) $c;
}
$c = array();
$c['commit'] = trim(substr($line, 7));
$c['full_message'] = '';
$i=1;
continue;
}
if ($i == $hdrs) {
$c['title'] = trim($line);
continue;
}
$match = array();
if (preg_match('/(\S+)\s*:\s*(.*)/', $line, $match)) {
$match[1] = strtolower($match[1]);
$c[$match[1]] = trim($match[2]);
if ($match[1] == 'date') {
$c['date'] = gmdate('Y-m-d H:i:s', strtotime($match[2]));
}
continue;
}
if ($i > ($hdrs+1)) {
$c['full_message'] .= trim($line)."\n";
continue;
}
}
$c['full_message'] = trim($c['full_message']);
$res[] = (object) $c;
return $res;
}
} }

View File

@ -31,8 +31,22 @@ Pluf::loadFunction('Pluf_Shortcuts_GetFormForModel');
*/ */
class IDF_Views_Source class IDF_Views_Source
{ {
public function index($request, $match) public function changeLog($request, $match)
{ {
$title = sprintf('%s Git Change Log', (string) $request->project);
$git = new IDF_Git(Pluf::f('git_repository'));
$branches = $git->getBranches();
$commit = $match[2];
$res = $git->getChangeLog($commit, 50);
return Pluf_Shortcuts_RenderToResponse('source/changelog.html',
array(
'page_title' => $title,
'title' => $title,
'changes' => $res,
'commit' => $commit,
'branches' => $branches,
),
$request);
} }
public function treeBase($request, $match) public function treeBase($request, $match)
@ -41,13 +55,22 @@ class IDF_Views_Source
$git = new IDF_Git(Pluf::f('git_repository')); $git = new IDF_Git(Pluf::f('git_repository'));
$branches = $git->getBranches(); $branches = $git->getBranches();
$res = $git->filesInTree($match[2]); $res = $git->filesInTree($match[2]);
$commit = $match[2]; $tree = $match[2];
$cobject = '';
$tree_in = in_array($tree, $branches);
foreach ($git->getChangeLog('', null) as $change) {
if ($change->tree == $tree) {
$cobject = $change;
}
}
return Pluf_Shortcuts_RenderToResponse('source/tree.html', return Pluf_Shortcuts_RenderToResponse('source/tree.html',
array( array(
'page_title' => $title, 'page_title' => $title,
'title' => $title, 'title' => $title,
'files' => $res, 'files' => $res,
'commit' => $commit, 'cobject' => $cobject,
'tree' => $tree,
'tree_in' => $tree_in,
'branches' => $branches, 'branches' => $branches,
), ),
$request); $request);
@ -57,37 +80,46 @@ class IDF_Views_Source
{ {
$title = sprintf('%s Git Source Tree', (string) $request->project); $title = sprintf('%s Git Source Tree', (string) $request->project);
$git = new IDF_Git(Pluf::f('git_repository')); $git = new IDF_Git(Pluf::f('git_repository'));
$branches = $git->getBranches(); $tree = $match[2];
$commit = $match[2];
$request_file = $match[3]; $request_file = $match[3];
$request_file_info = $git->getFileInfo($request_file); $request_file_info = $git->getFileInfo($request_file, $tree);
if (!$request_file_info) throw new Pluf_HTTP_Error404(); if (!$request_file_info) throw new Pluf_HTTP_Error404();
$bc = self::makeBreadCrumb($request->project, $commit, $request_file_info->file);
$page_title = $bc.' - '.$title;
if ($request_file_info->type != 'tree') { if ($request_file_info->type != 'tree') {
return new Pluf_HTTP_Response($git->getBlob($request_file_info->hash), return new Pluf_HTTP_Response($git->getBlob($request_file_info->hash),
'application/octet-stream'); 'application/octet-stream');
} }
$res = $git->filesInTree($commit, $request_file_info); $bc = self::makeBreadCrumb($request->project, $tree, $request_file_info->file);
$page_title = $bc.' - '.$title;
$branches = $git->getBranches();
$cobject = '';
$tree_in = in_array($tree, $branches);
$res = $git->filesInTree($tree, $request_file_info);
// try to find the previous level if it exists. // try to find the previous level if it exists.
$prev = split('/', $request_file); $prev = split('/', $request_file);
$l = array_pop($prev); $l = array_pop($prev);
$previous = substr($request_file, 0, -strlen($l.' ')); $previous = substr($request_file, 0, -strlen($l.' '));
foreach ($git->getChangeLog('', null) as $change) {
if ($change->tree == $tree) {
$cobject = $change; //$git->getCommit($tree);
}
}
return Pluf_Shortcuts_RenderToResponse('source/tree.html', return Pluf_Shortcuts_RenderToResponse('source/tree.html',
array( array(
'page_title' => $page_title, 'page_title' => $page_title,
'title' => $title, 'title' => $title,
'breadcrumb' => $bc, 'breadcrumb' => $bc,
'files' => $res, 'files' => $res,
'commit' => $commit, 'tree' => $tree,
'cobject' => $cobject,
'base' => $request_file_info->file, 'base' => $request_file_info->file,
'prev' => $previous, 'prev' => $previous,
'tree_in' => $tree_in,
'branches' => $branches, 'branches' => $branches,
), ),
$request); $request);
} }
public static function makeBreadCrumb($project, $commit, $file, $sep='/') public static function makeBreadCrumb($project, $tree, $file, $sep='/')
{ {
$elts = split('/', $file); $elts = split('/', $file);
$out = array(); $out = array();
@ -97,7 +129,7 @@ class IDF_Views_Source
$stack .= ($i==0) ? $elt : '/'.$elt; $stack .= ($i==0) ? $elt : '/'.$elt;
$url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::tree', $url = Pluf_HTTP_URL_urlForView('IDF_Views_Source::tree',
array($project->shortname, array($project->shortname,
$commit, $stack)); $tree, $stack));
$out[] = '<a href="'.$url.'">'.Pluf_esc($elt).'</a>'; $out[] = '<a href="'.$url.'">'.Pluf_esc($elt).'</a>';
$i++; $i++;
} }

View File

@ -110,6 +110,12 @@ $ctl[] = array('regex' => '#^/p/(\w+)/source/tree/(\w+)/(.*)$#',
'model' => 'IDF_Views_Source', 'model' => 'IDF_Views_Source',
'method' => 'tree'); 'method' => 'tree');
$ctl[] = array('regex' => '#^/p/(\w+)/source/changes/(\w+)/$#',
'base' => $base,
'priority' => 4,
'model' => 'IDF_Views_Source',
'method' => 'changeLog');
// ---------- ADMIN -------------------------------------- // ---------- ADMIN --------------------------------------

View File

@ -43,7 +43,7 @@
{if $project} {if $project}
{* <a href="{url 'IDF_Views_Project::home', array($project.shortname)}"{block tabhome}{/block}>{trans 'Project Home'}</a> *} {* <a href="{url 'IDF_Views_Project::home', array($project.shortname)}"{block tabhome}{/block}>{trans 'Project Home'}</a> *}
<a href="{url 'IDF_Views_Issue::index', array($project.shortname)}"{block tabissues}{/block}>{trans 'Issues'}</a> <a href="{url 'IDF_Views_Issue::index', array($project.shortname)}"{block tabissues}{/block}>{trans 'Issues'}</a>
<a href="{url 'IDF_Views_Source::index', array($project.shortname)}"{block tabsource}{/block}>{trans 'Source'}</a> <a href="{url 'IDF_Views_Source::treeBase', array($project.shortname, 'master')}"{block tabsource}{/block}>{trans 'Source'}</a>
{if $isOwner} {if $isOwner}
<a href="{url 'IDF_Views_Project::admin', array($project.shortname)}"{block tabadmin}{/block}>{trans 'Administer'}</a>{/if}{/if} <a href="{url 'IDF_Views_Project::admin', array($project.shortname)}"{block tabadmin}{/block}>{trans 'Administer'}</a>{/if}{/if}
</div> </div>

View File

@ -2,7 +2,8 @@
{block tabsource} class="active"{/block} {block tabsource} class="active"{/block}
{block subtabs} {block subtabs}
<div id="sub-tabs"> <div id="sub-tabs">
{trans 'Source Tree'} {trans 'Source Tree'} |
<a href="{url 'IDF_Views_Source::changeLog', array($project.shortname, 'master')}">{trans 'Change Log'}</a>
</div> </div>
{/block} {/block}
{block title}{$title}{/block} {block title}{$title}{/block}

View File

@ -0,0 +1,34 @@
{extends "source/base.html"}
{block docclass}yui-t1{/block}
{block body}
<table summary="" class="tree-list">
<thead>
<tr>
<th>{trans 'Age'}</th>
<th>{trans 'Message'}</th>
<th>{trans 'Details'}</th>
</tr>
</thead>
<tbody>
{foreach $changes as $change}
{aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $change.tree)}
<tr>
<td><a href="{$url}">{$change.date|dateago:"wihtout"}</a></td>
<td>{$change.title}{if $change.full_message}<br /><span class="smaller">{$change.full_message}</span>{/if}</td>
<td><span class="smaller">{trans 'Tree:'}&nbsp;<a href="{$url}">{$change.tree}</a><br />
{trans 'By:'} {$change.author|strip_tags} {* this remove the email address *}
</span></td>
</tr>
{/foreach}
</tbody>
</table>
{/block}
{block context}
<p><strong>{trans 'Branches:'}</strong><br />
{foreach $branches as $branch}
{aurl 'url', 'IDF_Views_Source::changeLog', array($project.shortname, $branch)}
<span class="label{if $commit == $branch} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br />
{/foreach}
</p>
{/block}

View File

@ -1,7 +1,7 @@
{extends "source/base.html"} {extends "source/base.html"}
{block docclass}yui-t1{/block} {block docclass}yui-t1{/block}
{block body} {block body}
<h2><a href="{url 'IDF_Views_Source::treeBase', array($project.shortname, $commit)}">{trans 'Root'}</a><span class="sep">/</span>{if $breadcrumb}{$breadcrumb|safe}{/if}</h2> <h2><a href="{url 'IDF_Views_Source::treeBase', array($project.shortname, $tree)}">{trans 'Root'}</a><span class="sep">/</span>{if $breadcrumb}{$breadcrumb|safe}{/if}</h2>
<table summary="" class="tree-list"> <table summary="" class="tree-list">
<thead> <thead>
<tr> <tr>
@ -10,23 +10,27 @@
<th>{trans 'Message'}</th> <th>{trans 'Message'}</th>
<th>{trans 'Size'}</th> <th>{trans 'Size'}</th>
</tr> </tr>
</thead> </thead>{if !$tree_in}
<tbody> {aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $tree)}
<tfoot>
<tr><th colspan="5">{blocktrans}In tree <a href="{$url}">{$tree}</a> created {$cobject.date|dateago}.{/blocktrans}</th></tr>
</tfoot>
{/if}<tbody>
{if $base} {if $base}
<tr> <tr>
<td>&nbsp;</td> <td>&nbsp;</td>
<td colspan="4"> <td colspan="4">
<a href="{url 'IDF_Views_Source::tree', array($project.shortname, $commit, $prev)}">..</a></td> <a href="{url 'IDF_Views_Source::tree', array($project.shortname, $tree, $prev)}">..</a></td>
</tr> </tr>
{/if} {/if}
{foreach $files as $file} {foreach $files as $file}
{aurl 'url', 'IDF_Views_Source::tree', array($project.shortname, $commit, $file.fullpath)} {aurl 'url', 'IDF_Views_Source::tree', array($project.shortname, $tree, $file.fullpath)}
<tr> <tr>
<td><img src="{media '/idf/img/'~$file.type~'.png'}" alt="{$file.type}" /></td> <td><img src="{media '/idf/img/'~$file.type~'.png'}" alt="{$file.type}" /></td>
<td><a href="{$url}">{$file.file}</a></td> <td{if $file.type != 'blob'} colspan="4"{/if}><a href="{$url}">{$file.file}</a></td>
<td><span class="smaller">{$file.time|timeago:"wihtout"}</span></td>
<td{if $file.type != 'blob'} colspan="2"{/if}><span class="smaller">{$file.log}</span></td>
{if $file.type == 'blob'} {if $file.type == 'blob'}
<td><span class="smaller">{$file.date|dateago:"wihtout"}</span></td>
<td><span class="smaller">{$file.log}</span></td>
<td>{$file.size|size}</td>{/if} <td>{$file.size|size}</td>{/if}
</tr> </tr>
{/foreach} {/foreach}
@ -38,7 +42,7 @@
<p><strong>{trans 'Branches:'}</strong><br /> <p><strong>{trans 'Branches:'}</strong><br />
{foreach $branches as $branch} {foreach $branches as $branch}
{aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $branch)} {aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $branch)}
<span class="label{if $commit == $branch} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br /> <span class="label{if $tree == $branch} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br />
{/foreach} {/foreach}
</p> </p>
{/block} {/block}

View File

@ -273,6 +273,16 @@ table.tree-list td {
vertical-align: top; vertical-align: top;
} }
table.tree-list tfoot th {
text-align: right;
font-weight: normal;
}
table.recent-issues tfoot th a {
color: #000;
font-weight: normal;
}
/** /**