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
搜索和替换文件或目录的一个好类--很实用
Oct 09 PHP
php select,radio和checkbox默认选择的实现方法
May 15 PHP
解析php通过cookies获取远程网页的指定代码
Jun 25 PHP
IIS安装Apache伪静态插件的具体操作图文
Jul 01 PHP
析构函数与php的垃圾回收机制详解
Oct 28 PHP
thinkphp视图模型查询提示ERR: 1146:Table 'db.pr_order_view' doesn't exist的解决方法
Oct 30 PHP
php获取网站百度快照日期的方法
Jul 29 PHP
详解Yaf框架PHPUnit集成测试方法
Dec 27 PHP
php生成二维码不保存服务器还有下载功能的实现代码
Aug 09 PHP
在laravel中使用with实现动态添加where条件
Oct 10 PHP
PHP array_reverse() 函数原理及实例解析
Jul 14 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_multi的应用
2013/07/17 PHP
php中动态修改ini配置
2014/10/14 PHP
php读取远程gzip压缩网页的方法
2014/12/29 PHP
php实现当前页面点击下载文件的简单方法
2016/09/22 PHP
PHP中危险的file_put_contents函数详解
2017/11/04 PHP
Laravel 5.5基于内置的Auth模块实现前后台登陆详解
2017/12/21 PHP
php实现微信原生支付(扫码支付)功能
2018/05/30 PHP
PHP中quotemeta()函数的用法讲解
2019/04/04 PHP
Nigma vs Alliance BO5 第五场2.14
2021/03/10 DOTA
调试Node.JS的辅助工具(NodeWatcher)
2012/01/04 Javascript
在子窗口中关闭父窗口的一句代码
2013/10/21 Javascript
MyEclipse取消验证Js的两种方法
2013/11/14 Javascript
jQuery绑定事件on()与弹窗的简要概述
2016/04/27 Javascript
AngularJS  自定义指令详解及实例代码
2016/09/14 Javascript
网站申请不到支付宝接口、微信接口,免接口收款实现方式几种解决办法
2016/12/14 Javascript
AngularJS中run方法的巧妙运用
2017/01/04 Javascript
jQuery插件autocomplete使用详解
2017/02/04 Javascript
利用vue.js插入dom节点的方法
2017/03/15 Javascript
JavaScript之json_动力节点Java学院整理
2017/06/29 Javascript
老生常谈javascript的面向对象思想
2017/08/22 Javascript
Angular实现类似博客评论的递归显示及获取回复评论的数据
2017/11/06 Javascript
Vue 实现显示/隐藏层的思路(加全局点击事件)
2019/12/31 Javascript
JavaScript enum枚举类型定义及使用方法
2020/05/15 Javascript
下载给定网页上图片的方法
2014/02/18 Python
Python中为什么要用self探讨
2015/04/14 Python
python3.5+tesseract+adb实现西瓜视频或头脑王者辅助答题
2018/01/17 Python
python安装教程
2018/02/28 Python
opencv python 傅里叶变换的使用
2018/07/21 Python
Django中提供的6种缓存方式详解
2019/08/05 Python
关于jupyter打开之后不能直接跳转到浏览器的解决方式
2020/04/13 Python
Crocs卡骆驰洞洞鞋日本官方网站:Crocs日本
2016/08/25 全球购物
企业员工培训感言
2014/02/26 职场文书
保送生自荐信范文
2015/03/26 职场文书
行政介绍信范文
2015/05/04 职场文书
对Golang中的FORM相关字段理解
2021/05/02 Golang
5种 JavaScript 方式实现数组扁平化
2021/10/05 Javascript