Skip to content

Instantly share code, notes, and snippets.

@riaf
Created September 1, 2009 09:29
Show Gist options
  • Select an option

  • Save riaf/179005 to your computer and use it in GitHub Desktop.

Select an option

Save riaf/179005 to your computer and use it in GitHub Desktop.
<?php
class Subversion extends Object
{
static protected $_cmd_path_ = '/usr/bin/svn';
protected $_result_;
static public function config_path($cmd_path){
self::$_cmd_path_ = $cmd_path;
}
static public function cmd_path(){
return self::$_cmd_path_;
}
static public function exec_cmd($cmd){
ob_start();
passthru($cmd);
return ob_get_clean();
}
public function exec(){
return $this->__exec__();
}
protected function __exec__(){
throw new SubversionException('method is not defined');
}
}
class SubversionException extends Exception
{
}
class SubversionCat extends Subversion
{
protected $_command_ = 'cat';
protected $repos_url;
protected $revision_no;
protected function __exec__(){
$ret = is_null($this->revision_no)? svn_cat($this->repos_url): svn_cat($this->repos_url, $this->revision_no);
if($ret === false){
throw new SubversionException();
}
return $ret;
}
}
if(!function_exists('svn_cat')){
function svn_cat($repos_url, $revision_no=null){
$cmd = sprintf('%s cat %s%s',
Subversion::cmd_path(),
is_null($revision_no)? '': sprintf('--revision=%d ', $revision_no),
escapeshellarg($repos_url));
$ret = Subversion::exec_cmd($cmd);
// TODO: errorhand
return $ret;
}
}
class SubversionExport extends Subversion
{
protected $_command_ = 'export';
protected $frompath;
protected $topath;
protected $working_copy = true;
protected function __exec__(){
$ret = svn_export($this->frompath, $this->topath, $this->working_copy);
if($ret === false){
throw new SubversionException();
}
return true;
}
}
if(!function_exists('svn_export')){
function svn_export($frompath, $topath, $working_copy=true){
$cmd = sprintf('%s export %s %s', Subversion::cmd_path(), escapeshellarg($frompath), escapeshellarg($topath));
$ret = Subversion::exec_cmd($cmd);
$bool = (strpos($ret, 'Export complete.') !== false || strpos($ret, 'Exported revision') !== false);
if($bool === false){
throw new SubversionException();
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment