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 相关文章推荐
discuz authcode 经典php加密解密函数解析
Jul 12 PHP
php 删除目录下N分钟前创建的所有文件的实现代码
Aug 10 PHP
php获得url参数中具有&amp;的值的方法
Mar 05 PHP
一个显示效果非常不错的PHP错误、异常处理类
Mar 21 PHP
Yii框架获取当前controlle和action对应id的方法
Dec 03 PHP
PHP输出两个数字中间有多少个回文数的方法
Mar 23 PHP
PHP Streams(流)详细介绍及使用
May 12 PHP
codeigniter实现get分页的方法
Jul 10 PHP
Yii中的cookie的发送和读取
Jul 27 PHP
谈谈PHP连接Access数据库的注意事项
Aug 12 PHP
SAE实时日志接口SDK用法示例
Oct 09 PHP
详解Yii2 rules 的验证规则
Dec 02 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中socket的用法详解
2014/10/24 PHP
修改PHP脚本使WordPress拦截垃圾评论的方法示例
2015/12/10 PHP
实例讲解YII2中多表关联的使用方法
2017/07/21 PHP
Tinymce+jQuery.Validation使用产生的BUG
2010/03/29 Javascript
jQuery Animation实现CSS3动画示例介绍
2013/08/14 Javascript
js实时获取并显示当前时间的方法
2015/07/31 Javascript
javascript设置和获取cookie的方法实例详解
2016/01/05 Javascript
基于jQuery实现仿搜狐辩论投票动画代码(附源码下载)
2016/02/18 Javascript
JavaScript中的各种操作符使用总结
2016/05/26 Javascript
javascript如何创建对象
2016/08/29 Javascript
细数JavaScript 一个等号,两个等号,三个等号的区别
2016/10/09 Javascript
ES2015 Symbol 一种绝不重复的值
2016/12/25 Javascript
JavaScript中正则表达式判断匹配规则及常用方法
2017/08/03 Javascript
JS+canvas画一个圆锥实例代码
2017/12/13 Javascript
解决iView中时间控件选择的时间总是少一天的问题
2018/03/15 Javascript
[01:56]2014DOTA2西雅图邀请赛 MVP外卡赛老队长精辟点评
2014/07/09 DOTA
编写Python脚本抓取网络小说来制作自己的阅读器
2015/08/20 Python
利用python代码写的12306订票代码
2015/12/20 Python
Python变量赋值的秘密分享
2018/04/03 Python
python图形开发GUI库wxpython使用方法详解
2020/02/14 Python
Python HTTP下载文件并显示下载进度条功能的实现
2020/04/02 Python
jupyter notebook实现显示行号
2020/04/13 Python
Python pandas如何向excel添加数据
2020/05/22 Python
Python远程linux执行命令实现
2020/11/11 Python
博朗(Braun)俄罗斯官方商店:德国小家电品牌
2019/09/24 全球购物
大学毕业生的自我鉴定
2013/11/30 职场文书
公积金转移接收函
2014/01/11 职场文书
教师旷工检讨书
2014/01/18 职场文书
销售业务员岗位职责
2014/01/29 职场文书
文秘应聘自荐书范文
2014/02/18 职场文书
教师四风自我剖析材料
2014/09/30 职场文书
党支部群众路线整改措施思想汇报
2014/10/10 职场文书
2014年财务个人工作总结
2014/12/08 职场文书
初中班主任工作总结2015
2015/05/13 职场文书
PostgreSQL聚合函数介绍以及分组和排序
2022/04/12 PostgreSQL
vue 数字翻牌器动态加载数据
2022/04/20 Vue.js