/** * * This class for execute the external program of SVN * * @auth Seven Yang http://www.bKjia.c0m * */ 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 (" ", $output); if (Strpos ($output, ' non-existent in that revision ')) { return false; } Return " " . $command. " " . $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 (" ", $output); if (Strpos ($output, ' Committed Revision ')) { return true; } Return " " . $command. " " . $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 (' ', $output); if (Strpos ($output, ' Committed Revision ')) { return true; } Return " " . $command. " " . $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 (' ', $output); if (Strpos ($output, ' Committed Revision ')) { return true; } Return " " . $command. " " . $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 (' ', $output); if (Strpos ($output, ' Committed Revision ')) { return true; } Return " " . $command. " " . $output; } static Public function diff ($pathA, $pathB) { $output = Svnpeer::runcmd ("svn diff $pathA $pathB"); Return implode (' ', $output); } static public Function Checkout ($url, $dir) { $command = "CD $dir && SVN co $url"; $output = Svnpeer::runcmd ($command); $output = Implode (' ', $output); if (Strstr ($output, ' Checked out Revision ')) { return true; } Return " " . $command. " " . $output; } static public Function Update ($PATH) { $command = "CD $path && svn up"; $output = Svnpeer::runcmd ($command); $output = Implode (' ', $output); Preg_match_all ("/[0-9]+/", $output, $ret); if (! $ret [0][0]) { Return " " . $command. " " . $output; } return $ret [0][0]; } static public Function merge ($revision, $url, $dir) { $command = "CD $dir && svn merge-r1: $revision $url"; $output = Implode (' ', Svnpeer::runcmd ($command)); if (Strstr ($output, ' Text conflicts ')) { Return ' Command: '. $command. ' '. $output; } return true; } static public Function commit ($dir, $comment) { $command = "CD $dir && svn commit-m ' $comment '"; $output = Implode (' ', 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 (' revision ' = = $key) { return $value; } } } static public Function getheadrevision ($path) { $command = "CD $path && svn up"; $output = Svnpeer::runcmd ($command); $output = Implode (' ', $output); Preg_match_all ("/[0-9]+/", $output, $ret); if (! $ret [0][0]) { Return " " . $command. " " . $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; } }
|