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 图片加水印与上传图片加水印php类
May 12 PHP
php5 apache 2.2 webservice 创建与配置(java)
Jan 27 PHP
PHP mcrypt可逆加密算法分析
Jul 19 PHP
解析php中用PHPMailer来发送邮件的示例(126.com的例子)
Jun 24 PHP
使用openssl实现rsa非对称加密算法示例
Jan 24 PHP
yii框架表单模型使用及以数组形式提交表单数据示例
Apr 30 PHP
用 Composer构建自己的 PHP 框架之使用 ORM
Oct 30 PHP
PHP模块memcached使用指南
Dec 08 PHP
php生成唯一的订单函数分享
Feb 02 PHP
PHP使用token防止表单重复提交的方法
Apr 07 PHP
php类的自动加载操作实例详解
Sep 28 PHP
php+jQuery实现的三级导航栏下拉菜单显示效果
Aug 10 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
十天学会php(1)
2006/10/09 PHP
关于更改Zend Studio/Eclipse代码风格主题的介绍
2013/06/23 PHP
Yii配置文件用法详解
2014/12/04 PHP
thinkphp3.2实现上传图片的控制器方法
2016/04/28 PHP
详解php框架Yaf路由重写
2017/06/20 PHP
PHP中危险的file_put_contents函数详解
2017/11/04 PHP
PHP常见的序列化与反序列化操作实例分析
2019/10/28 PHP
记Laravel调用Gin接口调用formData上传文件的实现方法
2019/12/12 PHP
PHP接入支付宝接口失效流程详解
2020/11/10 PHP
JQuery的一些小应用收集
2010/03/27 Javascript
Document对象内容集合(比较全)
2010/09/06 Javascript
jQuery性能优化28条建议你值得借鉴
2013/02/16 Javascript
node.js中的定时器nextTick()和setImmediate()区别分析
2014/11/26 Javascript
jQueryMobile之Helloworld与页面切换的方法
2015/02/04 Javascript
jQuery实现点击任意位置弹出层外关闭弹出层效果
2016/10/19 Javascript
js随机生成一个验证码
2017/06/01 Javascript
vue keep-alive请求数据的方法示例
2018/05/16 Javascript
微信小程序实现文字从右向左无限滚动
2020/11/18 Javascript
使用pm2部署node生产环境的方法步骤
2019/03/09 Javascript
基于Proxy的小程序状态管理实现
2019/06/14 Javascript
layui实现左侧菜单点击右侧内容区显示
2019/07/26 Javascript
js实现多图和单图上传显示
2019/12/18 Javascript
React Hooks 实现和由来以及解决的问题详解
2020/01/17 Javascript
vue+Element中table表格实现可编辑(select下拉框)
2020/05/21 Javascript
python读取二进制mnist实例详解
2017/05/31 Python
Python实现嵌套列表去重方法示例
2017/12/28 Python
详解python OpenCV学习笔记之直方图均衡化
2018/02/08 Python
python enumerate内置函数用法总结
2020/01/07 Python
Pycharm安装Qt Design快捷工具的详细教程
2020/11/18 Python
英文自荐信
2013/12/19 职场文书
党员思想汇报范文
2013/12/30 职场文书
2014年加油站工作总结
2014/12/04 职场文书
2015年党员公开承诺书范文
2015/01/22 职场文书
平遥古城导游词
2015/02/03 职场文书
导游词之秦皇岛燕塞湖
2020/01/03 职场文书
JavaScript实例 ODO List分析
2022/01/22 Javascript