php教程 php手册 php操作SVN类

php操作SVN类

Jun 21, 2016 am 08:52 AM
command output quot return

使用PHP完成SVN的操作,包括复制,查看列表,删除,移动,创建目录,查看diff,更新,合并,提交,获取状态,获取commit log,获取当前版本号操作。在svn 1.6.11版本中测试通过。

 

/**
*
* This class for execute the external program of svn
*
* @auth Seven Yang
*
*/
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;
}
}



본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

C 언어의 return 사용법에 대한 자세한 설명 C 언어의 return 사용법에 대한 자세한 설명 Oct 07, 2023 am 10:58 AM

C 언어의 return 사용법에 대한 자세한 설명

Java에서 return 및 finally 문의 실행 순서는 무엇입니까? Java에서 return 및 finally 문의 실행 순서는 무엇입니까? Apr 25, 2023 pm 07:55 PM

Java에서 return 및 finally 문의 실행 순서는 무엇입니까?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

Ansible Ad-Hoc(피어 투 피어 모드) Ansible Ad-Hoc(피어 투 피어 모드) Feb 18, 2024 pm 04:48 PM

Ansible Ad-Hoc(피어 투 피어 모드)

프런트엔드 출력 설정 프런트엔드 출력 설정 Feb 19, 2024 am 09:30 AM

프런트엔드 출력 설정

자바스크립트 함수 반환값과 반환문에 대한 자세한 설명 자바스크립트 함수 반환값과 반환문에 대한 자세한 설명 Aug 04, 2022 am 09:46 AM

자바스크립트 함수 반환값과 반환문에 대한 자세한 설명

Python에서 반환 값을 사용하는 방법 Python에서 반환 값을 사용하는 방법 Oct 07, 2023 am 11:10 AM

Python에서 반환 값을 사용하는 방법

Vue3는 어떻게 설정 구문 설탕을 사용하여 반환 쓰기를 거부합니까? Vue3는 어떻게 설정 구문 설탕을 사용하여 반환 쓰기를 거부합니까? May 12, 2023 pm 06:34 PM

Vue3는 어떻게 설정 구문 설탕을 사용하여 반환 쓰기를 거부합니까?

See all articles