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制作静态网站的模板框架(二)
Oct 09 PHP
一个很不错的PHP翻页类
Jun 01 PHP
通过具体程序来理解PHP里面的抽象类
Jan 28 PHP
PHP在引号前面添加反斜杠(PHP去除反斜杠)
Sep 28 PHP
php对二维数组按指定键值key排序示例代码
Nov 26 PHP
PHP中cookie和session的区别实例分析
Aug 28 PHP
ucenter通信原理分析
Jan 09 PHP
php session的锁和并发
Jan 22 PHP
PHP自定义多进制的方法
Nov 03 PHP
Redis使用Eval多个键值自增的操作实例
Nov 04 PHP
php-fpm.conf配置文件中文说明详解及重要参数说明
Oct 10 PHP
关于laravel 数据库迁移中integer类型是无法指定长度的问题
Oct 09 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 5.3新特性命名空间规则解析及高级功能
2010/03/11 PHP
让PHP支持断点续传的源码
2010/05/16 PHP
php使用Smarty的相关注意事项及访问变量的几种方式
2011/12/08 PHP
php中time()和mktime()方法的区别
2013/09/28 PHP
javascript 学习笔记(onchange等)
2010/11/14 Javascript
使用非html5实现js板连连看游戏示例代码
2013/09/22 Javascript
javascript客户端遍历控件与获取父容器对象示例代码
2014/01/06 Javascript
JavaScript获取图片的原始尺寸以宽度为例
2014/05/04 Javascript
javascript实现切换td中的值
2014/12/05 Javascript
优化RequireJS项目的相关技巧总结
2015/07/01 Javascript
JavaScript实现LI列表数据绑定的方法
2015/08/04 Javascript
浅谈jQuery为哪般去掉了浏览器检测
2016/08/29 Javascript
利用bootstrapValidator验证UEditor
2016/09/14 Javascript
完美解决js传递参数中加号和&amp;号自动改变的方法
2016/10/11 Javascript
详解angular中如何监控dom渲染完毕
2017/01/03 Javascript
Node.js与Sails redis组件的使用教程
2017/02/14 Javascript
JS实现动态给标签控件添加事件的方法示例
2017/05/13 Javascript
JavaScript判断变量名是否存在数组中的实例
2017/12/28 Javascript
python发腾讯微博代码分享
2014/01/10 Python
详解Python中的type()方法的使用
2015/05/21 Python
深入解答关于Python的11道基本面试题
2017/04/01 Python
Vue的el-scrollbar实现自定义滚动
2018/05/29 Python
基于Python pip用国内镜像下载的方法
2018/06/12 Python
selenium使用chrome浏览器测试(附chromedriver与chrome的对应关系表)
2018/11/29 Python
python中的句柄操作的方法示例
2019/06/20 Python
python读出当前时间精度到秒的代码
2019/07/05 Python
在PyCharm中安装PaddlePaddle的方法
2021/02/05 Python
英国领先的杂志订阅网站:Magazine.co.uk
2018/01/25 全球购物
Shell编程面试题
2012/05/30 面试题
党的群众路线教育实践活动批评与自我批评范文
2014/10/16 职场文书
教师党员个人自我评价
2015/03/04 职场文书
小学生暑假生活总结
2015/07/13 职场文书
趣味运动会广播稿
2015/08/19 职场文书
2016大学生就业指导课心得体会
2016/01/15 职场文书
Vue.js 带下拉选项的输入框(Textbox with Dropdown)组件
2021/04/17 Vue.js
Mysql如何查看是否使用到索引
2022/12/24 MySQL