<?php
/
SVN 外部号令 类
@author rubekid
@todo comment need addslashes for svn commit
/
class SvnUtils {
/
svn 账号
/
const SVN_USERNAME = rubekid;
/
svn 暗码
/
const SVN_PASSWORD = 123456;
/
设备文件目次 (随便率性指定一个姑且目次,解决svn: warning: Cant open file /root/.subversion/servers: Permission denied)
/
const SVN_CONFIG_DIR = /var/tmp/;
/
svn list
@param ¥repository string
@return boolean
/
static public function ls(¥repository) {
¥command = 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
/
static public function copy(¥src, ¥dst, ¥comment) {
¥command = 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
@param ¥url string
@param ¥comment string
@return boolean
/
static public function (¥url, ¥comment) {
¥command = 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
/
static public function move(¥src, ¥dst, ¥comment) {
¥command = 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
/
static public function mkdir(¥url, ¥comment) {
¥command = 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
/
static public function diff(¥pathA, ¥pathB) {
¥output = self::runCmd ( svn diff ¥pathA ¥pathB );
return implode ( <br />, ¥output );
}
/
svn checkout
@param ¥url string
@param ¥dir string
@return boolean
/
static public function checkout(¥url, ¥dir) {
¥command = cd ¥dir && 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
@param ¥path string
/
static public function (¥path) {
¥command = cd ¥path && 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
/
static public function merge(¥revision, ¥url, ¥dir) {
¥command = cd ¥dir && 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
/
static public function commit(¥dir, ¥comment) {
¥command = cd ¥dir && 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
/
static public function getStatus(¥dir) {
¥command = cd ¥dir && svn st;
return self::runCmd ( ¥command );
}
/
svn 冲突
@param ¥dir string
@return boolean
/
static public 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
/
static public function getLog(¥path) {
¥command = svn log ¥path --xml;
¥output = self::runCmd ( ¥command );
return implode ( , ¥output );
}
/
svn info
@param ¥path string
/
static public function getPathRevision(¥path) {
¥command = 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;
}
}
}
/
svn up
@param ¥path string
/
static public function getHeadRevision(¥path) {
¥command = cd ¥path && 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];
}
/
Run a cmd and return result
@param ¥command string
@return array
/
static protected function runCmd(¥command) {
¥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, ¥output );
return ¥output;
}
}