SvnPeer. php
Copy codeThe Code is as follows:
<? Php
/**
*
* This class for execute the external program of svn
*
* @ Auth Seven Yang <qineer@gmail.com>
*
*/
Class SvnPeer
{
/**
* List directory entries in the repository
*
* @ Param string a specific project repository path
* @ Return bool true, if validated successfully, otherwise false
*/
Static public function ls ($ repository)
{
$ Command = "svn ls". $ repository;
$ Output = SvnPeer: runCmd ($ command );
$ Output = implode ("<br>", $ output );
If (strpos ($ output, 'non-existent in that revision ')){
Return false;
}
Return "<br>". $ command. "<br>". $ output;
}
/**
* Duplicate something in working copy or repository, remembering history
*
* @ Param $ src
* @ Param $ dst
* @ Param $ comment string specify log message
* @ Return bool true, if copy successfully, otherwise return the error message
*
* @ Todo comment need addslashes for svn commit
*/
Static public function copy ($ src, $ dst, $ comment)
{
$ Command = "svn cp $ src $ dst-m' $ comment '";
$ Output = SvnPeer: runCmd ($ command );
$ Output = implode ("<br>", $ output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return "<br>". $ command. "<br>". $ output;
}
/**
* Remove files and directories from version control
*
* @ Param $ url
* @ Return bool true, if delete successfully, otherwise return the error message
*
* @ Todo comment need addslashes for svn commit
*/
Static public function delete ($ url, $ comment)
{
$ Command = "svn del $ url-m' $ comment '";
$ Output = SvnPeer: runCmd ($ command );
$ Output = implode ('<br>', $ output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return "<br>". $ command. "<br>". $ output;
}
/**
* Move and/or rename something in working copy or repository
*
* @ Param $ src string trunk path
* @ Param $ dst string new branch path
* @ Param $ comment string specify log message
* @ Return bool true, if move successfully, otherwise return the error message
*
* @ Todo comment need addslashes for svn commit
*/
Static public function move ($ src, $ dst, $ comment)
{
$ Command = "svn mv $ src $ dst-m' $ comment '";
$ Output = SvnPeer: runCmd ($ command );
$ Output = implode ('<br>', $ output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return "<br>". $ command. "<br>". $ output;
}
/**
* Create a new directory under version control
*
* @ Param $ url string
* @ Param $ comment string the svn message
* @ Return bool true, if create successfully, otherwise return the error message
*
* @ Todo comment need addslashes for svn commit
*/
Static public function mkdir ($ url, $ comment)
{
$ Command = "svn mkdir $ url-m' $ comment '";
$ Output = SvnPeer: runCmd ($ command );
$ Output = implode ('<br>', $ output );
If (strpos ($ output, 'committed revision ')){
Return true;
}
Return "<br>". $ command. "<br>". $ output;
}
Static public function diff ($ pathA, $ pathB)
{
$ Output = SvnPeer: runCmd ("svn diff $ pathA $ pathB ");
Return implode ('<br>', $ output );
}
Static public function checkout ($ url, $ dir)
{
$ Command = "cd $ dir & svn co $ url ";
$ Output = SvnPeer: runCmd ($ command );
$ Output = implode ('<br>', $ output );
If (strstr ($ output, 'checked out revision ')){
Return true;
}
Return "<br>". $ command. "<br>". $ output;
}
Static public function update ($ path)
{
$ Command = "cd $ path & svn up ";
$ Output = SvnPeer: 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];
}
Static public function merge ($ revision, $ url, $ dir)
{
$ Command = "cd $ dir & svn merge-r1: $ revision $ url ";
$ Output = implode ('<br>', SvnPeer: runCmd ($ command ));
If (strstr ($ output, 'text conflicts ')){
Return 'COMMAND: '. $ Command.' <br> '. $ output;
}
Return true;
}
Static public function commit ($ dir, $ comment)
{
$ Command = "cd $ dir & svn commit-m' $ comment '";
$ Output = implode ('<br>', SvnPeer: runCmd ($ command ));
If (strpos ($ output, 'committed revision') | empty ($ output )){
Return true;
}
Return $ output;
}
Static public function getStatus ($ dir)
{
$ Command = "cd $ dir & svn st ";
Return SvnPeer: runCmd ($ command );
}
Static public function hasConflict ($ dir)
{
$ Output = SvnPeer: getStatus ($ dir );
Foreach ($ output as $ line ){
If ('c' = substr (trim ($ line), 0, 1) | ('! '= Substr (trim ($ line), 0, 1 ))){
Return true;
}
}
Return false;
}
/**
* Show the log messages for a set of path with XML
*
* @ Param path string
* @ Return log message string
*/
Static public function getLog ($ path)
{
$ Command = "svn log $ path -- xml ";
$ Output = SvnPeer: runCmd ($ command );
Return implode ('', $ output );
}
Static public function getPathRevision ($ path)
{
$ Command = "svn info $ path -- xml ";
$ Output = SvnPeer: runCmd ($ command );
$ String = implode ('', $ output );
$ Xml = new SimpleXMLElement ($ string );
Foreach ($ xml-> entry [0]-> attributes () as $ key => $ value ){
If ('review' = $ key ){
Return $ value;
}
}
}
Static public function getHeadRevision ($ path)
{
$ Command = "cd $ path & svn up ";
$ Output = SvnPeer: 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 string command line
* @ Param boolen true need add the svn authentication
* @ Return array the contents of the output that svn execute
*/
Static protected function runCmd ($ command)
{
$ AuthCommand = '-- username '. SVN_USERNAME. '-- password '. SVN_PASSWORD. '-- no-auth-cache -- non-interactive -- config-dir '. SVN_CONFIG_DIR. '. subversion ';
Exec ($ command. $ authCommand. "2> & 1", $ output );
Return $ output;
}
}