Reworked the option / argument handling in the SVN interface to

have less code duplication.
This commit is contained in:
Thomas Keller 2011-08-13 02:28:15 +02:00
parent b1b72190e1
commit b17de014ec

View File

@ -33,7 +33,6 @@
*/ */
class IDF_Scm_Svn extends IDF_Scm class IDF_Scm_Svn extends IDF_Scm
{ {
public $username = ''; public $username = '';
public $password = ''; public $password = '';
private $assoc = array('dir' => 'tree', private $assoc = array('dir' => 'tree',
@ -48,11 +47,7 @@ class IDF_Scm_Svn extends IDF_Scm
public function isAvailable() public function isAvailable()
{ {
$cmd = sprintf(Pluf::f('svn_path', 'svn').' info --no-auth-cache --xml --username=%s --password=%s %s', $cmd = $this->svnCmd(array('info', '--xml'), $this->repo);
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($this->repo));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlInfo = self::shell_exec('IDF_Scm_Svn::isAvailable', $cmd); $xmlInfo = self::shell_exec('IDF_Scm_Svn::isAvailable', $cmd);
try { try {
@ -163,13 +158,7 @@ class IDF_Scm_Svn extends IDF_Scm
return IDF_Scm::REVISION_VALID; return IDF_Scm::REVISION_VALID;
} }
$cmd = sprintf(Pluf::f('svn_path', 'svn').' info --no-auth-cache --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('info'), $this->repo, $rev);
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($rev),
escapeshellarg($this->repo),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
self::exec('IDF_Scm_Svn::validateRevision', $cmd, $out, $ret); self::exec('IDF_Scm_Svn::validateRevision', $cmd, $out, $ret);
if ($ret == 0) if ($ret == 0)
@ -191,13 +180,9 @@ class IDF_Scm_Svn extends IDF_Scm
} }
// Else, test the path on revision // Else, test the path on revision
$cmd = sprintf(Pluf::f('svn_path', 'svn').' info --no-auth-cache --xml --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('info', '--xml'),
escapeshellarg($this->username), $this->repo.'/'.self::smartEncode($path),
escapeshellarg($this->password), $rev);
escapeshellarg($rev),
escapeshellarg($this->repo.'/'.self::smartEncode($path)),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlInfo = self::shell_exec('IDF_Scm_Svn::testHash', $cmd); $xmlInfo = self::shell_exec('IDF_Scm_Svn::testHash', $cmd);
// If exception is thrown, return false // If exception is thrown, return false
@ -219,13 +204,9 @@ class IDF_Scm_Svn extends IDF_Scm
public function getTree($commit, $folder='/', $branch=null) public function getTree($commit, $folder='/', $branch=null)
{ {
$cmd = sprintf(Pluf::f('svn_path', 'svn').' ls --no-auth-cache --xml --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('ls', '--xml'),
escapeshellarg($this->username), $this->repo.'/'.self::smartEncode($folder),
escapeshellarg($this->password), $commit);
escapeshellarg($commit),
escapeshellarg($this->repo.'/'.self::smartEncode($folder)),
escapeshellarg($commit));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getTree', $cmd)); $xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getTree', $cmd));
$res = array(); $res = array();
$folder = (strlen($folder) and ($folder != '/')) ? $folder.'/' : ''; $folder = (strlen($folder) and ($folder != '/')) ? $folder.'/' : '';
@ -261,13 +242,7 @@ class IDF_Scm_Svn extends IDF_Scm
if (isset($this->cache['commitmess'][$rev])) { if (isset($this->cache['commitmess'][$rev])) {
return $this->cache['commitmess'][$rev]; return $this->cache['commitmess'][$rev];
} }
$cmd = sprintf(Pluf::f('svn_path', 'svn').' log --no-auth-cache --xml --limit 1 --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('log', '--xml', '--limit', '1'), $this->repo, $rev);
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($rev),
escapeshellarg($this->repo),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
try { try {
$xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getCommitMessage', $cmd)); $xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getCommitMessage', $cmd));
$this->cache['commitmess'][$rev] = (string) $xml->logentry->msg; $this->cache['commitmess'][$rev] = (string) $xml->logentry->msg;
@ -283,13 +258,8 @@ class IDF_Scm_Svn extends IDF_Scm
if ($rev == null) { if ($rev == null) {
$rev = 'HEAD'; $rev = 'HEAD';
} }
$cmd = sprintf(Pluf::f('svn_path', 'svn').' info --no-auth-cache --xml --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('info', '--xml'),
escapeshellarg($this->username), $this->repo.'/'.self::smartEncode($filename), $rev);
escapeshellarg($this->password),
escapeshellarg($rev),
escapeshellarg($this->repo.'/'.self::smartEncode($filename)),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getPathInfo', $cmd)); $xml = simplexml_load_string(self::shell_exec('IDF_Scm_Svn::getPathInfo', $cmd));
if (!isset($xml->entry)) { if (!isset($xml->entry)) {
return false; return false;
@ -311,13 +281,9 @@ class IDF_Scm_Svn extends IDF_Scm
public function getFile($def, $cmd_only=false) public function getFile($def, $cmd_only=false)
{ {
$cmd = sprintf(Pluf::f('svn_path', 'svn').' cat --no-auth-cache --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('cat'),
escapeshellarg($this->username), $this->repo.'/'.self::smartEncode($def->fullpath),
escapeshellarg($this->password), $def->rev);
escapeshellarg($def->rev),
escapeshellarg($this->repo.'/'.self::smartEncode($def->fullpath)),
escapeshellarg($def->rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
return ($cmd_only) ? return ($cmd_only) ?
$cmd : self::shell_exec('IDF_Scm_Svn::getFile', $cmd); $cmd : self::shell_exec('IDF_Scm_Svn::getFile', $cmd);
} }
@ -333,11 +299,7 @@ class IDF_Scm_Svn extends IDF_Scm
return $this->cache['branches']; return $this->cache['branches'];
} }
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' ls --no-auth-cache --username=%s --password=%s --revision=HEAD %s@HEAD', $cmd = $this->svnCmd(array('ls'), $this->repo.'/branches', 'HEAD');
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($this->repo.'/branches'));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
self::exec('IDF_Scm_Svn::getBranches', $cmd, $out, $ret); self::exec('IDF_Scm_Svn::getBranches', $cmd, $out, $ret);
if ($ret == 0) { if ($ret == 0) {
foreach ($out as $entry) { foreach ($out as $entry) {
@ -348,11 +310,8 @@ class IDF_Scm_Svn extends IDF_Scm
} }
} }
ksort($res); ksort($res);
$cmd = sprintf(Pluf::f('svn_path', 'svn').' info --no-auth-cache --username=%s --password=%s --revision=HEAD %s@HEAD',
escapeshellarg($this->username), $cmd = $this->svnCmd(array('info'), $this->repo.'/trunk', 'HEAD');
escapeshellarg($this->password),
escapeshellarg($this->repo.'/trunk'));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
self::exec('IDF_Scm_Svn::getBranches', $cmd, $out, $ret); self::exec('IDF_Scm_Svn::getBranches', $cmd, $out, $ret);
if ($ret == 0) { if ($ret == 0) {
$res = array('trunk' => 'trunk') + $res; $res = array('trunk' => 'trunk') + $res;
@ -372,11 +331,7 @@ class IDF_Scm_Svn extends IDF_Scm
return $this->cache['tags']; return $this->cache['tags'];
} }
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' ls --no-auth-cache --username=%s --password=%s --revision=HEAD %s@HEAD', $cmd = $this->svnCmd(array('ls'), $this->repo.'/tags', 'HEAD');
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($this->repo.'/tags'));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
self::exec('IDF_Scm_Svn::getTags', $cmd, $out, $ret); self::exec('IDF_Scm_Svn::getTags', $cmd, $out, $ret);
if ($ret == 0) { if ($ret == 0) {
foreach ($out as $entry) { foreach ($out as $entry) {
@ -429,13 +384,8 @@ class IDF_Scm_Svn extends IDF_Scm
return false; return false;
} }
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' log --no-auth-cache --xml --limit 1 -v --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('log', '--xml', '--limit', '1', '-v'),
escapeshellarg($this->username), $this->repo, $commit);
escapeshellarg($this->password),
escapeshellarg($commit),
escapeshellarg($this->repo),
escapeshellarg($commit));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlRes = self::shell_exec('IDF_Scm_Svn::getCommit', $cmd); $xmlRes = self::shell_exec('IDF_Scm_Svn::getCommit', $cmd);
$xml = simplexml_load_string($xmlRes); $xml = simplexml_load_string($xmlRes);
$res['author'] = (string) $xml->logentry->author; $res['author'] = (string) $xml->logentry->author;
@ -477,12 +427,7 @@ class IDF_Scm_Svn extends IDF_Scm
private function getDiff($rev='HEAD') private function getDiff($rev='HEAD')
{ {
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' diff --no-auth-cache -c %s --username=%s --password=%s %s', $cmd = $this->svnCmd(array('diff', '-c', $rev), $this->repo);
escapeshellarg($rev),
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($this->repo));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
return self::shell_exec('IDF_Scm_Svn::getDiff', $cmd); return self::shell_exec('IDF_Scm_Svn::getDiff', $cmd);
} }
@ -494,13 +439,8 @@ class IDF_Scm_Svn extends IDF_Scm
if ($this->validateRevision($commit) != IDF_Scm::REVISION_VALID) { if ($this->validateRevision($commit) != IDF_Scm::REVISION_VALID) {
return null; return null;
} }
$cmd = sprintf(Pluf::f('svn_path', 'svn').' log --xml -v --no-auth-cache -r %s --username=%s --password=%s %s', $cmd = $this->svnCmd(array('log', '--xml', '-v'), $this->repo, $commit);
escapeshellarg($commit),
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($this->repo));
$out = array(); $out = array();
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$out = self::shell_exec('IDF_Scm_Svn::getChanges', $cmd); $out = self::shell_exec('IDF_Scm_Svn::getChanges', $cmd);
$xml = simplexml_load_string($out); $xml = simplexml_load_string($out);
if (count($xml) == 0) { if (count($xml) == 0) {
@ -577,21 +517,15 @@ class IDF_Scm_Svn extends IDF_Scm
* *
* @return array Changes. * @return array Changes.
*/ */
public function getChangeLog($branch=null, $n=10) public function getChangeLog($rev=null, $n=10)
{ {
if ($branch != 'HEAD' and !preg_match('/^\d+$/', $branch)) { if ($rev != 'HEAD' and !preg_match('/^\d+$/', $rev)) {
// we accept only revisions or HEAD // we accept only revisions or HEAD
$branch = 'HEAD'; $rev = 'HEAD';
} }
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' log --no-auth-cache --xml -v --limit %s --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('log', '--xml', '-v', '--limit', $n),
escapeshellarg($n), $this->repo.'@'.$rev);
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($branch),
escapeshellarg($this->repo),
escapeshellarg($branch));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlRes = self::shell_exec('IDF_Scm_Svn::getChangeLog', $cmd); $xmlRes = self::shell_exec('IDF_Scm_Svn::getChangeLog', $cmd);
$xml = simplexml_load_string($xmlRes); $xml = simplexml_load_string($xmlRes);
foreach ($xml->logentry as $entry) { foreach ($xml->logentry as $entry) {
@ -617,13 +551,8 @@ class IDF_Scm_Svn extends IDF_Scm
public function getProperties($rev, $path='') public function getProperties($rev, $path='')
{ {
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' proplist --no-auth-cache --xml --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('proplist', '--xml'),
escapeshellarg($this->username), $this->repo.'/'.self::smartEncode($path), $rev);
escapeshellarg($this->password),
escapeshellarg($rev),
escapeshellarg($this->repo.'/'.self::smartEncode($path)),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlProps = self::shell_exec('IDF_Scm_Svn::getProperties', $cmd); $xmlProps = self::shell_exec('IDF_Scm_Svn::getProperties', $cmd);
$props = simplexml_load_string($xmlProps); $props = simplexml_load_string($xmlProps);
@ -652,14 +581,8 @@ class IDF_Scm_Svn extends IDF_Scm
private function getProperty($property, $rev, $path='') private function getProperty($property, $rev, $path='')
{ {
$res = array(); $res = array();
$cmd = sprintf(Pluf::f('svn_path', 'svn').' propget --no-auth-cache --xml %s --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('propget', $property, '--xml'),
escapeshellarg($property), $this->repo.'/'.self::smartEncode($path), $rev);
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($rev),
escapeshellarg($this->repo.'/'.self::smartEncode($path)),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlProp = self::shell_exec('IDF_Scm_Svn::getProperty', $cmd); $xmlProp = self::shell_exec('IDF_Scm_Svn::getProperty', $cmd);
$prop = simplexml_load_string($xmlProp); $prop = simplexml_load_string($xmlProp);
@ -676,17 +599,38 @@ class IDF_Scm_Svn extends IDF_Scm
public function getLastCommit($rev='HEAD') public function getLastCommit($rev='HEAD')
{ {
$xmlInfo = ''; $xmlInfo = '';
$cmd = sprintf(Pluf::f('svn_path', 'svn').' info --no-auth-cache --xml --username=%s --password=%s --revision=%s %s@%s', $cmd = $this->svnCmd(array('info', '--xml'), $this->repo, $rev);
escapeshellarg($this->username),
escapeshellarg($this->password),
escapeshellarg($rev),
escapeshellarg($this->repo),
escapeshellarg($rev));
$cmd = Pluf::f('idf_exec_cmd_prefix', '').$cmd;
$xmlInfo = self::shell_exec('IDF_Scm_Svn::getLastCommit', $cmd); $xmlInfo = self::shell_exec('IDF_Scm_Svn::getLastCommit', $cmd);
$xml = simplexml_load_string($xmlInfo); $xml = simplexml_load_string($xmlInfo);
return (string) $xml->entry->commit['revision']; return (string) $xml->entry->commit['revision'];
} }
private function svnCmd($args = array(), $repoarg = null, $revarg = null)
{
$cmdline = array();
$cmdline[] = Pluf::f('idf_exec_cmd_prefix', '');
$cmdline[] = Pluf::f('svn_path', 'svn');
$cmdline[] = '--no-auth-cache';
$cmdline[] = '--username='.escapeshellarg($this->username);
$cmdline[] = '--password='.escapeshellarg($this->password);
foreach ($args as $arg) {
$cmdline[] = escapeshellarg($arg);
}
if ($repoarg != null) {
if ($revarg != null) {
$repoarg .= '@'.$revarg;
}
$cmdline[] = escapeshellarg($repoarg);
}
if ($revarg != null) {
$cmdline[] = '--revision='.escapeshellarg($revarg);
}
return implode(' ', $cmdline);
}
} }