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 03 PHP
php无序树实现方法
Jul 28 PHP
如何实现php图片等比例缩放
Jul 28 PHP
Thinkphp无限级分类代码
Nov 11 PHP
学习php设计模式 php实现桥梁模式(bridge)
Dec 07 PHP
Zend Framework动作助手Json用法实例分析
Mar 05 PHP
php进程间通讯实例分析
Jul 11 PHP
Yii框架实现邮箱激活的方法【数字签名】
Oct 18 PHP
php注册和登录界面的实现案例(推荐)
Oct 24 PHP
PHP自定义多进制的方法
Nov 03 PHP
Laravel如何创建服务器提供者实例代码
Apr 15 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
造势之举?韩国总统候选人发布《星际争霸》地图
2017/04/22 星际争霸
注意!PHP 7中不要做的10件事
2016/09/18 PHP
PHP基于GD库的图像处理方法小结
2016/09/27 PHP
解析 thinkphp 框架中的部分方法
2017/05/07 PHP
javascript运行机制之this详细介绍
2014/02/07 Javascript
基于 Docker 开发 NodeJS 应用
2014/07/30 NodeJs
JS对象与json字符串格式转换实例
2014/10/28 Javascript
jQuery实现form表单元素序列化为json对象的方法
2015/12/09 Javascript
js实现select二级联动下拉菜单
2020/04/17 Javascript
在html中引入外部js文件,并调用带参函数的方法
2016/10/31 Javascript
JavaScript面试题(指针、帽子和女朋友)
2016/11/23 Javascript
Angular.JS利用ng-disabled属性和ng-model实现禁用button效果
2017/04/05 Javascript
.net MVC+Bootstrap下使用localResizeIMG上传图片
2017/04/21 Javascript
vue异步加载高德地图的实现
2018/06/19 Javascript
微信小程序导航栏跟随滑动效果的实现代码
2019/05/14 Javascript
layui异步加载table表中某一列数据的例子
2019/09/16 Javascript
谈谈我在vue-cli3中用预渲染遇到的坑
2020/04/22 Javascript
Jquery ajax书写方法代码实例解析
2020/06/12 jQuery
vue 中的动态传参和query传参操作
2020/11/09 Javascript
pyside写ui界面入门示例
2014/01/22 Python
Python多进程机制实例详解
2015/07/02 Python
用python处理图片实现图像中的像素访问
2018/05/04 Python
python获取代码运行时间的实例代码
2018/06/11 Python
在linux下实现 python 监控usb设备信号
2019/07/03 Python
python同时替换多个字符串方法示例
2019/09/17 Python
Python中logging日志库实例详解
2020/02/19 Python
python要安装在哪个盘
2020/06/15 Python
Python logging模块异步线程写日志实现过程解析
2020/06/30 Python
怎样创建、运行java程序
2014/08/01 面试题
我看到了用指针调用函数的不同语法形式
2014/07/16 面试题
财务主管自我鉴定
2014/01/17 职场文书
花店创业计划书范文
2014/02/07 职场文书
员工安全承诺书
2014/05/22 职场文书
家装电话营销开场白
2015/05/29 职场文书
i5-10400f处理相当于i7多少水平
2022/04/19 数码科技
SQL中的连接查询详解
2022/06/21 SQL Server