使用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 相关文章推荐
PHP 5.0对象模型深度探索之属性和方法
Mar 27 PHP
php面向对象全攻略 (二) 实例化对象 使用对象成员
Sep 30 PHP
30 个很棒的PHP开源CMS内容管理系统小结
Oct 14 PHP
解析coreseek for sphinx的使用
Jun 21 PHP
PHP与javascript实现变量交互的示例代码
Jul 23 PHP
浅析php与数据库代码开发规范
Aug 08 PHP
curl不使用文件存取cookie php使用curl获取cookie示例
Jan 26 PHP
php计算两个日期时间差(返回年、月、日)
Jun 19 PHP
smarty简单入门实例
Nov 28 PHP
php中preg_match的isU代表什么意思
Oct 01 PHP
php+jquery+html实现点击不刷新加载更多的实例代码
Aug 12 PHP
PHP7原生MySQL数据库操作实现代码
Jul 03 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
让你的网站首页自动选择语言转跳
2006/12/06 PHP
smarty模板引擎使用内建函数foreach循环取出所有数组值的方法
2015/01/22 PHP
php使用Imagick生成图片的方法
2015/07/31 PHP
实例讲解PHP设计模式编程中的简单工厂模式
2016/02/29 PHP
php模板引擎技术简单实现
2016/03/15 PHP
PHP-CGI远程代码执行漏洞分析与防范
2017/05/07 PHP
PC端微信扫码支付成功之后自动跳转php版代码
2017/07/07 PHP
如何在PHP中生成随机数
2020/06/04 PHP
实现复选框全选/全不选切换
2006/12/23 Javascript
Jquery从头学起第四讲 jquery入门教程
2010/08/01 Javascript
js中document.getElementByid、document.all和document.layers区分介绍
2011/12/08 Javascript
js控制table合并具体实现
2014/02/20 Javascript
兼容Firefox的Javascript XSLT 处理XML文件
2014/12/31 Javascript
JavaScript自定义等待wait函数实例分析
2015/03/23 Javascript
js实现人民币大写金额形式转换
2016/04/27 Javascript
Bootstrap源码解读标签、徽章、缩略图和警示框(8)
2016/12/26 Javascript
jQuery简单实现MD5加密的方法
2017/03/03 Javascript
jQuery设置图片等比例缩小的方法
2017/04/29 jQuery
JavaScript设置名字输入不合法的实现方法
2017/05/23 Javascript
vue-cli3环境变量与分环境打包的方法示例
2019/02/18 Javascript
vue实现分页栏效果
2019/06/28 Javascript
Angular如何由模板生成DOM树的方法
2019/12/23 Javascript
一文秒懂JavaScript构造函数、实例、原型对象以及原型链
2020/08/25 Javascript
python 文件与目录操作
2008/12/24 Python
Python进程间通信用法实例
2015/06/04 Python
Python之父谈Python的未来形式
2016/07/01 Python
对numpy 数组和矩阵的乘法的进一步理解
2018/04/04 Python
对Python中list的倒序索引和切片实例讲解
2018/11/15 Python
使用Python给头像戴上圣诞帽的图像操作过程解析
2019/09/20 Python
如何使用python socket模块实现简单的文件下载
2020/09/04 Python
基于HTML5 Canvas 实现弹出框效果
2017/06/05 HTML / CSS
Vuori官网:运动服装的终级表现
2021/01/27 全球购物
行政部总经理岗位职责
2014/01/04 职场文书
跑出一片天观后感
2015/06/08 职场文书
numpy数据类型dtype转换实现
2021/04/24 Python
CentOS7 minimal 最小化安装网络设置过程
2022/12/24 Servers