2008-11-23 16:45:00 +00:00
|
|
|
<?php
|
|
|
|
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# This file is part of InDefero, an open source project management application.
|
|
|
|
# Copyright (C) 2008 Céondo Ltd and contributors.
|
|
|
|
#
|
|
|
|
# InDefero is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# InDefero is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mercurial utils.
|
|
|
|
*
|
|
|
|
*/
|
2009-05-25 10:16:34 +00:00
|
|
|
class IDF_Scm_Mercurial extends IDF_Scm
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
2009-05-25 10:16:34 +00:00
|
|
|
public static function factory($project)
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
2009-05-25 10:16:34 +00:00
|
|
|
$rep = sprintf(Pluf::f('mercurial_repositories'), $project->shortname);
|
|
|
|
return new IDF_Scm_Mercurial($rep, $project);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAvailable()
|
|
|
|
{
|
|
|
|
return true;
|
2008-11-23 16:45:00 +00:00
|
|
|
}
|
|
|
|
|
2008-12-05 10:34:02 +00:00
|
|
|
public function findAuthor($author)
|
|
|
|
{
|
|
|
|
// We extract the email.
|
|
|
|
$match = array();
|
|
|
|
if (!preg_match('/<(.*)>/', $author, $match)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$sql = new Pluf_SQL('email=%s', array($match[1]));
|
|
|
|
$users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen()));
|
|
|
|
return ($users->count() > 0) ? $users[0] : null;
|
|
|
|
}
|
|
|
|
|
2009-05-25 10:16:34 +00:00
|
|
|
public function getMainBranch()
|
|
|
|
{
|
|
|
|
return 'tip';
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getAnonymousAccessUrl($project)
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
2008-11-24 19:27:03 +00:00
|
|
|
return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname);
|
2008-11-23 16:45:00 +00:00
|
|
|
}
|
|
|
|
|
2009-05-25 10:16:34 +00:00
|
|
|
public static function getAuthAccessUrl($project, $user)
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
2009-05-25 10:16:34 +00:00
|
|
|
return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2008-11-23 16:45:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a given object hash.
|
|
|
|
*
|
|
|
|
* @param string Object hash.
|
|
|
|
* @param null to be svn client compatible
|
|
|
|
* @return mixed false if not valid or 'blob', 'tree', 'commit'
|
|
|
|
*/
|
|
|
|
public function testHash($hash, $dummy=null)
|
|
|
|
{
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -r %s',
|
2008-11-23 16:45:00 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($hash));
|
|
|
|
$ret = 0;
|
|
|
|
$out = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
exec($cmd, $out, $ret);
|
2008-11-23 16:45:00 +00:00
|
|
|
return ($ret != 0) ? false : 'commit';
|
|
|
|
}
|
|
|
|
|
2009-05-25 10:16:34 +00:00
|
|
|
public function getTree($commit, $folder='/', $branch=null)
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
|
|
|
// now we grab the info about this commit including its tree.
|
2009-05-25 10:16:34 +00:00
|
|
|
$folder = ($folder == '/') ? '' : $folder;
|
2008-11-23 16:45:00 +00:00
|
|
|
$co = $this->getCommit($commit);
|
|
|
|
if ($folder) {
|
|
|
|
// As we are limiting to a given folder, we need to find
|
|
|
|
// the tree corresponding to this folder.
|
|
|
|
$found = false;
|
|
|
|
foreach ($this->getTreeInfo($co->tree, true, '', true) as $file) {
|
|
|
|
if ($file->type == 'tree' and $file->file == $folder) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$found) {
|
|
|
|
throw new Exception(sprintf(__('Folder %1$s not found in commit %2$s.'), $folder, $commit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$res = $this->getTreeInfo($commit, $recurse=true, $folder);
|
|
|
|
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, $folder='', $root=false)
|
|
|
|
{
|
|
|
|
if ('commit' != $this->testHash($tree)) {
|
|
|
|
throw new Exception(sprintf(__('Not a valid tree: %s.'), $tree));
|
|
|
|
}
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd_tmpl = Pluf::f('hg_path', 'hg').' manifest -R %s --debug -r %s';
|
2008-11-23 16:45:00 +00:00
|
|
|
$cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo), $tree, ($recurse) ? '' : '');
|
|
|
|
$out = array();
|
|
|
|
$res = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
exec($cmd, $out);
|
2009-04-07 09:19:07 +00:00
|
|
|
$tmp_hack = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
while (null !== ($line = array_pop($out))) {
|
2008-11-23 16:45:00 +00:00
|
|
|
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
|
|
|
|
$file = trim($file);
|
|
|
|
$dir = explode('/', $file, -1);
|
|
|
|
$tmp = '';
|
2009-05-25 10:16:34 +00:00
|
|
|
for ($i=0, $n=count($dir); $i<$n; $i++) {
|
2008-11-23 16:45:00 +00:00
|
|
|
if ($i > 0) {
|
|
|
|
$tmp .= '/';
|
|
|
|
}
|
2009-01-17 09:03:10 +00:00
|
|
|
$tmp .= $dir[$i];
|
2009-04-07 09:19:07 +00:00
|
|
|
if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) {
|
2009-05-25 10:16:34 +00:00
|
|
|
$out[] = "empty\t000\t\t$tmp/";
|
2009-04-07 09:19:07 +00:00
|
|
|
$tmp_hack["empty\t000\t\t$tmp/"] = 1;
|
|
|
|
}
|
2008-11-23 16:45:00 +00:00
|
|
|
}
|
|
|
|
if (preg_match('/^(.*)\/$/', $file, $match)) {
|
|
|
|
$type = 'tree';
|
|
|
|
$file = $match[1];
|
|
|
|
} else {
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$res[] = (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'hash' => $hash, 'fullpath' => ($folder) ? $folder.'/'.$file : $file,
|
|
|
|
'file' => $file);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2009-05-25 10:16:34 +00:00
|
|
|
public function getPathInfo($totest, $commit='tip')
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd_tmpl = Pluf::f('hg_path', 'hg').' manifest -R %s --debug -r %s';
|
2008-11-23 16:45:00 +00:00
|
|
|
$cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo), $commit);
|
|
|
|
$out = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
exec($cmd, $out);
|
|
|
|
$tmp_hack = array();
|
|
|
|
while (null !== ($line = array_pop($out))) {
|
2008-11-23 16:45:00 +00:00
|
|
|
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
|
|
|
|
$file = trim($file);
|
|
|
|
$dir = explode('/', $file, -1);
|
|
|
|
$tmp = '';
|
2009-05-25 10:16:34 +00:00
|
|
|
for ($i=0, $n=count($dir); $i<$n; $i++) {
|
2008-11-23 16:45:00 +00:00
|
|
|
if ($i > 0) {
|
|
|
|
$tmp .= '/';
|
|
|
|
}
|
2009-01-17 09:03:10 +00:00
|
|
|
$tmp .= $dir[$i];
|
2009-05-25 10:16:34 +00:00
|
|
|
if ($tmp == $totest) {
|
|
|
|
return (object) array('perm' => '000', 'type' => 'tree',
|
|
|
|
'hash' => $hash,
|
|
|
|
'file' => $totest,
|
|
|
|
'commit' => $commit
|
|
|
|
);
|
|
|
|
}
|
2009-04-07 09:19:07 +00:00
|
|
|
if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) {
|
2009-05-25 10:16:34 +00:00
|
|
|
$out[] = "empty\t000\t\t$tmp/";
|
2009-04-07 09:19:07 +00:00
|
|
|
$tmp_hack["empty\t000\t\t$tmp/"] = 1;
|
2008-11-23 16:45:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (preg_match('/^(.*)\/$/', $file, $match)) {
|
|
|
|
$type = 'tree';
|
|
|
|
$file = $match[1];
|
|
|
|
} else {
|
|
|
|
$type = 'blob';
|
|
|
|
}
|
2009-05-25 10:16:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2008-11-23 16:45:00 +00:00
|
|
|
if ($totest == $file) {
|
|
|
|
return (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'hash' => $hash,
|
|
|
|
'file' => $file,
|
|
|
|
'commit' => $commit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a blob.
|
|
|
|
*
|
|
|
|
* @param string request_file_info
|
|
|
|
* @param null to be svn client compatible
|
|
|
|
* @return string Raw blob
|
|
|
|
*/
|
|
|
|
public function getBlob($request_file_info, $dummy=null)
|
|
|
|
{
|
2009-05-25 10:16:34 +00:00
|
|
|
return shell_exec(sprintf(Pluf::f('hg_path', 'hg').' cat -R %s -r %s %s',
|
2008-11-23 16:45:00 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
$dummy,
|
|
|
|
escapeshellarg($this->repo . '/' . $request_file_info->file)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the branches.
|
|
|
|
*
|
|
|
|
* @return array Branches.
|
|
|
|
*/
|
|
|
|
public function getBranches()
|
|
|
|
{
|
2009-05-25 10:16:34 +00:00
|
|
|
if (isset($this->cache['branches'])) {
|
|
|
|
return $this->cache['branches'];
|
|
|
|
}
|
2008-11-23 16:45:00 +00:00
|
|
|
$out = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
exec(sprintf(Pluf::f('hg_path', 'hg').' branches -R %s',
|
2008-11-25 20:18:12 +00:00
|
|
|
escapeshellarg($this->repo)), $out);
|
2008-11-23 16:45:00 +00:00
|
|
|
$res = array();
|
|
|
|
foreach ($out as $b) {
|
|
|
|
preg_match('/(\S+).*\S+:(\S+)/', $b, $match);
|
2009-05-25 10:16:34 +00:00
|
|
|
$res[$match[1]] = '';
|
2008-11-23 16:45:00 +00:00
|
|
|
}
|
2009-05-25 10:16:34 +00:00
|
|
|
$this->cache['branches'] = $res;
|
2008-11-23 16:45:00 +00:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2009-05-25 10:16:34 +00:00
|
|
|
public function inBranches($commit, $path)
|
|
|
|
{
|
|
|
|
return (in_array($commit, array_keys($this->getBranches())))
|
|
|
|
? array($commit) : array();
|
|
|
|
}
|
|
|
|
|
2008-11-23 16:45:00 +00:00
|
|
|
/**
|
|
|
|
* Get commit details.
|
|
|
|
*
|
2009-01-20 12:12:17 +00:00
|
|
|
* @param string Commit ('HEAD')
|
|
|
|
* @param bool Get commit diff (false)
|
|
|
|
* @return array Changes
|
2008-11-23 16:45:00 +00:00
|
|
|
*/
|
2009-01-20 12:12:17 +00:00
|
|
|
public function getCommit($commit='tip', $getdiff=false)
|
2008-11-23 16:45:00 +00:00
|
|
|
{
|
2009-01-20 12:12:17 +00:00
|
|
|
$tmpl = ($getdiff) ?
|
2009-02-25 13:28:14 +00:00
|
|
|
Pluf::f('hg_path', 'hg').' log -p -r %s -R %s' : Pluf::f('hg_path', 'hg').' log -r %s -R %s';
|
2009-01-20 12:12:17 +00:00
|
|
|
$cmd = sprintf($tmpl,
|
|
|
|
escapeshellarg($commit), escapeshellarg($this->repo));
|
2008-11-23 16:45:00 +00:00
|
|
|
$out = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
exec($cmd, $out);
|
2008-11-23 16:45:00 +00:00
|
|
|
$log = array();
|
|
|
|
$change = array();
|
|
|
|
$inchange = false;
|
|
|
|
foreach ($out as $line) {
|
|
|
|
if (!$inchange and 0 === strpos($line, 'diff -r')) {
|
|
|
|
$inchange = true;
|
|
|
|
}
|
|
|
|
if ($inchange) {
|
|
|
|
$change[] = $line;
|
|
|
|
} else {
|
|
|
|
$log[] = $line;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$out = self::parseLog($log, 6);
|
|
|
|
$out[0]->changes = implode("\n", $change);
|
|
|
|
return $out[0];
|
|
|
|
}
|
|
|
|
|
2009-01-17 17:46:26 +00:00
|
|
|
/**
|
2009-01-20 15:26:36 +00:00
|
|
|
* Check if a commit is big.
|
2009-01-17 17:46:26 +00:00
|
|
|
*
|
|
|
|
* @param string Commit ('HEAD')
|
2009-01-20 15:26:36 +00:00
|
|
|
* @return bool The commit is big
|
2009-01-17 17:46:26 +00:00
|
|
|
*/
|
2009-01-20 15:26:36 +00:00
|
|
|
public function isCommitLarge($commit='HEAD')
|
2009-01-17 17:46:26 +00:00
|
|
|
{
|
2009-01-20 15:26:36 +00:00
|
|
|
return false;
|
2009-01-17 17:46:26 +00:00
|
|
|
}
|
2008-11-23 16:45:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get latest changes.
|
|
|
|
*
|
|
|
|
* @param string Commit ('HEAD').
|
|
|
|
* @param int Number of changes (10).
|
|
|
|
* @return array Changes.
|
|
|
|
*/
|
|
|
|
public function getChangeLog($commit='tip', $n=10)
|
|
|
|
{
|
2009-02-25 13:28:14 +00:00
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -l%s ', escapeshellarg($this->repo), $n, $commit);
|
2008-11-23 16:45:00 +00:00
|
|
|
$out = array();
|
2009-05-25 10:16:34 +00:00
|
|
|
exec($cmd, $out);
|
2008-11-23 16:45:00 +00:00
|
|
|
return self::parseLog($out, 6);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 += 1;
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$i++;
|
|
|
|
if (0 === strpos($line, 'changeset:')) {
|
|
|
|
if (count($c) > 0) {
|
|
|
|
$c['full_message'] = trim($c['full_message']);
|
|
|
|
$res[] = (object) $c;
|
|
|
|
}
|
|
|
|
$c = array();
|
|
|
|
$c['commit'] = substr(strrchr($line, ':'), 1);
|
|
|
|
$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]);
|
|
|
|
if ($match[1] == 'user') {
|
|
|
|
$c['author'] = $match[2];
|
|
|
|
} elseif ($match[1] == 'summary') {
|
|
|
|
$c['title'] = $match[2];
|
|
|
|
} else {
|
|
|
|
$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['tree'] = $c['commit'];
|
|
|
|
$c['full_message'] = trim($c['full_message']);
|
|
|
|
$res[] = (object) $c;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the command to create a zip archive at a given commit.
|
|
|
|
*
|
|
|
|
* @param string Commit
|
|
|
|
* @param string Prefix ('git-repo-dump')
|
|
|
|
* @return string Command
|
|
|
|
*/
|
|
|
|
public function getArchiveCommand($commit, $prefix='')
|
|
|
|
{
|
2009-01-29 19:29:45 +00:00
|
|
|
return sprintf(Pluf::f('idf_exec_cmd_prefix', '').
|
2009-02-25 13:28:14 +00:00
|
|
|
Pluf::f('hg_path', 'hg').' archive --type=zip -R %s -r %s -',
|
2008-11-23 16:45:00 +00:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($commit));
|
|
|
|
}
|
|
|
|
}
|