Improved the support of the git browser.

Now the reference is always a commit as from a commit it is always possible to travel back to the corresponding tree and blobs.
This commit is contained in:
Loic d'Anterroches 2008-07-27 17:43:51 +02:00
parent 9a2b8e249a
commit b6084cbf07
6 changed files with 185 additions and 113 deletions

View File

@ -23,92 +23,143 @@
/** /**
* Git utils. * Git utils.
*
*/ */
class IDF_Git class IDF_Git
{ {
public $repo = ''; public $repo = '';
public $mediumtree_fmt = 'commit %H%nAuthor: %an <%ae>%nTree: %T%nDate: %ai%n%n%s%n%n%b';
public function __construct($repo) public function __construct($repo)
{ {
$this->repo = $repo; $this->repo = $repo;
} }
/** /**
* Given a commit hash (or a branch) returns an array of files in * Test a given object hash.
* it. *
* @param string Object hash.
* @return mixed false if not valid or 'blob', 'tree', 'commit'
*/
public function testHash($hash)
{
$cmd = sprintf('GIT_DIR=%s git cat-file -t %s',
escapeshellarg($this->repo),
escapeshellarg($hash));
$ret = 0; $out = array();
exec($cmd, &$out, &$ret);
if ($ret != 0) return false;
return trim($out[0]);
}
/**
* Given a commit hash returns an array of files in it.
* *
* A file is a class with the following properties: * A file is a class with the following properties:
* *
* 'perm', 'type', 'size', 'hash', 'file' * 'perm', 'type', 'size', 'hash', 'file'
* *
* @param string Tree ('HEAD') * @param string Commit ('HEAD')
* @param string Base folder ('') * @param string Base folder ('')
* @return array * @return array
*/ */
public function filesInTree($tree='HEAD', $basefolder='') public function filesAtCommit($commit='HEAD', $folder='')
{ {
if (is_object($basefolder)) { if ('commit' != $this->testHash($commit)) {
$base = $basefolder; throw new Exception(sprintf(__('Not a valid commit: %s.'), $commit));
} else if (
$basefolder != ''
and
(
(false === ($base=$this->getFileInfo($basefolder, $tree)))
or
($base->type != 'tree')
)) {
throw new Exception(sprintf('Base folder "%s" not found.', $basefolder));
} else {
// no base
$base = (object) array('file' => '',
'hash' => $tree);
} }
// now we grab the info about this commit including its tree.
$res = array(); $co = $this->getCommit($commit);
$out = array(); if ($folder) {
$cmd = sprintf('GIT_DIR=%s git-ls-tree -t -l %s', $this->repo, $base->hash); // As we are limiting to a given folder, we need to find
exec($cmd, &$out); // the tree corresponding to this folder.
$rawlog = array(); $found = false;
foreach ($this->getBranches() as $br) { foreach ($this->getTreeInfo($co->tree) as $file) {
$cmd = sprintf('GIT_DIR=%s git log --raw --abbrev=40 --pretty=oneline %s', if ($file->type == 'tree' and $file->file == $folder) {
$this->repo, $br); $found = true;
exec($cmd, &$rawlog); $tree = $file->hash;
} break;
$rawlog = implode("\n", array_reverse($rawlog)); }
$current_dir = getcwd();
chdir(substr($this->repo, 0, -5));
foreach ($out as $line) {
list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY);
$matches = array();
$date = '1970-01-01 12:00:00';
$log = '';
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, if (!$found) {
'size' => $size, 'hash' => $hash, throw new Exception(sprintf(__('Folder %1$s not found in commit %2$s.'), $folder, $commit));
'fullpath' => ($base->file) ? $base->file.'/'.$file : $file, }
'log' => $log, 'date' => $date, } else {
'file' => $file); $tree = $co->tree;
}
$res = array();
// get the raw log corresponding to this commit to find the
// origin of each file.
$rawlog = array();
$cmd = sprintf('GIT_DIR=%s git log --raw --abbrev=40 --pretty=oneline %s',
escapeshellarg($this->repo), escapeshellarg($commit));
exec($cmd, &$rawlog);
// We reverse the log to be able to use a fixed efficient
// regex without back tracking.
$rawlog = implode("\n", array_reverse($rawlog));
foreach ($this->getTreeInfo($tree, false) as $file) {
// Now we grab the files in the current tree with as much
// information as possible.
$matches = array();
if ($file->type == 'blob' and preg_match('/^\:\d{6} \d{6} [0-9a-f]{40} '.$file->hash.' .*^([0-9a-f]{40})/msU',
$rawlog, &$matches)) {
$fc = $this->getCommit($matches[1]);
$file->date = $fc->date;
$file->log = $fc->title;
} else if ($file->type == 'blob') {
$file->date = $co->date;
$file->log = $co->title;
}
$file->fullpath = ($folder) ? $folder.'/'.$file->file : $file->file;
$res[] = $file;
} }
chdir($current_dir);
return $res; return $res;
} }
/**
* Get the tree info.
*
* @param string Tree hash
* @param bool Do we recurse in subtrees (true)
* @return array Array of file information.
*/
public function getTreeInfo($tree, $recurse=true)
{
if ('tree' != $this->testHash($tree)) {
throw new Exception(sprintf(__('Not a valid tree: %s.'), $tree));
}
$cmd_tmpl = 'GIT_DIR=%s git-ls-tree%s -t -l %s';
$cmd = sprintf($cmd_tmpl,
escapeshellarg($this->repo),
($recurse) ? ' -r' : '',
escapeshellarg($tree));
$out = array();
$res = array();
exec($cmd, &$out);
foreach ($out as $line) {
list($perm, $type, $hash, $size, $file) = preg_split('/ |\t/', $line, 5, PREG_SPLIT_NO_EMPTY);
$res[] = (object) array('perm' => $perm, 'type' => $type,
'size' => $size, 'hash' => $hash,
'file' => $file);
}
return $res;
}
/** /**
* Get the file info. * Get the file info.
* *
* @param string File * @param string File
* @param string Tree ('HEAD') * @param string Commit ('HEAD')
* @return false Information * @return false Information
*/ */
public function getFileInfo($totest, $tree='HEAD') public function getFileInfo($totest, $commit='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, $tree); $cmd = sprintf($cmd_tmpl,
escapeshellarg($this->repo),
escapeshellarg($commit));
$out = array(); $out = array();
exec($cmd, &$out); exec($cmd, &$out);
foreach ($out as $line) { foreach ($out as $line) {
@ -131,7 +182,8 @@ class IDF_Git
public function getBlob($hash) public function getBlob($hash)
{ {
return shell_exec(sprintf('GIT_DIR=%s git-cat-file blob %s', return shell_exec(sprintf('GIT_DIR=%s git-cat-file blob %s',
$this->repo, $hash)); escapeshellarg($this->repo),
escapeshellarg($hash)));
} }
/** /**
@ -142,7 +194,8 @@ class IDF_Git
public function getBranches() public function getBranches()
{ {
$out = array(); $out = array();
exec(sprintf('GIT_DIR=%s git branch', $this->repo), &$out); exec(sprintf('GIT_DIR=%s git branch',
escapeshellarg($this->repo)), &$out);
$res = array(); $res = array();
foreach ($out as $b) { foreach ($out as $b) {
$res[] = substr($b, 2); $res[] = substr($b, 2);
@ -158,8 +211,10 @@ class IDF_Git
*/ */
public function getCommit($commit='HEAD') public function getCommit($commit='HEAD')
{ {
$cmd = sprintf('GIT_DIR=%s git show --date=iso --pretty=medium %s', $cmd = sprintf('GIT_DIR=%s git show --date=iso --pretty=format:%s %s',
escapeshellarg($this->repo), $commit); escapeshellarg($this->repo),
"'".$this->mediumtree_fmt."'",
escapeshellarg($commit);
$out = array(); $out = array();
exec($cmd, &$out); exec($cmd, &$out);
$log = array(); $log = array();
@ -175,7 +230,7 @@ class IDF_Git
$log[] = $line; $log[] = $line;
} }
} }
$out = self::parseLog($log); $out = self::parseLog($log, 4);
$out[0]->changes = $change; $out[0]->changes = $change;
return $out[0]; return $out[0];
} }
@ -184,20 +239,19 @@ class IDF_Git
/** /**
* Get latest changes. * Get latest changes.
* *
* @param string Tree ('HEAD'). * @param string Commit ('HEAD').
* @param int Number of changes (10). * @param int Number of changes (10).
* @return array Changes. * @return array Changes.
*/ */
public function getChangeLog($tree='HEAD', $n=10) public function getChangeLog($commit='HEAD', $n=10)
{ {
$format = 'commit %H%nAuthor: %an <%ae>%nTree: %T%nDate: %ai%n%n%s%n%n%b';
if ($n === null) $n = ''; if ($n === null) $n = '';
else $n = ' -'.$n; else $n = ' -'.$n;
$cmd = sprintf('GIT_DIR=%s git log%s --date=iso --pretty=format:\'%s\' %s', $cmd = sprintf('GIT_DIR=%s git log%s --date=iso --pretty=format:\'%s\' %s',
escapeshellarg($this->repo), $n, $format, $tree); escapeshellarg($this->repo), $n, $this->mediumtree_fmt,
escapeshellarg($commit));
$out = array(); $out = array();
exec($cmd, &$out); exec($cmd, &$out);
//print_r($cmd);
return self::parseLog($out, 4); return self::parseLog($out, 4);
} }

View File

@ -53,26 +53,18 @@ 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'));
$commit = $match[2];
$branches = $git->getBranches(); $branches = $git->getBranches();
$res = $git->filesInTree($match[2]); $res = $git->filesAtCommit($commit);
$tree = $match[2]; $cobject = $git->getCommit($commit);
$cobject = ''; $tree_in = in_array($commit, $branches);
$tree_in = in_array($tree, $branches);
foreach ($branches as $br) {
foreach ($git->getChangeLog($br, null) as $change) {
if ($change->tree == $tree) {
$cobject = $change;
break 2;
}
}
}
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,
'cobject' => $cobject, 'cobject' => $cobject,
'tree' => $tree, 'commit' => $commit,
'tree_in' => $tree_in, 'tree_in' => $tree_in,
'branches' => $branches, 'branches' => $branches,
), ),
@ -83,39 +75,31 @@ 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'));
$tree = $match[2]; $commit = $match[2];
$request_file = $match[3]; $request_file = $match[3];
$request_file_info = $git->getFileInfo($request_file, $tree); $request_file_info = $git->getFileInfo($request_file, $commit);
if (!$request_file_info) throw new Pluf_HTTP_Error404(); if (!$request_file_info) throw new Pluf_HTTP_Error404();
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');
} }
$bc = self::makeBreadCrumb($request->project, $tree, $request_file_info->file); $bc = self::makeBreadCrumb($request->project, $commit, $request_file_info->file);
$page_title = $bc.' - '.$title; $page_title = $bc.' - '.$title;
$branches = $git->getBranches(); $branches = $git->getBranches();
$cobject = ''; $cobject = $git->getCommit();
$tree_in = in_array($tree, $branches); $tree_in = in_array($commit, $branches);
$res = $git->filesInTree($tree, $request_file_info); $res = $git->filesAtCommit($commit, $request_file);
// 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 ($branches as $br) {
foreach ($git->getChangeLog($br, null) as $change) {
if ($change->tree == $tree) {
$cobject = $change;
break 2;
}
}
}
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,
'tree' => $tree, 'commit' => $commit,
'cobject' => $cobject, 'cobject' => $cobject,
'base' => $request_file_info->file, 'base' => $request_file_info->file,
'prev' => $previous, 'prev' => $previous,
@ -125,7 +109,7 @@ class IDF_Views_Source
$request); $request);
} }
public static function makeBreadCrumb($project, $tree, $file, $sep='/') public static function makeBreadCrumb($project, $commit, $file, $sep='/')
{ {
$elts = split('/', $file); $elts = split('/', $file);
$out = array(); $out = array();
@ -135,7 +119,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,
$tree, $stack)); $commit, $stack));
$out[] = '<a href="'.$url.'">'.Pluf_esc($elt).'</a>'; $out[] = '<a href="'.$url.'">'.Pluf_esc($elt).'</a>';
$i++; $i++;
} }

View File

@ -2,8 +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'} | <a {if $inSourceTree}class="active" {/if}href="{url 'IDF_Views_Source::treeBase', array($project.shortname, 'master')}">{trans 'Source Tree'}</a> |
<a href="{url 'IDF_Views_Source::changeLog', array($project.shortname, 'master')}">{trans 'Change Log'}</a> <a {if $inChangeLog}class="active" {/if}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

@ -1,23 +1,31 @@
{extends "source/base.html"} {extends "source/base.html"}
{block docclass}yui-t1{/block} {block docclass}yui-t1{assign $inChangeLog=true}{/block}
{block body} {block body}
<table summary="" class="tree-list"> <table summary="" class="tree-list">
<thead> <thead>
<tr> <tr>
<th>{trans 'Age'}</th> <th>{trans 'Age'}</th>
<th>{trans 'Message'}</th> <th>{trans 'Message'}</th>
<th>{trans 'Details'}</th> {* <th>{trans 'Details'}</th> *}
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{foreach $changes as $change} {foreach $changes as $change}
{aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $change.tree)} {aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $change.commit)}
<tr> <tr class="log">
<td><a href="{$url}">{$change.date|dateago:"wihtout"}</a></td> <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>{$change.title}{if $change.full_message}<br /><br />{$change.full_message}{/if}</td>
<td><span class="smaller">{trans 'Tree:'}&nbsp;<a href="{$url}">{$change.tree}</a><br /> </tr>
{trans 'By:'} {$change.author|strip_tags} {* this remove the email address *} <tr class="extra">
</span></td> <td colspan="2">
<div class="helptext right">{trans 'Commit:'}&nbsp;<a href="{$url}" class="mono">{$change.commit}</a>,
{trans 'by'} {$change.author|strip_tags} {* this remove the email address *}
</div>
</td>
{* <td><span class="smaller">{trans 'Tree:'}&nbsp;<a href="{$url}">{$change.commit}</a><br />
{trans 'By:'} {$change.author|strip_tags}
</span></td> *}
</tr> </tr>
{/foreach} {/foreach}
</tbody> </tbody>

View File

@ -1,7 +1,7 @@
{extends "source/base.html"} {extends "source/base.html"}
{block docclass}yui-t1{/block} {block docclass}yui-t1{assign $inSourceTree=true}{/block}
{block body} {block body}
<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> <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>
<table summary="" class="tree-list"> <table summary="" class="tree-list">
<thead> <thead>
<tr> <tr>
@ -11,26 +11,31 @@
<th>{trans 'Size'}</th> <th>{trans 'Size'}</th>
</tr> </tr>
</thead>{if !$tree_in} </thead>{if !$tree_in}
{aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $tree)} {aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $commit)}
<tfoot> <tfoot>
<tr><th colspan="5">{blocktrans}In tree <a href="{$url}">{$tree}</a> created {$cobject.date|dateago}.{/blocktrans}</th></tr> <tr><th colspan="5">{blocktrans}In commit <a class="mono" href="{$url}">{$commit}</a> created {$cobject.date|dateago}.{/blocktrans}<br />
<span class="smaller">{blocktrans}By {$cobject.author|strip_tags|trim}, {$cobject.title}{/blocktrans}</span>
</th></tr>
</tfoot> </tfoot>
{/if}<tbody> {/if}<tbody>
{if $base} {if $base}
<tr> <tr>
<td>&nbsp;</td> <td>&nbsp;</td>
<td colspan="4"> <td>
<a href="{url 'IDF_Views_Source::tree', array($project.shortname, $tree, $prev)}">..</a></td> <a href="{url 'IDF_Views_Source::tree', array($project.shortname, $commit, $prev)}">..</a></td>
<td colspan="3"></td>
</tr> </tr>
{/if} {/if}
{foreach $files as $file} {foreach $files as $file}
{aurl 'url', 'IDF_Views_Source::tree', array($project.shortname, $tree, $file.fullpath)} {aurl 'url', 'IDF_Views_Source::tree', array($project.shortname, $commit, $file.fullpath)}
<tr> <tr>
<td><img src="{media '/idf/img/'~$file.type~'.png'}" alt="{$file.type}" /></td> <td class="fileicon"><img src="{media '/idf/img/'~$file.type~'.png'}" alt="{$file.type}" /></td>
<td{if $file.type != 'blob'} colspan="4"{/if}><a href="{$url}">{$file.file}</a></td> <td{if $file.type != 'blob'} colspan="4"{/if}><a href="{$url}">{$file.file}</a></td>
{if $file.type == 'blob'} {if $file.type == 'blob'}
{if isset($file.date)}
<td><span class="smaller">{$file.date|dateago:"wihtout"}</span></td> <td><span class="smaller">{$file.date|dateago:"wihtout"}</span></td>
<td><span class="smaller">{$file.log}</span></td> <td><span class="smaller">{$file.log}</span></td>
{else}<td colspan="2"></td>{/if}
<td>{$file.size|size}</td>{/if} <td>{$file.size|size}</td>{/if}
</tr> </tr>
{/foreach} {/foreach}
@ -42,7 +47,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 $tree == $branch} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br /> <span class="label{if $commit == $branch} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br />
{/foreach} {/foreach}
</p> </p>
{/block} {/block}

View File

@ -23,6 +23,14 @@
padding: 0 1em; padding: 0 1em;
} }
.right {
text-align: right;
}
.mono {
font-family: monospace;
}
div.context { div.context {
padding-left: 1em; padding-left: 1em;
} }
@ -259,7 +267,7 @@ table.tree-list {
table.tree-list th { table.tree-list th {
background-color: #e4e8E0; background-color: #e4e8E0;
vertical-align: top; vertical-align: top;
border-color: #d3d7cf; border-color: #d3d7cf;
} }
table.tree-list tr { table.tree-list tr {
@ -278,12 +286,25 @@ table.tree-list tfoot th {
font-weight: normal; font-weight: normal;
} }
table.recent-issues tfoot th a { table.tree-list tfoot th a {
color: #000; color: #000;
font-weight: normal; font-weight: normal;
} }
table.tree-list tr.log {
border-bottom: 1px solid #e7ebe3;
/* background-color: #eef2ea !important; */
}
table.tree-list tr.extra {
/* border-bottom: 1px solid #e7ebe3; */
/* background-color: #eef2ea !important; */
}
table td.fileicon {
width: 20px;
}
/** /**
* Autocomplete. * Autocomplete.