svn版本之间修改文件目录获取并导入到本地

该博客介绍了如何通过PHP的SvnPeer类来获取 SVN 版本之间的文件变更,用户输入版本号范围后,程序会导出指定版本间的文件和目录。文章提供了一个用于导出和展示文件变更的HTML表单,以及相关PHP代码实现,包括文件导出和获取文件变更列表的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <form method="post" action="">
        <input type="text" name="version_from" placeholder="版本起始号">
        <input type="text" name="version_to" placeholder="版本结束号">
        <input type="submit" value="提交">
    </form>
</html>
<?php
/**
 * @desc svn版本之间文件目录获取
 * @author Ezreal.yin
 * @date 2016-04-26
 * @说明:确保svn安装的时候选择了command line client tools(默认不选择),把svn加入到环境变量
 */
if ($_POST)
{
    $version_from = isset($_POST['version_from']) ? $_POST['version_from'] : 0;
    $version_to = isset($_POST['version_to']) ? $_POST['version_to'] : 0;
    
    $url = 'svn://192.168.1.102/test/trunk';
    //获取版本之间的文件变动
    $arr = SvnPeer::getChangedFiles($url, $version_from-1, $version_to);//包含某个版本
    $dir = array();
    if (!empty($arr))
    {
        foreach ($arr as $value)
        {
            if (strstr($value, 'svn:'))
            {
                //字符串处理获取变动文件路径
                $dir[] = substr(strstr($value, 'svn:'), 37);//根据自己网站目录填写从什么位置分割
            }
        }
    }
    if (!empty($dir))
    {
        //生成变动文件夹
        $path = date("Y-m-d") . '---' . $version_from. '-' . $version_to;
        if (!is_dir($path))
        {
            mkdir($path, 0777);
        }
        //生成文件夹
        foreach ($dir as $key => $value)
        {
            $dir_arr = explode('/', $value);
            if (!empty($dir_arr))
            {
                $tmp_path = '';
                foreach ($dir_arr as $k => $v) {
                    //如果包含.php, .js, .css, .txt, .sql, .html等就是文件
                    if (strstr($v, '.php') || strstr($v, '.js') || strstr($v, '.css') || strstr($v, '.txt') || strstr($v, '.sql') || strstr($v, '.html'))
                    {
                        $file = SvnPeer::export($url.$tmp_path.'/'.$v, $path.$tmp_path);
                    }
                    else
                    {
                        $tmp_path .= '/' . $v;
                        //文件夹不存在就创建文件夹
                        if(!is_dir($path.$tmp_path))
                        {
                            mkdir($path.$tmp_path, 0777);
                        }
                    }
                }
            }
        }
        echo 'success';
    }
}
class SvnPeer {

    const SVN_USERNAME = "test";

    const SVN_PASSWORD = "test";

    const SVN_CONFIG_DIR = "/tmp/";//(任意指定一个临时目录,解决svn: warning: Can't open file '/root/.subversion/servers': Permission denied)

    /**
     * 导出单个文件
     *
     * @param $src string
     * @param $dst string
     * @return boolean
     *
     */
    public static function export($src, $dst) {
        $command = "svn export $src $dst";
        $output = self::runCmd ( $command );
        $output = implode ( "<br />", $output );
        if (strpos ( $output, 'Committed revision' )) {
            return true;
        }
        return "<br />" . $command . "<br />" . $output;
    }
    /**
     * 获取两个版本间修改的文件信息列表
     *
     * @param $fromVersion int
     * @param $headRevision int
     * @param $$path string
     *
     * @return array
     */
    public static function getChangedFiles($path, $fromVersion, $headRevision ){
        $files = array();
        $pipe = "";
        $command = "svn diff -r {$fromVersion}:{$headRevision} --summarize $path";
        $output = self::runCmd ( $command ,$pipe);
        $files = array_merge($files, $output);
        $command = "svn diff -r {$headRevision}:{$fromVersion} --summarize $path"; //文件删除可用逆向对比
        $output = self::runCmd ( $command ,$pipe);
        $files = array_merge($files, $output);
        return array_unique($files);
    }
    /**
     * 运行CMD 返回结果
     * @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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值