您的位置:首页 > 其它

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

2016-04-26 16:18 381 查看
<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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  svn