SVN class written in PHP. Copy the code as follows :? Php *** SVN external command class ** @ authorrubekid ** @ todocommentneedaddslashesforsvncommit ** classSvnUtils {*** svn account * constSV
The code is as follows:
/**
* SVN external command class
*
* @ Author rubekid
*
* @ Todo comment need addslashes for svn commit
*
*/
Class SvnUtils {
/**
*
* Svn account
*/
Const SVN_USERNAME = "robot ";
/**
* Svn password
*/
Const SVN_PASSWORD = "robot2013 ";
/**
* Configuration file directory (any temporary directory specified to solve 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 ("
", $ Output );
If (strpos ($ output, 'non-existent in that revision ')){
Return false;
}
Return"
". $ Command ."
". $ 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 ("
", $ Output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return"
". $ Command ."
". $ 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 ('
', $ Output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return"
". $ Command ."
". $ 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 ('
', $ Output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return"
". $ Command ."
". $ 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 ('
', $ Output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return"
". $ Command ."
". $ 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 ('
', $ 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 ('
', $ Output );
If (strstr ($ output, 'checked out revision ')){
Return true;
}
Return"
". $ Command ."
". $ Output;
}
/**
* Svn update
* @ Param $ path string
*/
Public static function update ($ path ){
$ Command = "cd $ path & sudo svn up ";
$ Output = self: runCmd ($ command );
$ Output = implode ('
', $ Output );
Preg_match_all ("/[0-9] +/", $ output, $ ret );
If (! $ Ret [0] [0]) {
Return"
". $ Command ."
". $ 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 ('
', Self: runCmd ($ command ));
If (strstr ($ output, 'text conflicts ')){
Return 'command: '. $ Command .'
'. $ 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 ('
', Self: runCmd ($ command ));
If (strpos ($ output, 'committed revision') | empty ($ output )){
Return true;
}
Return $ output;
}
/**
* Svn status (output state of files and directories in WC)
*
* @ Param $ dir string
*/
Public static function getStatus ($ dir ){
$ Command = "cd $ dir & sudo svn st ";
Return self: runCmd ($ command );
}
/**
* Svn conflict
*
* @ 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;
}
}
}
/**
* Obtain the latest version number.
* @ Param $ path string
*/
Public static function getHeadRevision ($ path ){
$ Command = "cd $ path & sudo svn up ";
$ Output = self: runCmd ($ command );
$ Output = implode ('
', $ Output );
Preg_match_all ("/[0-9] +/", $ output, $ ret );
If (! $ Ret [0] [0]) {
Return"
". $ Command ."
". $ Output;
}
Return $ ret [0] [0];
}
/**
* Obtain the earliest version number of a file.
*
* @ 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]);
}
/**
* Get the list of files modified between two versions
*
* @ 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"; // reverse Comparison of file deletion availability
$ Output = self: runCmd ($ command, $ pipe );
$ Files = array_merge ($ files, $ output );
Return array_unique ($ files );
}
/**
* Obtain the modified content of a file between two versions.
*
* @ 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;
}
/**
* View file content
*
* @ 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 (you can add pipelines to pre-filter the returned data)
* @ 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;
}
}
The http://www.bkjia.com/PHPjc/328034.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328034.htmlTechArticle code is as follows :? Php/*** SVN external command class ** @ author rubekid ** @ todo comment need addslashes for svn commit **/class SvnUtils {/*** svn account */const SV...