使用PHP编写的SVN类
<?php
/**
* svn 外部命令 类
*
* @author rubekid
*
* @todo comment need addslashes for svn commit
*
*/
class svnutils {
/**
*
* svn 账号
*/
const svn_username = "robot";
/**
* svn 密码
*/
const svn_password = "robot2013";
/**
* 配置文件目录 (任意指定一个临时目录,解决svn: warning: can't open file '/root/.subversion/servers': permission denied)
*/
const svn_config_dir = "/var/tmp/";
/**
* svn list
*
* @param $repository string
* @return boolean
*
*/
public static function ls($repository) {
$command = "sudo svn ls " . $repository;
$output = self::runcmd ( $command );
$output = implode ( "<br />", $output );
if (strpos ( $output, 'non-existent in that revision' )) {
return false;
}
return "<br />" . $command . "<br />" . $output;
}
/**
* svn copy
*
* @param $src string
* @param $dst string
* @param $comment string
* @return boolean
*
*/
public static function copy($src, $dst, $comment) {
$command = "sudo svn cp $src $dst -m '$comment'";
$output = self::runcmd ( $command );
$output = implode ( "<br />", $output );
if (strpos ( $output, 'committed revision' )) {
return true;
}
return "<br />" . $command . "<br />" . $output;
}
/**
* svn delete
*
* @param $url string
* @param $comment string
* @return boolean
*
*/
public static function delete($url, $comment) {
$command = "sudo svn del $url -m '$comment'";
$output = self::runcmd ( $command );
$output = implode ( '<br />', $output );
if (strpos ( $output, 'committed revision' )) {
return true;
}
return "<br />" . $command . "<br />" . $output;
}
/**
* svn move
*
* @param $src string
* @param $dst string
* @param $comment string
* @return boolean
*/
public static function move($src, $dst, $comment) {
$command = "sudo svn mv $src $dst -m '$comment'";
$output = self::runcmd ( $command );
$output = implode ( '<br />', $output );
if (strpos ( $output, 'committed revision' )) {
return true;
}
return "<br />" . $command . "<br />" . $output;
}
/**
* svn mkdir
*
* @param $url string
* @param $comment string
* @return boolean
*/
public static function mkdir($url, $comment) {
$command = "sudo svn mkdir $url -m '$comment'";
$output = self::runcmd ( $command );
$output = implode ( '<br />', $output );
if (strpos ( $output, 'committed revision' )) {
return true;
}
return "<br />" . $command . "<br />" . $output;
}
/**
* svn diff
* @param $patha string
* @param $pathb string
* @return string
*/
public static function diff($patha, $pathb) {
$output = self::runcmd ( "sudo svn diff $patha $pathb" );
return implode ( '<br />', $output );
}
/**
* svn checkout
* @param $url string
* @param $dir string
* @return boolean
*/
public static function checkout($url, $dir) {
$command = "cd $dir && sudo svn co $url";
$output = self::runcmd ( $command );
$output = implode ( '<br />', $output );
if (strstr ( $output, 'checked out revision' )) {
return true;
}
return "<br />" . $command . "<br />" . $output;
}
/**
* svn update
* @param $path string
*/
public static function update($path) {
$command = "cd $path && sudo svn up";
$output = self::runcmd ( $command );
$output = implode ( '<br />', $output );
preg_match_all ( "/[0-9]+/", $output, $ret );
if (! $ret [0] [0]) {
return "<br />" . $command . "<br />" . $output;
}
return $ret [0] [0];
}
/**
* svn merge
*
* @param $revision string
* @param $url string
* @param $dir string
*
* @return boolean
*/
public static function merge($revision, $url, $dir) {
$command = "cd $dir && sudo svn merge -r1:$revision $url";
$output = implode ( '<br />', self::runcmd ( $command ) );
if (strstr ( $output, 'text conflicts' )) {
return 'command: ' . $command . '<br />' . $output;
}
return true;
}
/**
* svn commit
*
* @param $dir string
* @param $comment string
*
* @return boolean
*/
public static function commit($dir, $comment) {
$command = "cd $dir && sudo svn commit -m'$comment'";
$output = implode ( '<br />', self::runcmd ( $command ) );
if (strpos ( $output, 'committed revision' ) || empty ( $output )) {
return true;
}
return $output;
}
/**
* svn status (输出wc中文件和目录的状态)
*
* @param $dir string
*/
public static function getstatus($dir) {
$command = "cd $dir && sudo svn st";
return self::runcmd ( $command );
}
/**
* svn 冲突
*
* @param $dir string
* @return boolean
*/
public static function hasconflict($dir) {
$output = self::getstatus ( $dir );
foreach ( $output as $line ) {
if ( substr ( trim ( $line ), 0, 1 ) == 'c' || (substr ( trim ( $line ), 0, 1 ) == '!')) {
return true;
}
}
return false;
}
/**
* svn log
*
* @param $path string
* @return string
*
*/
public static function getlog($path) {
$command = "sudo svn log $path --xml";
$output = self::runcmd ( $command );
return implode ( '', $output );
}
/**
* svn info
* @param $path string
*/
public static function getpathrevision($path) {
$command = "sudo svn info $path --xml";
$output = self::runcmd ( $command );
$string = implode ( '', $output );
$xml = new simplexmlelement ( $string );
foreach ( $xml->entry [0]->attributes () as $key => $value ) {
if ( $key == 'revision' ) {
return $value;
}
}
}
/**
* 获取最新版本号
* @param $path string
*/
public static function getheadrevision($path) {
$command = "cd $path && sudo svn up";
$output = self::runcmd ( $command );
$output = implode ( '<br />', $output );
preg_match_all ( "/[0-9]+/", $output, $ret );
if (! $ret [0] [0]) {
return "<br />" . $command . "<br />" . $output;
}
return $ret [0] [0];
}
/**
* 获取某文件最早版本号
*
* @param $filepath string
*
*/
public static function getfilefirstversion($filepath){
$command = "sudo svn log {$filepath}";
$output = self::runcmd ( $command , "|grep -i ^r[0-9]* |awk '{print $1}'");
if(empty($output)){
return false;
}
return str_replace("r", '', $output[count($output)-1]);
}
/**
* 获取两个版本间修改的文件信息列表
*
* @param $fromversion int
* @param $headrevision int
* @param $$path string
*
* @return array
*/
public static function getchangedfiles($path, $fromversion, $headrevision ){
$files = array();
$pipe = "|grep -i ^index:|awk -f : '{print $2}'";
$command = "svn diff -r {$fromversion}:{$headrevision} $path";
$output = self::runcmd ( $command ,$pipe);
$files = array_merge($files, $output);
$command = "svn diff -r {$headrevision}:{$fromversion} $path"; //文件删除可用逆向对比
$output = self::runcmd ( $command ,$pipe);
$files = array_merge($files, $output);
return array_unique($files);
}
/**
* 获取两个版本间某文件修改 的内容
*
* @param $filepath string
* @param $fromversion int
* @param $headrevision int
*
* @return array
*/
public static function getchangedinfo( $filepath, $fromversion, $headrevision ){
$command = "sudo svn diff -r {$fromversion}:{$headrevision} $filepath";
$output = self::runcmd ( $command );
return $output;
}
/**
* 查看文件内容
*
* @param $filepath string
* @param $version int
*
* @return array
*/
public static function getfilecontent($filepath, $version){
$command = "sudo svn cat -r {$version} $filepath";
$output = self::runcmd ( $command );
return $output;
}
/**
* run a cmd and return result
* @param $command string
* @param $pipe string (可以增加管道对返回数据进行预筛选)
* @return array
*/
protected static function runcmd($command , $pipe ="") {
$authcommand = ' --username ' . self::svn_username . ' --password ' . self::svn_password . ' --no-auth-cache --non-interactive --config-dir ' . self::svn_config_dir . '.subversion';
exec ( $command . $authcommand . " 2>&1" . $pipe, $output );
return $output;
}
}