php操作SVN版本服务器类代码


Posted in PHP onNovember 27, 2011

SvnPeer.php

<?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 ('revision' == $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; 
} 
}
PHP 相关文章推荐
php学习笔记(三)操作符与控制结构
Aug 06 PHP
php5.2以下版本无json_decode函数的解决方法
May 25 PHP
php中$美元符号与Zen Coding冲突问题解决方法分享
May 28 PHP
PHP实现图片不变型裁剪及图片按比例裁剪的方法
Jan 14 PHP
PHP实现的文件上传类与用法详解
Jul 05 PHP
php提取微信账单的有效信息
Oct 01 PHP
实例解析php的数据类型
Oct 24 PHP
PHP Trait代码复用类与多继承实现方法详解
Jun 17 PHP
thinkphp5.1框架模板布局与模板继承用法分析
Jul 19 PHP
PHP架构及原理知识点详解
Dec 22 PHP
php文件上传原理与实现方法详解
Dec 20 PHP
laravel框架select2多选插件初始化默认选中项操作示例
Feb 18 PHP
支持中文的php加密解密类代码
Nov 27 #PHP
php UBB 解析实现代码
Nov 27 #PHP
PHP高自定义性安全验证码代码
Nov 27 #PHP
php中XMLHttpRequest(Ajax)不能设置自定义的Referer的解决方法
Nov 26 #PHP
Linux fgetcsv取得的数组元素为空字符串的解决方法
Nov 25 #PHP
php 团购折扣计算公式
Nov 24 #PHP
php中$_REQUEST、$_POST、$_GET的区别和联系小结
Nov 23 #PHP
You might like
第十五节--Zend引擎的发展
2006/11/16 PHP
php将gd生成的图片缓存到memcache的小例子
2013/06/05 PHP
php导出word文档与excel电子表格的简单示例代码
2014/03/08 PHP
PHP独立Session数据库存储操作类分享
2014/06/11 PHP
php中动态修改ini配置
2014/10/14 PHP
PHP实现数组array转换成xml的方法
2016/07/19 PHP
php 实现一个字符串加密解密的函数实例代码
2016/11/01 PHP
php执行多个存储过程的方法【基于thinkPHP】
2016/11/08 PHP
PHP html_entity_decode()函数讲解
2019/02/25 PHP
js实现同一个页面多个渐变效果的方法
2015/04/10 Javascript
javascript验证邮件地址和MX记录的方法
2015/06/16 Javascript
jQuery多文件异步上传带进度条实例代码
2016/08/16 Javascript
require.js 加载 vue组件 r.js 合并压缩的实例
2016/10/14 Javascript
js实现简单的计算器功能
2017/01/16 Javascript
axios学习教程全攻略
2017/03/26 Javascript
Vue自定义事件(详解)
2017/08/19 Javascript
Angularjs实现上传图片预览功能
2017/09/01 Javascript
JS实现显示当前日期的实例代码
2018/07/03 Javascript
解决vue单页路由跳转后scrollTop的问题
2018/09/03 Javascript
js实现随机点名程序
2020/09/17 Javascript
JavaScript canvas绘制渐变颜色的矩形
2020/02/18 Javascript
Vue组件化开发之通用型弹出框的实现
2020/02/28 Javascript
Node在Controller层进行数据校验的过程详解
2020/08/28 Javascript
python之pandas用法大全
2018/03/13 Python
PyQt 图解Qt Designer工具的使用方法
2019/08/06 Python
html5触摸事件判断滑动方向的实现
2018/06/05 HTML / CSS
canvas需要在标签里直接定义宽高
2014/12/17 HTML / CSS
生物医学工程专业学生求职信范文分享
2013/12/14 职场文书
寒假家长评语大全
2014/04/16 职场文书
学生评语集锦
2015/01/04 职场文书
中英文求职信范文
2015/03/19 职场文书
2015年库房管理工作总结
2015/10/14 职场文书
MySQL中日期型单行函数代码详解
2021/06/21 MySQL
vue如何实现关闭对话框后刷新列表
2022/04/08 Vue.js
Python实现批量将文件复制到新的目录中再修改名称
2022/04/12 Python
Oracle用户管理及赋权
2022/04/24 Oracle