2008-11-23 17:45:00 +01: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.
|
2011-04-02 21:37:07 +02:00
|
|
|
# Copyright (C) 2008-2011 Céondo Ltd and contributors.
|
2008-11-23 17:45:00 +01:00
|
|
|
#
|
|
|
|
# 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 ***** */
|
|
|
|
|
|
|
|
/**
|
2011-05-25 23:26:14 +02:00
|
|
|
* A simple RAII helper that manages style files to format hg's log output
|
|
|
|
*/
|
|
|
|
class IDF_Scm_Mercurial_LogStyle
|
|
|
|
{
|
|
|
|
const FULL_LOG = 1;
|
|
|
|
const CHANGES = 2;
|
|
|
|
|
|
|
|
public function __construct($type)
|
|
|
|
{
|
|
|
|
$this->file = tempnam(Pluf::f('tmp_folder'), 'hg-log-style-');
|
|
|
|
|
|
|
|
if ($type == self::FULL_LOG) {
|
|
|
|
$style = 'changeset = "'
|
|
|
|
. 'changeset: {node|short}\n'
|
|
|
|
. 'branch: {branch}\n'
|
|
|
|
. 'author: {author}\n'
|
|
|
|
. 'date: {date|isodate}\n'
|
|
|
|
. 'parents: {parents}\n\n'
|
|
|
|
. '{desc}\n'
|
|
|
|
. '\0\n"'
|
|
|
|
. "\n"
|
|
|
|
. 'parent = "{node|short} "'
|
|
|
|
. "\n";
|
|
|
|
} elseif ($type == self::CHANGES) {
|
|
|
|
$style = 'changeset = "'
|
|
|
|
. 'file_mods: {file_mods}\n'
|
|
|
|
. 'file_adds: {file_adds}\n'
|
|
|
|
. 'file_dels: {file_dels}\n'
|
|
|
|
. 'file_copies: {file_copies}\n\n'
|
|
|
|
. '\0\n"'
|
|
|
|
. "\n"
|
|
|
|
. 'file_mod = "{file_mod}\0"'
|
|
|
|
. "\n"
|
|
|
|
. 'file_add = "{file_add}\0"'
|
|
|
|
. "\n"
|
|
|
|
. 'file_del = "{file_del}\0"'
|
|
|
|
. "\n"
|
2011-06-10 01:34:33 +02:00
|
|
|
. 'file_copy = "{source}\0{name}\0"'
|
2011-05-25 23:26:14 +02:00
|
|
|
. "\n";
|
|
|
|
} else {
|
|
|
|
throw new IDF_Scm_Exception('invalid type ' . $type);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($this->file, $style);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct()
|
|
|
|
{
|
|
|
|
@unlink($this->file);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return $this->file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main SCM class for Mercurial
|
2008-11-23 17:45:00 +01:00
|
|
|
*
|
2011-05-25 23:26:14 +02:00
|
|
|
* Note: Some commands take a --debug option, this is not lousy coding, but
|
|
|
|
* totally wanted, as hg returns additional / different data in this
|
|
|
|
* mode on which this largely depends.
|
2008-11-23 17:45:00 +01:00
|
|
|
*/
|
2009-05-25 12:16:34 +02:00
|
|
|
class IDF_Scm_Mercurial extends IDF_Scm
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2009-05-27 23:08:55 +02:00
|
|
|
public function __construct($repo, $project=null)
|
|
|
|
{
|
|
|
|
$this->repo = $repo;
|
|
|
|
$this->project = $project;
|
2011-05-25 23:26:14 +02:00
|
|
|
}
|
2009-05-27 23:08:55 +02:00
|
|
|
|
2009-06-19 17:31:45 +02:00
|
|
|
public function getRepositorySize()
|
|
|
|
{
|
2010-02-24 15:06:13 +01:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').'du -sk '
|
2009-06-19 17:31:45 +02:00
|
|
|
.escapeshellarg($this->repo);
|
2010-05-02 00:56:04 +02:00
|
|
|
$out = explode(' ',
|
2010-02-17 21:57:28 +01:00
|
|
|
self::shell_exec('IDF_Scm_Mercurial::getRepositorySize',
|
2010-05-02 00:56:04 +02:00
|
|
|
$cmd),
|
2010-02-17 21:57:28 +01:00
|
|
|
2);
|
2009-06-19 21:37:39 +02:00
|
|
|
return (int) $out[0]*1024;
|
2009-06-19 17:31:45 +02:00
|
|
|
}
|
|
|
|
|
2009-05-25 12:16:34 +02:00
|
|
|
public static function factory($project)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2009-05-25 12:16:34 +02:00
|
|
|
$rep = sprintf(Pluf::f('mercurial_repositories'), $project->shortname);
|
|
|
|
return new IDF_Scm_Mercurial($rep, $project);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isAvailable()
|
|
|
|
{
|
2009-10-01 14:56:54 +02:00
|
|
|
try {
|
|
|
|
$branches = $this->getBranches();
|
|
|
|
} catch (IDF_Scm_Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (count($branches) > 0);
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
|
2008-12-05 11:34:02 +01:00
|
|
|
public function findAuthor($author)
|
|
|
|
{
|
|
|
|
// We extract the email.
|
|
|
|
$match = array();
|
|
|
|
if (!preg_match('/<(.*)>/', $author, $match)) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-02-09 16:35:50 +01:00
|
|
|
return Pluf::factory('IDF_EmailAddress')->get_user_for_email_address($match[1]);
|
2008-12-05 11:34:02 +01:00
|
|
|
}
|
|
|
|
|
2009-05-25 12:16:34 +02:00
|
|
|
public function getMainBranch()
|
|
|
|
{
|
|
|
|
return 'tip';
|
|
|
|
}
|
|
|
|
|
2010-05-02 00:56:04 +02:00
|
|
|
public static function getAnonymousAccessUrl($project, $commit=null)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2008-11-24 20:27:03 +01:00
|
|
|
return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname);
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
|
2010-05-02 00:56:04 +02:00
|
|
|
public static function getAuthAccessUrl($project, $user, $commit=null)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2009-05-25 12:16:34 +02:00
|
|
|
return sprintf(Pluf::f('mercurial_remote_url'), $project->shortname);
|
|
|
|
}
|
|
|
|
|
2010-09-01 13:13:52 +00:00
|
|
|
public function validateRevision($rev)
|
2009-05-25 12:16:34 +02:00
|
|
|
{
|
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -r %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($rev));
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2011-04-01 11:15:13 +02:00
|
|
|
self::exec('IDF_Scm_Mercurial::validateRevision', $cmd, $out, $ret);
|
2010-09-01 13:13:52 +00:00
|
|
|
|
|
|
|
// FIXME: apparently a given hg revision can also be ambigious -
|
|
|
|
// handle this case here sometime
|
|
|
|
if ($ret == 0 && count($out) > 0)
|
|
|
|
return IDF_Scm::REVISION_VALID;
|
|
|
|
return IDF_Scm::REVISION_INVALID;
|
2008-11-23 17:45:00 +01: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 14:28:14 +01:00
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log -R %s -r %s',
|
2008-11-23 17:45:00 +01:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($hash));
|
2010-05-02 00:56:04 +02:00
|
|
|
$ret = 0;
|
2008-11-23 17:45:00 +01:00
|
|
|
$out = array();
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::testHash', $cmd, $out, $ret);
|
2010-05-02 00:56:04 +02:00
|
|
|
return ($ret != 0) ? false : 'commit';
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
|
2009-05-25 12:16:34 +02:00
|
|
|
public function getTree($commit, $folder='/', $branch=null)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
|
|
|
// now we grab the info about this commit including its tree.
|
2009-05-25 12:16:34 +02:00
|
|
|
$folder = ($folder == '/') ? '' : $folder;
|
2008-11-23 17:45:00 +01: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;
|
|
|
|
}
|
2010-05-02 00:56:04 +02:00
|
|
|
}
|
2008-11-23 17:45:00 +01:00
|
|
|
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.
|
|
|
|
*
|
2010-05-02 00:56:04 +02:00
|
|
|
* @param string Tree hash
|
2008-11-23 17:45:00 +01:00
|
|
|
* @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 14:28:14 +01:00
|
|
|
$cmd_tmpl = Pluf::f('hg_path', 'hg').' manifest -R %s --debug -r %s';
|
2011-05-25 23:26:14 +02:00
|
|
|
$cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($tree));
|
2008-11-23 17:45:00 +01:00
|
|
|
$out = array();
|
|
|
|
$res = array();
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::getTreeInfo', $cmd, $out);
|
2009-04-07 11:19:07 +02:00
|
|
|
$tmp_hack = array();
|
2009-05-25 12:16:34 +02:00
|
|
|
while (null !== ($line = array_pop($out))) {
|
2008-11-23 17:45:00 +01:00
|
|
|
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
|
|
|
|
$file = trim($file);
|
|
|
|
$dir = explode('/', $file, -1);
|
|
|
|
$tmp = '';
|
2009-05-25 12:16:34 +02:00
|
|
|
for ($i=0, $n=count($dir); $i<$n; $i++) {
|
2008-11-23 17:45:00 +01:00
|
|
|
if ($i > 0) {
|
|
|
|
$tmp .= '/';
|
|
|
|
}
|
2009-01-17 10:03:10 +01:00
|
|
|
$tmp .= $dir[$i];
|
2009-04-07 11:19:07 +02:00
|
|
|
if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) {
|
2009-05-25 12:16:34 +02:00
|
|
|
$out[] = "empty\t000\t\t$tmp/";
|
2009-04-07 11:19:07 +02:00
|
|
|
$tmp_hack["empty\t000\t\t$tmp/"] = 1;
|
|
|
|
}
|
2008-11-23 17:45:00 +01: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;
|
|
|
|
}
|
|
|
|
}
|
2010-01-20 22:33:53 +01:00
|
|
|
$fullpath = ($folder) ? $folder.'/'.$file : $file;
|
|
|
|
$efullpath = self::smartEncode($fullpath);
|
2010-05-02 00:56:04 +02:00
|
|
|
$res[] = (object) array('perm' => $perm, 'type' => $type,
|
2010-01-20 22:33:53 +01:00
|
|
|
'hash' => $hash, 'fullpath' => $fullpath,
|
|
|
|
'efullpath' => $efullpath, 'file' => $file);
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2009-05-25 12:16:34 +02:00
|
|
|
public function getPathInfo($totest, $commit='tip')
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2009-02-25 14:28:14 +01:00
|
|
|
$cmd_tmpl = Pluf::f('hg_path', 'hg').' manifest -R %s --debug -r %s';
|
2011-05-25 23:26:14 +02:00
|
|
|
$cmd = sprintf($cmd_tmpl, escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($commit));
|
2008-11-23 17:45:00 +01:00
|
|
|
$out = array();
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::getPathInfo', $cmd, $out);
|
2009-05-25 12:16:34 +02:00
|
|
|
$tmp_hack = array();
|
|
|
|
while (null !== ($line = array_pop($out))) {
|
2008-11-23 17:45:00 +01:00
|
|
|
list($hash, $perm, $exec, $file) = preg_split('/ |\t/', $line, 4);
|
|
|
|
$file = trim($file);
|
|
|
|
$dir = explode('/', $file, -1);
|
|
|
|
$tmp = '';
|
2009-05-25 12:16:34 +02:00
|
|
|
for ($i=0, $n=count($dir); $i<$n; $i++) {
|
2008-11-23 17:45:00 +01:00
|
|
|
if ($i > 0) {
|
|
|
|
$tmp .= '/';
|
|
|
|
}
|
2009-01-17 10:03:10 +01:00
|
|
|
$tmp .= $dir[$i];
|
2009-05-25 12:16:34 +02:00
|
|
|
if ($tmp == $totest) {
|
2009-06-19 15:51:31 +02:00
|
|
|
$pathinfo = pathinfo($totest);
|
2010-05-02 00:56:04 +02:00
|
|
|
return (object) array('perm' => '000', 'type' => 'tree',
|
|
|
|
'hash' => $hash,
|
2009-06-19 15:51:31 +02:00
|
|
|
'fullpath' => $totest,
|
|
|
|
'file' => $pathinfo['basename'],
|
2009-05-25 12:16:34 +02:00
|
|
|
'commit' => $commit
|
|
|
|
);
|
|
|
|
}
|
2009-04-07 11:19:07 +02:00
|
|
|
if (!isset($tmp_hack["empty\t000\t\t$tmp/"])) {
|
2009-05-25 12:16:34 +02:00
|
|
|
$out[] = "empty\t000\t\t$tmp/";
|
2009-04-07 11:19:07 +02:00
|
|
|
$tmp_hack["empty\t000\t\t$tmp/"] = 1;
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (preg_match('/^(.*)\/$/', $file, $match)) {
|
|
|
|
$type = 'tree';
|
|
|
|
$file = $match[1];
|
|
|
|
} else {
|
|
|
|
$type = 'blob';
|
|
|
|
}
|
|
|
|
if ($totest == $file) {
|
2009-06-19 15:51:31 +02:00
|
|
|
$pathinfo = pathinfo($totest);
|
2010-05-02 00:56:04 +02:00
|
|
|
return (object) array('perm' => $perm, 'type' => $type,
|
|
|
|
'hash' => $hash,
|
2009-06-19 15:51:31 +02:00
|
|
|
'fullpath' => $totest,
|
|
|
|
'file' => $pathinfo['basename'],
|
2008-11-23 17:45:00 +01:00
|
|
|
'commit' => $commit
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-05-02 00:56:04 +02:00
|
|
|
|
2009-05-25 14:18:48 +02:00
|
|
|
public function getFile($def, $cmd_only=false)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2009-05-25 14:18:48 +02:00
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' cat -R %s -r %s %s',
|
2010-05-02 00:56:04 +02:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($def->commit),
|
2010-01-20 21:27:18 +01:00
|
|
|
escapeshellarg($this->repo.'/'.$def->fullpath));
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-05-02 00:56:04 +02:00
|
|
|
return ($cmd_only) ?
|
2010-02-17 21:57:28 +01:00
|
|
|
$cmd : self::shell_exec('IDF_Scm_Mercurial::getFile', $cmd);
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the branches.
|
|
|
|
*
|
|
|
|
* @return array Branches.
|
|
|
|
*/
|
|
|
|
public function getBranches()
|
|
|
|
{
|
2009-05-25 12:16:34 +02:00
|
|
|
if (isset($this->cache['branches'])) {
|
|
|
|
return $this->cache['branches'];
|
|
|
|
}
|
2008-11-23 17:45:00 +01:00
|
|
|
$out = array();
|
2010-05-02 00:56:04 +02:00
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' branches -R %s',
|
2009-07-23 23:33:08 +02:00
|
|
|
escapeshellarg($this->repo));
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::getBranches', $cmd, $out);
|
2008-11-23 17:45:00 +01:00
|
|
|
$res = array();
|
|
|
|
foreach ($out as $b) {
|
2011-05-25 23:26:14 +02:00
|
|
|
preg_match('/(.+?)\s+\S+:(\S+)/', $b, $match);
|
2009-05-25 12:16:34 +02:00
|
|
|
$res[$match[1]] = '';
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
2009-05-25 12:16:34 +02:00
|
|
|
$this->cache['branches'] = $res;
|
2008-11-23 17:45:00 +01:00
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2009-11-06 18:06:01 +01:00
|
|
|
/**
|
|
|
|
* Get the tags.
|
|
|
|
*
|
|
|
|
* @return array Tags.
|
|
|
|
*/
|
|
|
|
public function getTags()
|
|
|
|
{
|
|
|
|
if (isset($this->cache['tags'])) {
|
|
|
|
return $this->cache['tags'];
|
|
|
|
}
|
|
|
|
$out = array();
|
2010-05-02 00:56:04 +02:00
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' tags -R %s',
|
2009-11-06 18:06:01 +01:00
|
|
|
escapeshellarg($this->repo));
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::getTags', $cmd, $out);
|
2009-11-06 18:06:01 +01:00
|
|
|
$res = array();
|
|
|
|
foreach ($out as $b) {
|
2011-05-25 23:26:14 +02:00
|
|
|
preg_match('/(.+?)\s+\S+:(\S+)/', $b, $match);
|
2009-11-06 18:06:01 +01:00
|
|
|
$res[$match[1]] = '';
|
|
|
|
}
|
|
|
|
$this->cache['tags'] = $res;
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
2009-05-25 12:16:34 +02:00
|
|
|
public function inBranches($commit, $path)
|
|
|
|
{
|
2010-05-02 00:56:04 +02:00
|
|
|
return (in_array($commit, array_keys($this->getBranches())))
|
2009-05-25 12:16:34 +02:00
|
|
|
? array($commit) : array();
|
|
|
|
}
|
|
|
|
|
2009-11-06 18:06:01 +01:00
|
|
|
public function inTags($commit, $path)
|
|
|
|
{
|
2010-05-02 00:56:04 +02:00
|
|
|
return (in_array($commit, array_keys($this->getTags())))
|
2009-11-06 18:06:01 +01:00
|
|
|
? array($commit) : array();
|
|
|
|
}
|
|
|
|
|
2008-11-23 17:45:00 +01:00
|
|
|
/**
|
|
|
|
* Get commit details.
|
|
|
|
*
|
2009-01-20 13:12:17 +01:00
|
|
|
* @param string Commit ('HEAD')
|
|
|
|
* @param bool Get commit diff (false)
|
|
|
|
* @return array Changes
|
2008-11-23 17:45:00 +01:00
|
|
|
*/
|
2009-10-01 14:56:54 +02:00
|
|
|
public function getCommit($commit, $getdiff=false)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2011-04-01 11:15:13 +02:00
|
|
|
if ($this->validateRevision($commit) != IDF_Scm::REVISION_VALID) {
|
2009-10-01 14:56:54 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-05-25 23:26:14 +02:00
|
|
|
|
|
|
|
$logStyle = new IDF_Scm_Mercurial_LogStyle(IDF_Scm_Mercurial_LogStyle::FULL_LOG);
|
2011-03-18 02:47:08 +01:00
|
|
|
$tmpl = ($getdiff)
|
2011-05-25 23:26:14 +02:00
|
|
|
? Pluf::f('hg_path', 'hg').' log --debug -p -r %s -R %s --style %s'
|
|
|
|
: Pluf::f('hg_path', 'hg').' log --debug -r %s -R %s --style %s';
|
2010-05-02 00:56:04 +02:00
|
|
|
$cmd = sprintf($tmpl,
|
2011-03-18 02:47:08 +01:00
|
|
|
escapeshellarg($commit),
|
|
|
|
escapeshellarg($this->repo),
|
2011-05-25 23:26:14 +02:00
|
|
|
escapeshellarg($logStyle->get()));
|
2008-11-23 17:45:00 +01:00
|
|
|
$out = array();
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::getCommit', $cmd, $out);
|
2008-11-23 17:45:00 +01: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;
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 23:26:14 +02:00
|
|
|
$out = self::parseLog($log);
|
2010-12-08 01:48:26 +01:00
|
|
|
$out[0]->diff = implode("\n", $change);
|
2008-11-23 17:45:00 +01:00
|
|
|
return $out[0];
|
|
|
|
}
|
|
|
|
|
2011-05-25 23:26:14 +02:00
|
|
|
/**
|
|
|
|
* @see IDF_Scm::getChanges()
|
|
|
|
*/
|
|
|
|
public function getChanges($commit)
|
|
|
|
{
|
|
|
|
if ($this->validateRevision($commit) != IDF_Scm::REVISION_VALID) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$logStyle = new IDF_Scm_Mercurial_LogStyle(IDF_Scm_Mercurial_LogStyle::CHANGES);
|
|
|
|
$tmpl = Pluf::f('hg_path', 'hg').' log --debug -r %s -R %s --style %s';
|
|
|
|
$cmd = sprintf($tmpl,
|
|
|
|
escapeshellarg($commit),
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($logStyle->get()));
|
|
|
|
$out = array();
|
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
|
|
|
self::exec('IDF_Scm_Mercurial::getChanges', $cmd, $out);
|
|
|
|
$log = self::parseLog($out);
|
|
|
|
// we expect only one log entry that contains all the needed information
|
|
|
|
$log = $log[0];
|
|
|
|
|
|
|
|
$return = (object) array(
|
|
|
|
'additions' => preg_split('/\0/', $log->file_adds, -1, PREG_SPLIT_NO_EMPTY),
|
|
|
|
'deletions' => preg_split('/\0/', $log->file_dels, -1, PREG_SPLIT_NO_EMPTY),
|
|
|
|
'patches' => preg_split('/\0/', $log->file_mods, -1, PREG_SPLIT_NO_EMPTY),
|
|
|
|
// hg has no support for built-in attributes, so this keeps empty
|
|
|
|
'properties' => array(),
|
2011-06-10 01:34:33 +02:00
|
|
|
// these two are filled below
|
|
|
|
'copies' => array(),
|
2011-05-25 23:26:14 +02:00
|
|
|
'renames' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$file_copies = preg_split('/\0/', $log->file_copies, -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
|
2011-06-10 01:34:33 +02:00
|
|
|
// copies are treated as renames if they have an add _and_ a drop;
|
|
|
|
// only if they only have an add, but no drop, they're treated as copies
|
2011-05-25 23:26:14 +02:00
|
|
|
for ($i=0; $i<count($file_copies); $i+=2) {
|
2011-06-10 01:34:33 +02:00
|
|
|
$src = $file_copies[$i];
|
|
|
|
$trg = $file_copies[$i+1];
|
|
|
|
$srcidx = array_search($src, $return->deletions);
|
|
|
|
$trgidx = array_search($trg, $return->additions);
|
|
|
|
if ($srcidx !== false && $trgidx !== false) {
|
|
|
|
$return->renames[$src] = $trg;
|
|
|
|
unset($return->deletions[$srcidx]);
|
|
|
|
unset($return->additions[$trgidx]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($srcidx === false && $trgidx !== false) {
|
|
|
|
$return->copies[$src] = $trg;
|
|
|
|
unset($return->additions[$trgidx]);
|
|
|
|
continue;
|
2011-05-25 23:26:14 +02:00
|
|
|
}
|
2011-06-10 01:34:33 +02:00
|
|
|
// file sutures (counter-operation to copy) not supported
|
2011-05-25 23:26:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2009-01-17 18:46:26 +01:00
|
|
|
/**
|
2009-01-20 16:26:36 +01:00
|
|
|
* Check if a commit is big.
|
2009-01-17 18:46:26 +01:00
|
|
|
*
|
|
|
|
* @param string Commit ('HEAD')
|
2009-01-20 16:26:36 +01:00
|
|
|
* @return bool The commit is big
|
2009-01-17 18:46:26 +01:00
|
|
|
*/
|
2009-01-20 16:26:36 +01:00
|
|
|
public function isCommitLarge($commit='HEAD')
|
2009-01-17 18:46:26 +01:00
|
|
|
{
|
2009-01-20 16:26:36 +01:00
|
|
|
return false;
|
2009-01-17 18:46:26 +01:00
|
|
|
}
|
2008-11-23 17:45:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get latest changes.
|
|
|
|
*
|
|
|
|
* @param string Commit ('HEAD').
|
|
|
|
* @param int Number of changes (10).
|
|
|
|
* @return array Changes.
|
|
|
|
*/
|
|
|
|
public function getChangeLog($commit='tip', $n=10)
|
|
|
|
{
|
2011-05-25 23:26:14 +02:00
|
|
|
$logStyle = new IDF_Scm_Mercurial_LogStyle(IDF_Scm_Mercurial_LogStyle::FULL_LOG);
|
|
|
|
|
|
|
|
// hg accepts revision IDs as arguments to --branch / -b as well and
|
|
|
|
// uses the branch of the revision in question to filter the other
|
|
|
|
// revisions
|
|
|
|
$cmd = sprintf(Pluf::f('hg_path', 'hg').' log --debug -R %s -l%s --style %s -b %s',
|
|
|
|
escapeshellarg($this->repo),
|
|
|
|
$n,
|
|
|
|
escapeshellarg($logStyle->get()),
|
|
|
|
escapeshellarg($commit));
|
2008-11-23 17:45:00 +01:00
|
|
|
$out = array();
|
2009-07-02 13:05:43 +02:00
|
|
|
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
|
2010-02-17 21:57:28 +01:00
|
|
|
self::exec('IDF_Scm_Mercurial::getChangeLog', $cmd, $out);
|
2011-05-25 23:26:14 +02:00
|
|
|
return self::parseLog($out);
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-05-25 23:26:14 +02:00
|
|
|
* Parse the log lines of our custom style format.
|
2008-11-23 17:45:00 +01:00
|
|
|
*
|
|
|
|
* @param array Lines.
|
|
|
|
* @return array Change log.
|
|
|
|
*/
|
2011-05-25 23:26:14 +02:00
|
|
|
public static function parseLog($lines)
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
|
|
|
$res = array();
|
|
|
|
$c = array();
|
2011-05-25 23:26:14 +02:00
|
|
|
$headers_processed = false;
|
2008-11-23 17:45:00 +01:00
|
|
|
foreach ($lines as $line) {
|
2011-05-25 23:26:14 +02:00
|
|
|
if ($line == "\0") {
|
|
|
|
$headers_processed = false;
|
2008-11-23 17:45:00 +01:00
|
|
|
if (count($c) > 0) {
|
2011-06-02 01:21:32 +02:00
|
|
|
if (array_key_exists('full_message', $c))
|
|
|
|
$c['full_message'] = trim($c['full_message']);
|
2008-11-23 17:45:00 +01:00
|
|
|
$res[] = (object) $c;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-25 23:26:14 +02:00
|
|
|
if (!$headers_processed && empty($line)) {
|
|
|
|
$headers_processed = true;
|
2008-11-23 17:45:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
2011-05-25 23:26:14 +02:00
|
|
|
if (!$headers_processed && preg_match('/^(\S+):\s*(.*)/', $line, $match)) {
|
2008-11-23 17:45:00 +01:00
|
|
|
$match[1] = strtolower($match[1]);
|
2011-05-25 23:26:14 +02:00
|
|
|
if ($match[1] == 'changeset') {
|
|
|
|
$c = array();
|
|
|
|
$c['commit'] = $match[2];
|
|
|
|
$c['tree'] = $c['commit'];
|
|
|
|
$c['full_message'] = '';
|
2011-06-02 01:21:32 +02:00
|
|
|
} elseif ($match[1] == 'author') {
|
2008-11-23 17:45:00 +01:00
|
|
|
$c['author'] = $match[2];
|
2010-11-17 01:53:17 +01:00
|
|
|
} elseif ($match[1] == 'branch') {
|
2011-05-25 23:26:14 +02:00
|
|
|
$c['branch'] = empty($match[2]) ? 'default' : $match[2];
|
|
|
|
} elseif ($match[1] == 'parents') {
|
|
|
|
$parents = preg_split('/\s+/', $match[2], -1, PREG_SPLIT_NO_EMPTY);
|
|
|
|
for ($i=0, $j=count($parents); $i<$j; ++$i) {
|
|
|
|
if ($parents[$i] == '000000000000')
|
|
|
|
unset($parents[$i]);
|
|
|
|
}
|
|
|
|
$c['parents'] = $parents;
|
2008-11-23 17:45:00 +01:00
|
|
|
} else {
|
|
|
|
$c[$match[1]] = trim($match[2]);
|
|
|
|
}
|
|
|
|
if ($match[1] == 'date') {
|
|
|
|
$c['date'] = gmdate('Y-m-d H:i:s', strtotime($match[2]));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-05-25 23:26:14 +02:00
|
|
|
if ($headers_processed) {
|
|
|
|
if (empty($c['title']))
|
|
|
|
$c['title'] = trim($line);
|
|
|
|
else
|
|
|
|
$c['full_message'] .= trim($line)."\n";
|
2008-11-23 17:45:00 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-30 21:52:40 +00:00
|
|
|
* Generate a zip archive at a given commit.
|
2008-11-23 17:45:00 +01:00
|
|
|
*
|
|
|
|
* @param string Commit
|
|
|
|
* @param string Prefix ('git-repo-dump')
|
2010-10-30 21:52:40 +00:00
|
|
|
* @return Pluf_HTTP_Response The HTTP response containing the zip archive
|
2008-11-23 17:45:00 +01:00
|
|
|
*/
|
2011-03-25 00:11:03 +01:00
|
|
|
public function getArchiveStream($commit, $prefix='')
|
2008-11-23 17:45:00 +01:00
|
|
|
{
|
2010-10-30 21:52:40 +00:00
|
|
|
$cmd = sprintf(Pluf::f('idf_exec_cmd_prefix', '').
|
2009-02-25 14:28:14 +01:00
|
|
|
Pluf::f('hg_path', 'hg').' archive --type=zip -R %s -r %s -',
|
2008-11-23 17:45:00 +01:00
|
|
|
escapeshellarg($this->repo),
|
|
|
|
escapeshellarg($commit));
|
2010-10-30 21:52:40 +00:00
|
|
|
return new Pluf_HTTP_Response_CommandPassThru($cmd, 'application/x-zip');
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|
2011-03-16 23:50:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see IDF_Scm::getDiffPathStripLevel()
|
|
|
|
*/
|
|
|
|
public function getDiffPathStripLevel()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2008-11-23 17:45:00 +01:00
|
|
|
}
|