使用PHP編寫的SVN類
<?php
/**
* SVN 外部命令 類
*
* @author rubekid
*
* @todo comment need addslashes for svn commit
*
*/
class SvnUtils {
/**
*
* svn 賬號(hào)
*/
const SVN_USERNAME = "robot";
/**
* svn 密碼
*/
const SVN_PASSWORD = "robot2013";
/**
* 配置文件目錄 (任意指定一個(gè)臨時(shí)目錄,解決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中文件和目錄的狀態(tài))
*
* @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;
}
}
}
/**
* 獲取最新版本號(hào)
* @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];
}
/**
* 獲取某文件最早版本號(hào)
*
* @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]);
}
/**
* 獲取兩個(gè)版本間修改的文件信息列表
*
* @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"; //文件刪除可用逆向?qū)Ρ?BR> $output = self::runCmd ( $command ,$pipe);
$files = array_merge($files, $output);
return array_unique($files);
}
/**
* 獲取兩個(gè)版本間某文件修改 的內(nèi)容
*
* @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;
}
/**
* 查看文件內(nèi)容
*
* @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 (可以增加管道對(duì)返回?cái)?shù)據(jù)進(jìn)行預(yù)篩選)
* @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;
}
}
相關(guān)文章
PHP 圖像處理與SESSION制作超簡(jiǎn)單驗(yàn)證碼的方法示例
這篇文章主要介紹了PHP 圖像處理與SESSION制作超簡(jiǎn)單驗(yàn)證碼的方法,結(jié)合實(shí)例形式詳細(xì)分析了PHP結(jié)合session繪制圖形驗(yàn)證碼相關(guān)操作技巧,需要的朋友可以參考下2019-12-12深入解析fsockopen與pfsockopen的區(qū)別
本篇文章是對(duì)fsockopen與pfsockopen的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07PHP5.3的垃圾回收機(jī)制(動(dòng)態(tài)存儲(chǔ)分配方案)深入理解
垃圾回收機(jī)制是一種動(dòng)態(tài)存儲(chǔ)分配方案,它會(huì)自動(dòng)釋放程序不再需要的已分配的內(nèi)存塊,PHP也在語言層實(shí)現(xiàn)了內(nèi)存的動(dòng)態(tài)管理.內(nèi)存的動(dòng)態(tài)管理將開發(fā)人員從繁瑣的內(nèi)存管理中解救出來2012-12-12真正根據(jù)utf8編碼的規(guī)律來進(jìn)行截取字符串的函數(shù)(utf8版sub_str )
真正根據(jù)utf8編碼的規(guī)律來進(jìn)行截取的字符的函數(shù),utf8版sub_str 支持1~6個(gè)字節(jié)的字符的截取,而非只針對(duì)中文,比網(wǎng)上的全2012-10-10PHP實(shí)現(xiàn)獲取文件后綴名的幾種常用方法
這篇文章主要介紹了PHP實(shí)現(xiàn)獲取文件后綴名的幾種常用方法,通過三種不同的方法實(shí)例分析了php獲取文件后綴名的實(shí)現(xiàn)技巧,分別通過字符串、文件屬性及數(shù)組等方式實(shí)現(xiàn)這一功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08PHP+JQuery+Ajax實(shí)現(xiàn)分頁方法詳解
這篇文章主要介紹了PHP+JQuery+Ajax實(shí)現(xiàn)分頁的方法,結(jié)合實(shí)例形式詳細(xì)分析了php數(shù)據(jù)查詢、分頁設(shè)置及ajax交互的相關(guān)技巧,并總結(jié)了分頁的相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-08-08php cookie的操作實(shí)現(xiàn)代碼(登錄)
cookie 常用于識(shí)別用戶。cookie 是服務(wù)器留在用戶計(jì)算機(jī)中的小文件。每當(dāng)相同的計(jì)算機(jī)通過瀏覽器請(qǐng)求頁面時(shí),它同時(shí)會(huì)發(fā)送 cookie。通過 PHP,您能夠創(chuàng)建并取回 cookie 的值。2010-12-12