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 选项及相关信息函数库
Dec 04 PHP
PHP5 操作MySQL数据库基础代码
Sep 29 PHP
PHP数组交集的优化代码分析
Mar 06 PHP
PHP学习之字符串比较和查找
Apr 17 PHP
解析CodeIgniter自定义配置文件
Jun 18 PHP
浅析php数据类型转换
Jan 09 PHP
php中get_cfg_var()和ini_get()的用法及区别
Mar 04 PHP
PHP设置进度条的方法
Jul 08 PHP
php实现批量修改文件名称的方法
Jul 23 PHP
php 获取文件行数的方法总结
Oct 11 PHP
ThinkPHP开发--使用七牛云储存
Sep 14 PHP
Laravel框架Blade模板简介及模板继承用法分析
Dec 03 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下使用CURL方式POST数据至API接口的代码
2013/02/14 PHP
从PHP $_SERVER相关参数判断是否支持Rewrite模块
2013/09/26 PHP
判断、添加和删除WordPress置顶文章的相关PHP函数小结
2015/12/10 PHP
thinkphp3.2实现在线留言提交验证码功能
2017/07/19 PHP
Windows平台实现PHP连接SQL Server2008的方法
2017/07/26 PHP
PHP pthreads v3下worker和pool的使用方法示例
2020/02/21 PHP
一段利用WSH修改和查看IP配置的代码
2008/05/11 Javascript
jquery maxlength使用说明
2011/09/09 Javascript
用JavaScript计算在UTF-8下存储字符串占用字节数
2013/08/08 Javascript
JS关键字球状旋转效果的实例代码
2013/11/29 Javascript
JS面向对象(3)之Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法
2016/02/25 Javascript
浅谈Web页面向后台提交数据的方式和选择
2016/09/23 Javascript
Bootstrap框架实现广告轮播效果
2016/11/28 Javascript
JavaScript学习笔记--常用的互动方法
2016/12/07 Javascript
JS实现搜索框文字可删除功能
2016/12/28 Javascript
浅谈vue中数据双向绑定的实现原理
2017/09/14 Javascript
Koa2 之文件上传下载的示例代码
2018/03/29 Javascript
详解Axios 如何取消已发送的请求
2018/10/20 Javascript
Vue实现一个无限加载列表功能
2018/11/13 Javascript
js中对象和面向对象与Json介绍
2019/01/21 Javascript
优雅的使用javascript递归画一棵结构树示例代码
2019/09/22 Javascript
ES5新增数组的实现方法
2020/05/12 Javascript
[01:05:59]Mineski vs Secret 2019国际邀请赛淘汰赛 败者组 BO3 第二场 8.22
2019/09/05 DOTA
对python中Json与object转化的方法详解
2018/12/31 Python
详解python校验SQL脚本命名规则
2019/03/22 Python
Python正则表达式实现简易计算器功能示例
2019/05/07 Python
PyQt5图形界面播放音乐的实例
2019/06/17 Python
Python自定义一个异常类的方法
2019/06/27 Python
pygame用blit()实现动画效果的示例代码
2020/05/28 Python
Kmeans均值聚类算法原理以及Python如何实现
2020/09/26 Python
CSS3教程:边框属性border的极致应用
2009/04/02 HTML / CSS
Hunkemöller西班牙:欧洲最大的内衣连锁店
2018/08/15 全球购物
函授毕业生自我鉴定
2013/11/06 职场文书
应届优秀本科大学毕业生自我鉴定
2014/01/21 职场文书
咖啡馆创业计划书
2014/01/26 职场文书
人大代表选举标语
2014/10/07 职场文书