SvnPeer.php
<BR><?php <BR>/** <BR>* <BR>* This class for execute the external program of svn <BR>* <BR>* @auth Seven Yang <BR>* <BR>*/ <BR>class SvnPeer <BR>{ <BR>/** <BR>* List directory entries in the repository <BR>* <BR>* @param string a specific project repository path <BR>* @return bool true, if validated successfully, otherwise false <BR>*/ <BR>static public function ls($repository) <BR>{ <BR>$command = "svn ls " . $repository; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode("<br>", $output); <BR>if (strpos($output, 'non-existent in that revision')) { <BR>return false; <BR>} <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>/** <BR>* Duplicate something in working copy or repository, remembering history <BR>* <BR>* @param $src <BR>* @param $dst <BR>* @param $comment string specify log message <BR>* @return bool true, if copy successfully, otherwise return the error message <BR>* <BR>* @todo comment need addslashes for svn commit <BR>*/ <BR>static public function copy($src, $dst, $comment) <BR>{ <BR>$command = "svn cp $src $dst -m '$comment'"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode("<br>", $output); <BR>if (strpos($output, 'Committed revision')) { <BR>return true; <BR>} <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>/** <BR>* Remove files and directories from version control <BR>* <BR>* @param $url <BR>* @return bool true, if delete successfully, otherwise return the error message <BR>* <BR>* @todo comment need addslashes for svn commit <BR>*/ <BR>static public function delete($url, $comment) <BR>{ <BR>$command = "svn del $url -m '$comment'"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode('<br>', $output); <BR>if (strpos($output, 'Committed revision')) { <BR>return true; <BR>} <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>/** <BR>* Move and/or rename something in working copy or repository <BR>* <BR>* @param $src string trunk path <BR>* @param $dst string new branch path <BR>* @param $comment string specify log message <BR>* @return bool true, if move successfully, otherwise return the error message <BR>* <BR>* @todo comment need addslashes for svn commit <BR>*/ <BR>static public function move($src, $dst, $comment) <BR>{ <BR>$command = "svn mv $src $dst -m '$comment'"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode('<br>', $output); <BR>if (strpos($output, 'Committed revision')) { <BR>return true; <BR>} <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>/** <BR>* Create a new directory under version control <BR>* <BR>* @param $url string <BR>* @param $comment string the svn message <BR>* @return bool true, if create successfully, otherwise return the error message <BR>* <BR>* @todo comment need addslashes for svn commit <BR>*/ <BR>static public function mkdir($url, $comment) <BR>{ <BR>$command = "svn mkdir $url -m '$comment'"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode('<br>', $output); <BR>if (strpos($output, 'Committed revision')) { <BR>return true; <BR>} <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>static public function diff($pathA, $pathB) <BR>{ <BR>$output = SvnPeer::runCmd("svn diff $pathA $pathB"); <BR>return implode('<br>', $output); <BR>} <BR>static public function checkout($url, $dir) <BR>{ <BR>$command = "cd $dir && svn co $url"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode('<br>', $output); <BR>if (strstr($output, 'Checked out revision')) { <BR>return true; <BR>} <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>static public function update($path) <BR>{ <BR>$command = "cd $path && svn up"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode('<br>', $output); <BR>preg_match_all("/[0-9]+/", $output, $ret); <BR>if (!$ret[0][0]){ <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>return $ret[0][0]; <BR>} <BR>static public function merge($revision, $url, $dir) <BR>{ <BR>$command = "cd $dir && svn merge -r1:$revision $url"; <BR>$output = implode('<br>', SvnPeer::runCmd($command)); <BR>if (strstr($output, 'Text conflicts')) { <BR>return 'Command: ' . $command .'<br>'. $output; <BR>} <BR>return true; <BR>} <BR>static public function commit($dir, $comment) <BR>{ <BR>$command = "cd $dir && svn commit -m'$comment'"; <BR>$output = implode('<br>', SvnPeer::runCmd($command)); <BR>if (strpos($output, 'Committed revision') || empty($output)) { <BR>return true; <BR>} <BR>return $output; <BR>} <BR>static public function getStatus($dir) <BR>{ <BR>$command = "cd $dir && svn st"; <BR>return SvnPeer::runCmd($command); <BR>} <BR>static public function hasConflict($dir) <BR>{ <BR>$output = SvnPeer::getStatus($dir); <BR>foreach ($output as $line){ <BR>if ('C' == substr(trim($line), 0, 1) || ('!' == substr(trim($line), 0, 1))){ <BR>return true; <BR>} <BR>} <BR>return false; <BR>} <BR>/** <BR>* Show the log messages for a set of path with XML <BR>* <BR>* @param path string <BR>* @return log message string <BR>*/ <BR>static public function getLog($path) <BR>{ <BR>$command = "svn log $path --xml"; <BR>$output = SvnPeer::runCmd($command); <BR>return implode('', $output); <BR>} <BR>static public function getPathRevision($path) <BR>{ <BR>$command = "svn info $path --xml"; <BR>$output = SvnPeer::runCmd($command); <BR>$string = implode('', $output); <BR>$xml = new SimpleXMLElement($string); <BR>foreach ($xml->entry[0]->attributes() as $key=>$value){ <BR>if ('revision' == $key) { <BR>return $value; <BR>} <BR>} <BR>} <BR>static public function getHeadRevision($path) <BR>{ <BR>$command = "cd $path && svn up"; <BR>$output = SvnPeer::runCmd($command); <BR>$output = implode('<br>', $out<a>本2文来*源gao($daima.com搞@代@#码(网</a><strong>搞gaodaima代码</strong>put); <BR>preg_match_all("/[0-9]+/", $output, $ret); <BR>if (!$ret[0][0]){ <BR>return "<br>" . $command . "<br>" . $output; <BR>} <BR>return $ret[0][0]; <BR>} <BR>/** <BR>* Run a cmd and return result <BR>* <BR>* @param string command line <BR>* @param boolen true need add the svn authentication <BR>* @return array the contents of the output that svn execute <BR>*/ <BR>static protected function runCmd($command) <BR>{ <BR>$authCommand = ' --username ' . SVN_USERNAME . ' --password ' . SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir '.SVN_CONFIG_DIR.'.subversion'; <BR>exec($command . $authCommand . " 2>&1", $output); <BR>return $output; <BR>} <BR>} <BR>