<?php /** * 导出指定版本之间的差异文件,如 100 和 200 之间的差异则导出 100(不包括) - 200(包括) 的所有修改 * 【SVN命令行】 * 1、查看版本间差异 * svn diff -r 2359:2360 --summarize --username heyw --password 123123 svn://192.168.0.10/rrc_cli * 2、导出某个版本文件到本地 * svn export -r 2360 svn://192.168.0.10/rrc_cli/common/controller/WechatBaseController.class.php /tmp/rrc_cli/common/controller/WechatBaseController.class.php --username heyw --password 123123 * * @example svn.php 60 62:63 70 * @author heyw */ // 初始化 # 项目配置 $svnUrl = array( 'rrc_cli' => 'svn://192.168.0.10/rrc_cli/' ); # 账号密码 $svnUser = 'heyw'; $svnPass = '123456'; # 根目录 define('SITE_PATH', dirname(__FILE__)); // 参数 $param = $_SERVER['argv']; if (empty($param) || count($param) < 3) { exit('run: php svn.php svn_project(www) change_version(60:61)'); } # 初始化 $svnPro = $param[1]; $changeVersion = $param[2]; if (empty($svnUrl[$svnPro])) { exit('wrong svn project'); } # 解析 $url = $svnUrl[$svnPro]; $params = explode(' ', $changeVersion); $diffFile = []; $maxVersion = 0; foreach ($params as $changeVer) { # 各版本之间的差异文件 $versionArr = explode(':', str_replace([':',':'], ':', $changeVer)); $oldVersion = $versionArr[0] - 1; $newVersion = empty($versionArr[1]) ? $versionArr[0] : $versionArr[1]; $diffCmd = "svn diff -r ${oldVersion}:${newVersion} --summarize --username ${svnUser} --password ${svnPass} ${url}"; exec($diffCmd, $diffList, $return); $diff = (array)$diffList; # 差异文件分析 foreach ($diff as $diffInfo) { if (preg_match('/([w]+)s+(svn:.+)/', $diffInfo, $matches)) { # 匹配变更类型,以及文件名称 $svnFileMode = $matches[1]; $svnFileName = $matches[2]; # 文件被删除,A、M、D、AM即增加且修改 if ($svnFileMode == 'D' || in_array($svnFileName, $diffFile)) { continue; } # 差异文件 && 日志 $diffFile[] = $svnFileName; echo $diffInfo . " "; } } $maxVersion = $newVersion; } # 版本差异 $diffCount = count($diffFile); if (count($diffFile) < 1) { exit("版本间没有差异"); } // 文件导出 # 目录,差异文件 $workPath = SITE_PATH . '/' . $svnPro; $diffFilePath = SITE_PATH . "/diff_" . str_replace(' ', '_', $changeVersion) . ".txt"; # 清空旧数据 @system('rm -rf ' . $workPath); @system('rm -rf ' . SITE_PATH . '/diff_*'); svnMkdir($workPath); # 导出版本文件 echo "开始导出... "; foreach ($diffFile as $svnFileName) { # 写日志 fileWrite($diffFilePath, $svnFileName . " ", 'a'); # 下载到本地 $localFilePath = $workPath . '/' . str_replace($url, '', $svnFileName); $localFileDir = dirname($localFilePath); svnMkdir($localFileDir); $export_cmd = "svn export -r ${maxVersion} ${svnFileName} ${localFilePath} --username ${svnUser} --password ${svnPass}"; system($export_cmd); } exit("共导出${diffCount}个差异文件"); /** * 创建文件夹 * * @param string $path 文件夹路径 * @param int $mode 访问权限 * @param bool $recursive 是否递归创建 * @return bool */ function svnMkdir($path = '', $mode = 0777, $recursive = true) { clearstatcache(); if (!is_dir($path)) { mkdir($path, $mode, $recursive); return chmod($path, $mode); } return true; } /** * 写文件 * * @param string $filename 文件名 * @param string $text 要写入的文本字符串 * @param string $openmod 文本写入模式('w':覆盖重写,'a':文本追加) * @return bool */ function fileWrite($filename = '', $text = '', $openmod = 'w') { if (@$fp = fopen($filename, $openmod)) { flock($fp, 2); fwrite($fp, $text); fclose($fp); return true; } else { return false; } }