<?php
/**
* SVN 外部命令 類
*
* @author rubekid
*
* @todo comment need addslashes for svn commit
*
*/
class SvnUtils {
/**
*
* svn 賬號
*/
const SVN_USERNAME = "robot";
/**
* svn 密碼
*/
const SVN_PASSWORD = "robot
/**
* 配置文件目錄 (任意指定一個臨時目錄
*/
const SVN_CONFIG_DIR = "/var/tmp/";
/**
* svn list
*
* @param $repository string
* @return boolean
*
*/
public static function ls($repository) {
$command = "sudo svn ls "
$output = self::runCmd ( $command );
$output = implode ( "<br />"
if (strpos ( $output
return false;
}
return "<br />"
}
/**
* svn copy
*
* @param $src string
* @param $dst string
* @param $comment string
* @return boolean
*
*/
public static function copy($src
$command = "sudo svn cp $src $dst
$output = self::runCmd ( $command );
$output = implode ( "<br />"
if (strpos ( $output
return true;
}
return "<br />"
}
/**
* svn delete
*
* @param $url string
* @param $comment string
* @return boolean
*
*/
public static function delete($url
$command = "sudo svn del $url
$output = self::runCmd ( $command );
$output = implode (
if (strpos ( $output
return true;
}
return "<br />"
}
/**
* svn move
*
* @param $src string
* @param $dst string
* @param $comment string
* @return boolean
*/
public static function move($src
$command = "sudo svn mv $src $dst
$output = self::runCmd ( $command );
$output = implode (
if (strpos ( $output
return true;
}
return "<br />"
}
/**
* svn mkdir
*
* @param $url string
* @param $comment string
* @return boolean
*/
public static function mkdir($url
$command = "sudo svn mkdir $url
$output = self::runCmd ( $command );
$output = implode (
if (strpos ( $output
return true;
}
return "<br />"
}
/**
* svn diff
* @param $pathA string
* @param $pathB string
* @return string
*/
public static function diff($pathA
$output = self::runCmd ( "sudo svn diff $pathA $pathB" );
return implode (
}
/**
* svn checkout
* @param $url string
* @param $dir string
* @return boolean
*/
public static function checkout($url
$command = "cd $dir && sudo svn co $url";
$output = self::runCmd ( $command );
$output = implode (
if (strstr ( $output
return true;
}
return "<br />"
}
/**
* svn update
* @param $path string
*/
public static function update($path) {
$command = "cd $path && sudo svn up";
$output = self::runCmd ( $command );
$output = implode (
preg_match_all ( "/[
if (! $ret [
return "<br />"
}
return $ret [
}
/**
* svn merge
*
* @param $revision string
* @param $url string
* @param $dir string
*
* @return boolean
*/
public static function merge($revision
$command = "cd $dir && sudo svn merge
$output = implode (
if (strstr ( $output
return
}
return true;
}
/**
* svn commit
*
* @param $dir string
* @param $comment string
*
* @return boolean
*/
public static function commit($dir
$command = "cd $dir && sudo svn commit
$output = implode (
if (strpos ( $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 )
return true;
}
}
return false;
}
/**
* svn log
*
* @param $path string
* @return string
*
*/
public static function getLog($path) {
$command = "sudo svn log $path
$output = self::runCmd ( $command );
return implode (
}
/**
* svn info
* @param $path string
*/
public static function getPathRevision($path) {
$command = "sudo svn info $path
$output = self::runCmd ( $command );
$string = implode (
$xml = new SimpleXMLElement ( $string );
foreach ( $xml
if ( $key ==
return $value;
}
}
}
/**
* 獲取最新版本號
* @param $path string
*/
public static function getHeadRevision($path) {
$command = "cd $path && sudo svn up";
$output = self::runCmd ( $command );
$output = implode (
preg_match_all ( "/[
if (! $ret [
return "<br />"
}
return $ret [
}
/**
* 獲取某文件最早版本號
*
* @param $filePath string
*
*/
public static function getFileFirstVersion($filePath){
$command = "sudo svn log {$filePath}";
$output = self::runCmd ( $command
if(empty($output)){
return false;
}
return str_replace("r"
}
/**
* 獲取兩個版本間修改的文件信息列表
*
* @param $fromVersion int
* @param $headRevision int
* @param $$path string
*
* @return array
*/
public static function getChangedFiles($path
$files = array();
$pipe = "|grep
$command = "svn diff
$output = self::runCmd ( $command
$files = array_merge($files
$command = "svn diff
$output = self::runCmd ( $command
$files = array_merge($files
return array_unique($files);
}
/**
* 獲取兩個版本間某文件修改 的內容
*
* @param $filePath string
* @param $fromVersion int
* @param $headRevision int
*
* @return array
*/
public static function getChangedInfo( $filePath
$command = "sudo svn diff
$output = self::runCmd ( $command );
return $output;
}
/**
* 查看文件內容
*
* @param $filePath string
* @param $version int
*
* @return array
*/
public static function getFileContent($filePath
$command = "sudo svn cat
$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
$authCommand =
exec ( $command
return $output;
}
}
From:http://tw.wingwit.com/Article/program/PHP/201311/21310.html