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 相关文章推荐
教你如何把一篇文章按要求分段
Oct 09 PHP
PHP mkdir()定义和用法
Jan 14 PHP
php session 预定义数组
Mar 16 PHP
ThinkPHP实现带验证码的文件上传功能实例
Nov 01 PHP
PHP 实现的将图片转换为TXT
Oct 21 PHP
smarty中改进truncate使其支持中文的方法
May 30 PHP
laravel 中如何使用ajax和vue总结
Aug 16 PHP
PHP分享图片的生成方法
Apr 25 PHP
php获得刚插入数据的id 的几种方法总结
May 31 PHP
PHP操作路由器实现方法示例
Apr 27 PHP
解决Laravel 不能创建 migration 的问题
Oct 09 PHP
php高性能日志系统 seaslog 的安装与使用方法分析
Feb 29 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 output buffering缓存及程序缓存深入解析
2013/07/15 PHP
php url路由入门实例
2014/04/23 PHP
PHP SPL标准库之文件操作(SplFileInfo和SplFileObject)实例
2015/05/11 PHP
php生成唯一数字id的方法汇总
2015/11/18 PHP
phpstorm 配置xdebug的示例代码
2019/03/31 PHP
用Javascript 和 CSS 实现脚注(Footnote)效果
2009/09/09 Javascript
JavaScript函数详解
2014/11/17 Javascript
jQuery模拟新浪微博首页滚动效果的方法
2015/03/11 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
javascript实现方法调用与方法触发小结
2016/03/26 Javascript
在Node.js中使用Javascript Generators详解
2016/05/05 Javascript
正则中的回溯定义与用法分析【JS与java实现】
2016/12/27 Javascript
xmlplus组件设计系列之选项卡(Tabbar)(5)
2017/05/03 Javascript
解决vue router组件状态刷新消失的问题
2018/08/01 Javascript
微信小程序自定义波浪组件使用方法详解
2019/09/21 Javascript
如何在Express4.x中愉快地使用async的方法
2020/11/18 Javascript
python使用线程封装的一个简单定时器类实例
2015/05/16 Python
python 时间戳与格式化时间的转化实现代码
2016/03/23 Python
Python可变参数*args和**kwargs用法实例小结
2018/04/27 Python
详解关于Django中ORM数据库迁移的配置
2018/10/08 Python
解决django前后端分离csrf验证的问题
2019/02/03 Python
Python求一批字符串的最长公共前缀算法示例
2019/03/02 Python
Pandas库之DataFrame使用的学习笔记
2019/06/21 Python
python利用wx实现界面按钮和按钮监听和字体改变的方法
2019/07/17 Python
python如何统计代码运行的时长
2019/07/24 Python
python2 中 unicode 和 str 之间的转换及与python3 str 的区别
2019/07/25 Python
python实现最大优先队列
2019/08/29 Python
wxPython之wx.DC绘制形状
2019/11/19 Python
Python编程快速上手——PDF文件操作案例分析
2020/02/28 Python
销售行政专员职责
2014/01/03 职场文书
小学三年级学生评语
2014/04/22 职场文书
教师业务培训方案
2014/05/01 职场文书
工作总结与自我评价
2014/09/18 职场文书
2014年招商引资工作总结
2014/11/22 职场文书
2016暑期师德培训心得体会
2016/01/09 职场文书
分享7个 Python 实战项目练习
2022/03/03 Python