使用PHP编写的SVN类


Posted in PHP onJuly 18, 2013
<?php
/**
 * SVN 外部命令 类
 *
 * @author rubekid
 *
 * @todo comment need addslashes for svn commit
 *
 */
class SvnUtils {
    /**
     *
     * svn 账号
     */
    const SVN_USERNAME = "robot";
    /**
     * svn 密码
     */
    const SVN_PASSWORD = "robot2013";
    /**
     * 配置文件目录   (任意指定一个临时目录,解决svn: warning: Can't open file '/root/.subversion/servers': Permission denied)
     */
    const SVN_CONFIG_DIR = "/var/tmp/";
    /**
     * svn list
     *
     * @param $repository string
     * @return boolean
     *
     */
    public static function ls($repository) {
        $command = "sudo svn ls " . $repository;
        $output = self::runCmd ( $command );
        $output = implode ( "<br />", $output );
        if (strpos ( $output, 'non-existent in that revision' )) {
            return false;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * svn copy
     *
     * @param $src string
     * @param $dst string
     * @param $comment string
     * @return boolean
     *
     */
    public static function copy($src, $dst, $comment) {
        $command = "sudo svn cp $src $dst -m '$comment'";
        $output = self::runCmd ( $command );
        $output = implode ( "<br />", $output );
        if (strpos ( $output, 'Committed revision' )) {
            return true;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * svn delete
     *
     * @param $url string
     * @param $comment string
     * @return boolean
     *
     */
    public static function delete($url, $comment) {
        $command = "sudo svn del $url -m '$comment'";
        $output = self::runCmd ( $command );
        $output = implode ( '<br />', $output );
        if (strpos ( $output, 'Committed revision' )) {
            return true;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * svn move
     *
     * @param $src string
     * @param $dst string
     * @param $comment string
     * @return boolean
     */
    public static function move($src, $dst, $comment) {
        $command = "sudo svn mv $src $dst -m '$comment'";
        $output = self::runCmd ( $command );
        $output = implode ( '<br />', $output );
        if (strpos ( $output, 'Committed revision' )) {
            return true;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * svn mkdir
     *
     * @param $url string
     * @param $comment string
     * @return boolean
     */
    public static function mkdir($url, $comment) {
        $command = "sudo svn mkdir $url -m '$comment'";
        $output = self::runCmd ( $command );
        $output = implode ( '<br />', $output );
        if (strpos ( $output, 'Committed revision' )) {
            return true;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * svn diff
     * @param $pathA string
     * @param $pathB string
     * @return string
     */
    public static function diff($pathA, $pathB) {
        $output = self::runCmd ( "sudo svn diff $pathA $pathB" );
        return implode ( '<br />', $output );
    }
    /**
     * svn checkout
     * @param $url string
     * @param $dir string
     * @return boolean
     */
    public static function checkout($url, $dir) {
        $command = "cd $dir && sudo svn co $url";
        $output = self::runCmd ( $command );
        $output = implode ( '<br />', $output );
        if (strstr ( $output, 'Checked out revision' )) {
            return true;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * svn update
     * @param $path string
     */
    public static function update($path) {
        $command = "cd $path && sudo svn up";
        $output = self::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];
    }
    /**
     * svn merge
     *
     * @param $revision string
     * @param $url string
     * @param $dir string
     *
     * @return boolean
     */
    public static function merge($revision, $url, $dir) {
        $command = "cd $dir && sudo svn merge -r1:$revision $url";
        $output = implode ( '<br />', self::runCmd ( $command ) );
        if (strstr ( $output, 'Text conflicts' )) {
            return 'Command: ' . $command . '<br />' . $output;
        }
        return true;
    }
    /**
     * svn commit
     *
     * @param $dir string
     * @param $comment string
     *
     * @return boolean
     */
    public static function commit($dir, $comment) {
        $command = "cd $dir && sudo svn commit -m'$comment'";
        $output = implode ( '<br />', self::runCmd ( $command ) );
        if (strpos ( $output, 'Committed revision' ) || empty ( $output )) {
            return true;
        }
        return $output;
    }
    /**
     * svn status (输出WC中文件和目录的状态)
     *
     * @param $dir string
     */
    public static function getStatus($dir) {
        $command = "cd $dir && sudo svn st";
        return self::runCmd ( $command );
    }
    /**
     * svn 冲突
     *
     * @param $dir string
     * @return boolean
     */
    public static function hasConflict($dir) {
        $output = self::getStatus ( $dir );
        foreach ( $output as $line ) {
            if ( substr ( trim ( $line ), 0, 1 ) == 'C' || (substr ( trim ( $line ), 0, 1 ) == '!')) {
                return true;
            }
        }
        return false;
    }
    /**
     * svn log
     *
     * @param $path string
     * @return string
     *
     */
    public static function getLog($path) {
        $command = "sudo svn log $path --xml";
        $output = self::runCmd ( $command );
        return implode ( '', $output );
    }
    /**
     * svn info
     * @param $path string
     */
    public static function getPathRevision($path) {
        $command = "sudo svn info $path --xml";
        $output = self::runCmd ( $command );
        $string = implode ( '', $output );
        $xml = new SimpleXMLElement ( $string );
        foreach ( $xml->entry [0]->attributes () as $key => $value ) {
            if ( $key == 'revision' ) {
                return $value;
            }
        }
    }
    /**
     * 获取最新版本号
     * @param $path string
     */
    public static function getHeadRevision($path) {
        $command = "cd $path && sudo svn up";
        $output = self::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];
    }
    /**
     * 获取某文件最早版本号
     *
     * @param $filePath string
     *
     */
    public static function getFileFirstVersion($filePath){
        $command = "sudo svn log {$filePath}";
        $output = self::runCmd ( $command , "|grep -i ^r[0-9]* |awk  '{print $1}'");
        if(empty($output)){
            return false;
        }
        return str_replace("r", '', $output[count($output)-1]);
    }
    /**
     * 获取两个版本间修改的文件信息列表
     *
     * @param $fromVersion int
     * @param $headRevision int
     * @param $$path string
     *
     * @return array
     */
    public static function getChangedFiles($path, $fromVersion, $headRevision ){
        $files = array();
        $pipe = "|grep -i ^Index:|awk -F : '{print $2}'";
        $command = "svn diff -r {$fromVersion}:{$headRevision} $path";
        $output = self::runCmd ( $command ,$pipe);
        $files = array_merge($files, $output);
        $command = "svn diff -r {$headRevision}:{$fromVersion} $path"; //文件删除可用逆向对比
        $output = self::runCmd ( $command ,$pipe);
        $files = array_merge($files, $output);
        return array_unique($files);
    }
    /**
     * 获取两个版本间某文件修改 的内容
     *
     * @param $filePath string
     * @param $fromVersion int
     * @param $headRevision int
     *
     * @return array
     */
    public static function getChangedInfo( $filePath, $fromVersion, $headRevision ){
        $command = "sudo svn diff -r {$fromVersion}:{$headRevision} $filePath";
        $output = self::runCmd ( $command );
        return $output;
    }
    /**
     * 查看文件内容
     *
     * @param $filePath string
     * @param $version int
     *
     * @return array
     */
    public static function getFileContent($filePath, $version){
        $command = "sudo svn cat -r {$version} $filePath";
        $output = self::runCmd ( $command );
        return $output;
    }
    /**
     * Run a cmd and return result
     * @param $command string
     * @param $pipe string (可以增加管道对返回数据进行预筛选)
     * @return array
     */
    protected static function runCmd($command , $pipe ="") {
        $authCommand = ' --username ' . self::SVN_USERNAME . ' --password ' . self::SVN_PASSWORD . ' --no-auth-cache --non-interactive --config-dir ' . self::SVN_CONFIG_DIR . '.subversion';
        exec ( $command . $authCommand . " 2>&1" . $pipe, $output );
        return $output;
    }
}
PHP 相关文章推荐
PHP5.0对象模型探索之抽象方法和抽象类
Sep 05 PHP
Phpbean路由转发的php代码
Jan 10 PHP
php 动态执行带有参数的类方法
Apr 10 PHP
PHP 压缩文件夹的类代码
Nov 05 PHP
PHP实现使用优酷土豆视频地址获取swf播放器分享地址
Jun 05 PHP
调试PHP程序的多种方法介绍
Nov 06 PHP
php启用sphinx全文搜索的实现方法
Dec 24 PHP
php专用数组排序类ArraySortUtil用法实例
Apr 03 PHP
php实现给一张图片加上水印效果
Jan 02 PHP
ThinkPHP的常用配置选项汇总
Mar 24 PHP
PHP中file_exists使用中遇到的问题小结
Apr 05 PHP
详谈php ip2long 出现负数的原因及解决方法
Apr 05 PHP
请离开include_once和require_once
Jul 18 #PHP
解析PHP中的unset究竟会不会释放内存
Jul 18 #PHP
解析php中curl_multi的应用
Jul 17 #PHP
php curl获取网页内容(IPV6下超时)的解决办法
Jul 16 #PHP
ie与session丢失(新窗口cookie丢失)实测及解决方案
Jul 15 #PHP
实测在class的function中include的文件中非php的global全局环境
Jul 15 #PHP
Php output buffering缓存及程序缓存深入解析
Jul 15 #PHP
You might like
php中的三元运算符使用说明
2011/07/03 PHP
php中常用的预定义变量小结
2012/05/09 PHP
PHP实现阿里大鱼短信验证的实例代码
2017/07/10 PHP
PHP自定义函数实现数组比较功能示例
2017/10/19 PHP
JavaScript与Image加载事件(onload)、加载状态(complete)
2011/02/14 Javascript
jQuery Ajax方法调用 Asp.Net WebService 的详细实例代码
2011/04/27 Javascript
ASP.NET jQuery 实例7 通过jQuery来获取DropDownList的Text/Value属性值
2012/02/03 Javascript
js 得到文件后缀(通过正则实现)
2013/07/08 Javascript
Jquery的Tabs内容轮换效果实现代码,几行搞定
2014/02/12 Javascript
jQuery select表单提交省市区城市三级联动核心代码
2014/06/09 Javascript
JS 排序输出实现table行号自增前端动态生成的tr
2014/08/13 Javascript
javascript模拟实现ajax加载框实例
2014/10/15 Javascript
JQUERY简单按钮轮换选中效果实现方法
2015/05/07 Javascript
JavaScript学习小结之使用canvas画“哆啦A梦”时钟
2016/07/24 Javascript
如何提高数据访问速度
2016/12/26 Javascript
详解angular中如何监控dom渲染完毕
2017/01/03 Javascript
socket.io学习教程之基本应用(二)
2017/04/29 Javascript
微信小程序 生命周期函数详解
2017/05/24 Javascript
深入理解vue2.0路由如何配置问题
2017/07/18 Javascript
jQuery中 DOM节点操作方法大全
2017/10/12 jQuery
VUE项目初建和常见问题总结
2019/09/12 Javascript
使用Python多线程爬虫爬取电影天堂资源
2016/09/23 Python
python如何实现视频转代码视频
2019/06/17 Python
通过Python实现一个简单的html页面
2020/05/16 Python
python爬虫数据保存到mongoDB的实例方法
2020/07/28 Python
Python3爬虫关于识别点触点选验证码的实例讲解
2020/07/30 Python
python中scrapy处理项目数据的实例分析
2020/11/22 Python
解决python的空格和tab混淆而报错的问题
2021/02/26 Python
CSS3正方体旋转示例代码
2013/08/08 HTML / CSS
美国家用和厨房电器销售网站:Appliances Connection
2020/01/24 全球购物
北京天润融通.net面试题笔试题
2012/02/20 面试题
个人简历自荐信
2014/06/26 职场文书
2014年祖国生日寄语
2014/09/19 职场文书
2015年房地产个人工作总结
2015/05/26 职场文书
导游词之澳门妈祖庙
2019/12/19 职场文书
PostgreSQL并行计算算法及参数强制并行度设置方法
2022/04/06 PostgreSQL