使用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 相关文章推荐
AJAX for PHP简单表数据查询实例
Jan 02 PHP
PHP提取字符串中的图片地址[正则表达式]
Nov 12 PHP
浅谈web上存漏洞及原理分析、防范方法(文件名检测漏洞)
Jun 29 PHP
php5.5中类级别的常量使用介绍
Oct 02 PHP
PHP遍历目录函数opendir()、readdir()、closedir()、rewinddir()总结
Nov 18 PHP
php实现mysql备份恢复分卷处理的方法
Dec 26 PHP
php实现随机显示图片方法汇总
May 21 PHP
Yii实现Command任务处理的方法详解
Jul 14 PHP
详解PHP处理密码的几种方式
Nov 30 PHP
php实现异步将远程链接上内容(图片或内容)写到本地的方法
Nov 30 PHP
PHPStorm2020.1永久激活及下载更新至2020(推荐)
Sep 25 PHP
如何用PHP websocket实现网页实时聊天
May 26 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 中的输出缓冲
2006/12/21 PHP
删除html标签得到纯文本可处理嵌套的标签
2014/04/28 PHP
PHP编程中的Session阻塞问题与解决方法分析
2017/08/07 PHP
Laravel5.5新特性之友好报错以及展示详解
2017/08/13 PHP
使用CSS3实现字体颜色渐变的实现
2021/03/09 HTML / CSS
HTML5如何适配 iPhone IOS 底部黑条
2021/03/09 HTML / CSS
js中opener与parent的区别详细解析
2014/01/14 Javascript
深入浅析JavaScript中的constructor
2016/04/19 Javascript
JS中使用变量保存arguments对象的方法
2016/06/03 Javascript
JS图片定时翻滚效果实现方法
2016/06/21 Javascript
JavaScript数组操作函数汇总
2016/08/05 Javascript
bootstrap datepicker插件默认英文修改为中文
2017/07/28 Javascript
在Vue组件中使用 TypeScript的方法
2018/02/28 Javascript
VUE DOM加载后执行自定义事件的方法
2018/09/07 Javascript
laravel-admin 与 vue 结合使用实例代码详解
2019/06/04 Javascript
微信小程序实现订单倒计时
2020/11/01 Javascript
初试vue-cli使用HBuilderx打包app的坑
2019/07/17 Javascript
p5.js临摹动态图形的方法
2019/10/23 Javascript
JS数组进阶示例【数组的几种函数用法】
2020/01/16 Javascript
python常用web框架简单性能测试结果分享(包含django、flask、bottle、tornado)
2014/08/25 Python
Python实现对比不同字体中的同一字符的显示效果
2015/04/23 Python
Python管理Windows服务小脚本
2018/03/12 Python
Django中日期处理注意事项与自定义时间格式转换详解
2018/08/06 Python
对Python中一维向量和一维向量转置相乘的方法详解
2019/08/26 Python
基于python的BP神经网络及异或实现过程解析
2019/09/30 Python
python用TensorFlow做图像识别的实现
2020/04/21 Python
Python txt文件常用读写操作代码实例
2020/08/03 Python
Python用access判断文件是否被占用的实例方法
2020/12/17 Python
基于CSS3制作立体效果导航菜单
2016/01/12 HTML / CSS
HTML中fieldset标签概述及使用方法
2013/02/01 HTML / CSS
美国在线奢侈品寄售商店:Luxury Garage Sale
2018/08/19 全球购物
保护环境的建议书
2014/03/12 职场文书
英语课外活动总结
2014/08/27 职场文书
感恩主题班会教案
2015/08/12 职场文书
《实心球》教学反思
2016/02/23 职场文书
Python制作一个随机抽奖小工具的实现
2021/07/07 Python