Improved the Mercurial backend.

This commit is contained in:
Loic d'Anterroches 2009-05-25 12:16:34 +02:00
parent a3f40447c0
commit f55769a946
4 changed files with 78 additions and 86 deletions

View File

@ -25,22 +25,19 @@
* Mercurial utils. * Mercurial utils.
* *
*/ */
class IDF_Scm_Mercurial class IDF_Scm_Mercurial extends IDF_Scm
{ {
public $repo = ''; public static function factory($project)
public function __construct($repo)
{ {
$this->repo = $repo; $rep = sprintf(Pluf::f('mercurial_repositories'), $project->shortname);
return new IDF_Scm_Mercurial($rep, $project);
}
public function isAvailable()
{
return true;
} }
/**
* Given the string describing the author from the log find the
* author in the database.
*
* @param string Author
* @return mixed Pluf_User or null
*/
public function findAuthor($author) public function findAuthor($author)
{ {
// We extract the email. // We extract the email.
@ -53,27 +50,28 @@ class IDF_Scm_Mercurial
return ($users->count() > 0) ? $users[0] : null; return ($users->count() > 0) ? $users[0] : null;
} }
/** public function getMainBranch()
* Returns the URL of the git daemon. {
* return 'tip';
* @param IDF_Project }
* @return string URL
*/ public static function getAnonymousAccessUrl($project)
public static function getRemoteAccessUrl($project)
{ {
return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname); return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname);
} }
/** public static function getAuthAccessUrl($project, $user)
* Returns this object correctly initialized for the project.
*
* @param IDF_Project
* @return IDF_Scm_Git
*/
public static function factory($project)
{ {
$rep = sprintf(Pluf::f('mercurial_repositories'), $project->shortname); return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname);
return new IDF_Scm_Mercurial($rep); }
public function isValidRevision($rev)
{
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -r %s',
escapeshellarg($this->repo),
escapeshellarg($rev));
exec($cmd, $out, $ret);
return ($ret == 0);
} }
/** /**
@ -90,24 +88,14 @@ class IDF_Scm_Mercurial
escapeshellarg($hash)); escapeshellarg($hash));
$ret = 0; $ret = 0;
$out = array(); $out = array();
IDF_Scm::exec($cmd, $out, $ret); exec($cmd, $out, $ret);
return ($ret != 0) ? false : 'commit'; return ($ret != 0) ? false : 'commit';
} }
/** public function getTree($commit, $folder='/', $branch=null)
* Given a commit hash returns an array of files in it.
*
* A file is a class with the following properties:
*
* 'perm', 'type', 'size', 'hash', 'file'
*
* @param string Commit ('HEAD')
* @param string Base folder ('')
* @return array
*/
public function filesAtCommit($commit='tip', $folder='')
{ {
// now we grab the info about this commit including its tree. // now we grab the info about this commit including its tree.
$folder = ($folder == '/') ? '' : $folder;
$co = $this->getCommit($commit); $co = $this->getCommit($commit);
if ($folder) { if ($folder) {
// As we are limiting to a given folder, we need to find // As we are limiting to a given folder, we need to find
@ -143,29 +131,23 @@ class IDF_Scm_Mercurial
$cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo), $tree, ($recurse) ? '' : ''); $cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo), $tree, ($recurse) ? '' : '');
$out = array(); $out = array();
$res = array(); $res = array();
IDF_Scm::exec($cmd, $out); exec($cmd, $out);
$out_hack = array();
$tmp_hack = array(); $tmp_hack = array();
foreach ($out as $line) { while (null !== ($line = array_pop($out))) {
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4); list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
$file = trim($file); $file = trim($file);
$dir = explode('/', $file, -1); $dir = explode('/', $file, -1);
$tmp = ''; $tmp = '';
for ($i=0; $i < count($dir); $i++) { for ($i=0, $n=count($dir); $i<$n; $i++) {
if ($i > 0) { if ($i > 0) {
$tmp .= '/'; $tmp .= '/';
} }
$tmp .= $dir[$i]; $tmp .= $dir[$i];
if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) { if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) {
$out_hack[] = "empty\t000\t\t$tmp/"; $out[] = "empty\t000\t\t$tmp/";
$tmp_hack["empty\t000\t\t$tmp/"] = 1; $tmp_hack["empty\t000\t\t$tmp/"] = 1;
} }
} }
$out_hack[] = "$hash\t$perm\t$exec\t$file";
}
foreach ($out_hack as $line) {
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
$file = trim($file);
if (preg_match('/^(.*)\/$/', $file, $match)) { if (preg_match('/^(.*)\/$/', $file, $match)) {
$type = 'tree'; $type = 'tree';
$file = $match[1]; $file = $match[1];
@ -190,47 +172,52 @@ class IDF_Scm_Mercurial
return $res; return $res;
} }
/** public function getPathInfo($totest, $commit='tip')
* Get the file info.
*
* @param string Commit ('HEAD')
* @return false Information
*/
public function getFileInfo($totest, $commit='tip')
{ {
$cmd_tmpl = Pluf::f('hg_path', 'hg').' manifest -R %s --debug -r %s'; $cmd_tmpl = Pluf::f('hg_path', 'hg').' manifest -R %s --debug -r %s';
$cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo), $commit); $cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo), $commit);
$out = array(); $out = array();
$res = array(); exec($cmd, $out);
IDF_Scm::exec($cmd, $out); $tmp_hack = array();
$out_hack = array(); while (null !== ($line = array_pop($out))) {
$tmp_hack =array();
foreach ($out as $line) {
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4); list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
$file = trim($file); $file = trim($file);
$dir = explode('/', $file, -1); $dir = explode('/', $file, -1);
$tmp = ''; $tmp = '';
for ($i=0; $i < count($dir); $i++) { for ($i=0, $n=count($dir); $i<$n; $i++) {
if ($i > 0) { if ($i > 0) {
$tmp .= '/'; $tmp .= '/';
} }
$tmp .= $dir[$i]; $tmp .= $dir[$i];
if ($tmp == $totest) {
return (object) array('perm' => '000', 'type' => 'tree',
'hash' => $hash,
'file' => $totest,
'commit' => $commit
);
}
if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) { if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) {
$out_hack[] = "empty\t000\t\t$tmp/"; $out[] = "empty\t000\t\t$tmp/";
$tmp_hack["empty\t000\t\t$tmp/"] = 1; $tmp_hack["empty\t000\t\t$tmp/"] = 1;
} }
} }
$out_hack[] = "$hash\t$perm\t$exec\t$file";
}
foreach ($out_hack as $line) {
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
$file = trim ($file);
if (preg_match('/^(.*)\/$/', $file, $match)) { if (preg_match('/^(.*)\/$/', $file, $match)) {
$type = 'tree'; $type = 'tree';
$file = $match[1]; $file = $match[1];
} else { } else {
$type = 'blob'; $type = 'blob';
} }
if (!$root and !$folder and preg_match('/^.*\/.*$/', $file)) {
continue;
}
if ($folder) {
preg_match('|^'.$folder.'[/]?([^/]+)?$|', $file,$match);
if (count($match) > 1) {
$file = $match[1];
} else {
continue;
}
}
if ($totest == $file) { if ($totest == $file) {
return (object) array('perm' => $perm, 'type' => $type, return (object) array('perm' => $perm, 'type' => $type,
'hash' => $hash, 'hash' => $hash,
@ -251,7 +238,7 @@ class IDF_Scm_Mercurial
*/ */
public function getBlob($request_file_info, $dummy=null) public function getBlob($request_file_info, $dummy=null)
{ {
return IDF_Scm::shell_exec(sprintf(Pluf::f('hg_path', 'hg').' cat -R %s -r %s %s', return shell_exec(sprintf(Pluf::f('hg_path', 'hg').' cat -R %s -r %s %s',
escapeshellarg($this->repo), escapeshellarg($this->repo),
$dummy, $dummy,
escapeshellarg($this->repo . '/' . $request_file_info->file))); escapeshellarg($this->repo . '/' . $request_file_info->file)));
@ -264,17 +251,27 @@ class IDF_Scm_Mercurial
*/ */
public function getBranches() public function getBranches()
{ {
if (isset($this->cache['branches'])) {
return $this->cache['branches'];
}
$out = array(); $out = array();
IDF_Scm::exec(sprintf(Pluf::f('hg_path', 'hg').' branches -R %s', exec(sprintf(Pluf::f('hg_path', 'hg').' branches -R %s',
escapeshellarg($this->repo)), $out); escapeshellarg($this->repo)), $out);
$res = array(); $res = array();
foreach ($out as $b) { foreach ($out as $b) {
preg_match('/(\S+).*\S+:(\S+)/', $b, $match); preg_match('/(\S+).*\S+:(\S+)/', $b, $match);
$res[] = $match[1]; $res[$match[1]] = '';
} }
$this->cache['branches'] = $res;
return $res; return $res;
} }
public function inBranches($commit, $path)
{
return (in_array($commit, array_keys($this->getBranches())))
? array($commit) : array();
}
/** /**
* Get commit details. * Get commit details.
* *
@ -289,7 +286,7 @@ class IDF_Scm_Mercurial
$cmd = sprintf($tmpl, $cmd = sprintf($tmpl,
escapeshellarg($commit), escapeshellarg($this->repo)); escapeshellarg($commit), escapeshellarg($this->repo));
$out = array(); $out = array();
IDF_Scm::exec($cmd, $out); exec($cmd, $out);
$log = array(); $log = array();
$change = array(); $change = array();
$inchange = false; $inchange = false;
@ -330,7 +327,7 @@ class IDF_Scm_Mercurial
{ {
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -l%s ', escapeshellarg($this->repo), $n, $commit); $cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -l%s ', escapeshellarg($this->repo), $n, $commit);
$out = array(); $out = array();
IDF_Scm::exec($cmd, $out); exec($cmd, $out);
return self::parseLog($out, 6); return self::parseLog($out, 6);
} }

View File

@ -221,10 +221,7 @@ class IDF_Views_Source
$l = array_pop($prev); $l = array_pop($prev);
$previous = substr($request_file, 0, -strlen($l.' ')); $previous = substr($request_file, 0, -strlen($l.' '));
$scmConf = $request->conf->getVal('scm', 'git'); $scmConf = $request->conf->getVal('scm', 'git');
$props = null; $props = $scm->getProperties($commit, $request_file);
if ($scmConf === 'svn') {
$props = $scm->getProperties($commit, $request_file);
}
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/tree.html', return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/tree.html',
array( array(
'page_title' => $page_title, 'page_title' => $page_title,
@ -335,10 +332,7 @@ class IDF_Views_Source
$l = array_pop($prev); $l = array_pop($prev);
$previous = substr($request_file, 0, -strlen($l.' ')); $previous = substr($request_file, 0, -strlen($l.' '));
$scmConf = $request->conf->getVal('scm', 'git'); $scmConf = $request->conf->getVal('scm', 'git');
$props = null; $props = $scm->getProperties($commit, $request_file);
if ($scmConf === 'svn') {
$props = $scm->getProperties($commit, $request_file);
}
$content = self::highLight($extra['mime'], $scm->getFile($request_file_info)); $content = self::highLight($extra['mime'], $scm->getFile($request_file_info));
return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/file.html', return Pluf_Shortcuts_RenderToResponse('idf/source/'.$scmConf.'/file.html',
array( array(

View File

@ -54,7 +54,7 @@
{block context} {block context}
<p><strong>{trans 'Branches:'}</strong><br /> <p><strong>{trans 'Branches:'}</strong><br />
{foreach $branches as $branch => $path} {foreach $branches as $branch => $path}
{aurl 'url', 'IDF_Views_Source::tree', array($project.shortname, $branch)} {aurl 'url', 'IDF_Views_Source::treeBase', array($project.shortname, $branch)}
<span class="label{if in_array($branch, $tree_in)} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br /> <span class="label{if in_array($branch, $tree_in)} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br />
{/foreach} {/foreach}
</p> </p>

View File

@ -48,9 +48,10 @@
{block context} {block context}
<p><strong>{trans 'Branches:'}</strong><br /> <p><strong>{trans 'Branches:'}</strong><br />
{foreach $branches as $branch} {foreach $branches as $branch => $path}
{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 in_array($branch, $tree_in)} active{/if}"><a href="{$url}" class="label">{$branch}</a></span><br />
{/foreach} {/foreach}
</p> </p>
{/block} {/block}