使用 SVN 作为版本控制的时候,整理过一个 导出文件脚本:利用 PHP 导出 SVN 新增或修改过的文件

现在换成了 Git,整理出类似的脚本:

【第一版】git.php

<?php
/**
* 导出指定版本之间的差异文件,如 100 和 200 之间的差异则导出 100(不包括) - 200(包括) 的所有修改
* 【Git 命令行】
* 1、查看版本间差异
* git diff b361dbd323dbfc442ddbdc43b00cba3e66c50214 988526fa12dfcbddc2462e232eae7f614ff28976 --name-status
*
* @example git.php old版本号 new版本号
* @author phpgo.cnblogs.com
*/ // 根目录
define('SITE_PATH', '/Users/jianbao/123'); // Git 工作区
define('GIT_WORK_PATH', '/Users/jianbao/PhpStormProjects/fiisoo/git_sjf_web'); // 项目路径前缀
$prefix = 'sjf_'; $error_msg = "You must useage like {$_SERVER['argv'][0]} old_version(不包括) new_version(包括)\n";
if ($_SERVER['argc'] != 3) {
echo $error_msg;
exit(1);
} $old_version = $_SERVER['argv'][1];
$new_version = $_SERVER['argv'][2]; //$export_path = SITE_PATH . "/${prefix}${old_version}_${new_version}";
$export_path = SITE_PATH . "/${prefix}" . date('H:i:s'); // 切换至 Git 工作区
chdir(GIT_WORK_PATH); echo "开始分析版本差异...\n";
$diff_cmd = "git diff ${old_version} ${new_version} --name-status"; exec($diff_cmd, $diff_list, $return);
$diff_list = (array)$diff_list;
foreach ($diff_list as $diff_info) {
echo $diff_info . "\n";
} // 清空旧数据
@system('rm -rf ' . SITE_PATH . "/${prefix}*"); // 新建文件夹
dir_mkdir($export_path); $diff_count = count($diff_list);
if ($diff_count < 1) {
echo "版本间没有差异\n";
exit(1);
} $diff_count = 0; // 导出版本差异文件
echo "开始导出...\n";
foreach ($diff_list as $diff_info) {
if (preg_match('/([\w]+)\s+(.+)/', $diff_info, $matches)) {
$git_file_mode = $matches[1];
$git_file_name = $matches[2]; // A、M、D、AM(即增加且修改)
// 文件被删除
if ($git_file_mode == 'D') {
continue;
}
$diff_count++; // 复制到导出目录
$from_file_path = GIT_WORK_PATH . '/' . $git_file_name;
$to_file_path = $export_path . '/' . $git_file_name;
$to_file_dir = dirname($to_file_path);
dir_mkdir($to_file_dir); copy($from_file_path, $to_file_path);
}
} echo "共导出${diff_count}个差异文件\n";
exit(0); /**
* 创建文件夹
*
* @param string $path 文件夹路径
* @param int $mode 访问权限
* @param bool $recursive 是否递归创建
* @return bool
*/
function dir_mkdir($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 file_write($filename = '', $text = '', $openmod = 'w') {
if (@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $text);
fclose($fp);
return true;
} else {
return false;
}
} /**
*【本地调试用】写对象(包括 数字、字符串、数组)
*
* @param string $text 要写入的文本字符串
* @param string $type 文本写入类型('w':覆盖重写,'a':文本追加)
* @return bool
*/
function write2($text, $type = 'a') {
$filename = SITE_PATH . '/write2.txt'; $text = "\n++++++++++++++++++++++++++++++++++++++++++\n"
. date('Y-m-d H:i:s') . "\n"
. print_r($text, true);
return file_write($filename, $text, $type);
}

【第二版】git_sjf_mas.php

<?php
/**
* 导出 某个分支(如:master/develop) 下,指定版本之间的差异文件,
* 如 100 和 200 之间的差异则导出 100(不包括) - 200(包括) 的所有修改
*
* @example git_sjf_mas.php old版本号 new版本号
* @author phpgo.cnblogs.com
*/ // 检查参数
$errorMsg = "【出错】You must useage like {$_SERVER['argv'][0]} old_version(不包括) new_version(包括)\n";
if ($_SERVER['argc'] != 3) {
echo $errorMsg;
exit(1);
} //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // 项目路径
$projectUrl = 'git@git.cnblogs.com:phpgo/sjf_web.git'; // 分支名
$branchName = 'master'; // 开发分支 改为 develop // 本地项目名
$projectName = 'git_sjf_mas'; // 开发分支 改为 git_sjf_dev // 输出路径
$exportPath = '/Users/jianbao/123'; // Git 工作区
$gitWorkPath = '/Users/jianbao/1/projects'; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ $subGitWorkPath = $gitWorkPath . "/${projectName}"; // 克隆 远程 develop 分支
if (!is_dir($subGitWorkPath)) {
// 切换至 Git 工作区
chdir($gitWorkPath); // 克隆 分支
system("git clone ${projectUrl} --branch ${branchName} ${subGitWorkPath}"); // 新建本地分支 develop
chdir($subGitWorkPath);
system("git branch ${branchName}");
} else {
chdir($subGitWorkPath);
} // 切换分支 develop
system("git checkout ${branchName}"); // 更新分支
system("git pull origin ${branchName}"); $oldVersion = $_SERVER['argv'][1];
$newVersion = $_SERVER['argv'][2]; // 检查 版本号
$oldVersionCmd = "git log --pretty='%h' -1 ${oldVersion}";
$newVersionCmd = "git log --pretty='%h' -1 ${newVersion}";
$ret = 0;
system($oldVersionCmd, $ret);
if ($ret !== 0) {
echo "【出错】版本号 ${oldVersion} 不存在\n";
exit(1);
} system($newVersionCmd, $ret);
if ($ret !== 0) {
echo "【出错】版本号 ${newVersion} 不存在\n";
exit(1);
} // 输出路径
$subExportPath = $exportPath . "/${projectName}_" . date('H:i:s'); echo "开始分析版本差异...\n";
$diffCmd = "git diff --name-status ${oldVersion} ${newVersion}"; exec($diffCmd, $diffList, $return);
$diffList = (array)$diffList;
foreach ($diffList as $diffInfo) {
echo $diffInfo . "\n";
} // 清空旧数据
//@system('DELTREE ' . $exportPath . "/${projectName}*"); $dh = opendir($exportPath);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullpath = $exportPath . "/" . $file;
if (is_dir($fullpath) && (strpos($file, $projectName) !== false)) {
dir_rmdir($fullpath);
}
}
}
closedir($dh); // 新建文件夹
dir_mkdir($subExportPath); $diffCount = count($diffList);
if ($diffCount < 1) {
echo "版本间没有差异\n";
exit(1);
} $diffCount = 0; // 导出版本差异文件
echo "开始导出...\n";
foreach ($diffList as $diffInfo) {
if (preg_match('/([\w]+)\s+(.+)/', $diffInfo, $matches)) {
$gitFileMode = $matches[1];
$gitFileName = $matches[2]; // A、M、D、AM(即增加且修改)
// 文件被删除
if ($gitFileMode == 'D') {
continue;
}
$diffCount++; // 复制到导出目录
$fromFilePath = $subGitWorkPath . '/' . $gitFileName;
$toFilePath = $subExportPath . '/' . $gitFileName;
$toFileDir = dirname($toFilePath);
dir_mkdir($toFileDir); copy($fromFilePath, $toFilePath);
}
} echo "共导出 ${diffCount} 个差异文件\n";
exit(0); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /**
* 创建文件夹
*
* @param string $path 文件夹路径
* @param int $mode 访问权限
* @param bool $recursive 是否递归创建
* @return bool
*/
function dir_mkdir($path = '', $mode = 0777, $recursive = true) {
clearstatcache();
if (!is_dir($path)) {
mkdir($path, $mode, $recursive);
return chmod($path, $mode);
} return true;
} /**
* 清空/删除 文件夹
*
* @param string $dir 文件夹路径
* @param bool $self 是否删除自己
* @return bool
*/
function dir_rmdir($dir, $self = true) {
$dh = opendir($dir);
while ($file = readdir($dh)) {
if ($file != "." && $file != "..") {
$fullpath = $dir . "/" . $file;
if (!is_dir($fullpath)) {
unlink($fullpath);
} else {
dir_rmdir($fullpath);
}
}
}
closedir($dh);
if ($self&& rmdir($dir)) {
return true;
} else {
return false;
}
} /**
* 写文件
*
* @param string $filename 文件名
* @param string $text 要写入的文本字符串
* @param string $openmod 文本写入模式('w':覆盖重写,'a':文本追加)
* @return bool
*/
function file_write($filename = '', $text = '', $openmod = 'w') {
if (@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $text);
fclose($fp);
return true;
} else {
return false;
}
} /**
*【本地调试用】写对象(包括 数字、字符串、数组)
*
* @param string $text 要写入的文本字符串
* @param string $type 文本写入类型('w':覆盖重写,'a':文本追加)
* @return bool
*/
function write2($text, $type = 'a') {
$filename = $GLOBALS['exportPath'] . '/write2.txt'; $text = "\n++++++++++++++++++++++++++++++++++++++++++\n"
. date('Y-m-d H:i:s') . "\n"
. print_r($text, true);
return file_write($filename, $text, $type);
}

利用 PHP 导出 Git 某个分支下,新增或修改过的文件的更多相关文章

  1. Mac Pro 利用PHP导出SVN新增或修改过的文件

    先前在 Windows 操作系统下,习惯用 TortoiseSVN 导出新增或修改过的文件([相当实用]如何让TortoiseSVN导出新增或修改过的文件 ),最近换成了 Mac Pro 笔记本电脑, ...

  2. 如何让TortoiseSVN导出新增或修改过的文件

    利用Windows系统下的TortoiseSVN客户端,可以导出指定版本之间修改过的文件,并保留完整的文件夹结构.下面我就来说说操作的步骤: 1.在网站项目的根目录下右键选择 “TortoiseSVN ...

  3. 【相当实用】如何让TortoiseSVN导出新增或修改过的文件

    当一个网站项目进入运营维护阶段以后,不会再频繁地更新全部源文件到服务器,这个时间的修改大多是局部的,因此更新文件只需更新修改过的文件,其他没有修改过的文件就没有必要上载到服务器.但一个稍微上规模的网站 ...

  4. 在git多分支repo仓库中彻底清除大文件

    坑的由来 repo中不小心上传了许多测试生成的data.结果可想而知,原本只有代码的仓库突然间变得无比臃肿(或者是慢慢臃肿),从早期的几十MB,迅速飙升至1G. 到底发生了什么 早些时候我对git的原 ...

  5. Linux系统下查找最近修改过的文件

    Linux的终端上,没有windows的搜索那样好用的图形界面工具,但find命令确是很强大的. 比如按名字查找一个文件,可以用 find / -name targetfilename . 唉,如果只 ...

  6. Git查看两个版本之间修改了哪些文件

    gdiff 63e3b647d55fcc643e793e650c893be8601719b1 548cdaf01dbc2f08d1ca0b697a24afe512b75a2f --stat git l ...

  7. Lunix/Mac下根据最后修改时间复制文件和文件夹,保持原有的目录结构

    度娘知道:http://zhidao.baidu.com/link?url=DD47jm6qDgT7yxsnz9e-NC4Fqd33oRoiIwcGLkw5TL4cbf50VKY2IONbHKH0IE ...

  8. git的介绍、git的功能特性、git工作流程、git 过滤文件、git多分支管理、远程仓库、把路飞项目传到远程仓库(非空的)、ssh链接远程仓库,协同开发

    Git(读音为/gɪt/)是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理. [1] 也是Linus Torvalds为了帮助管理Linux内核开发而开发的一个开放源码 ...

  9. 使用git创建分支

    Git大法好--3.Git分支本地操作详解 这时已经切换到了dingBranch分支下面了,在项目文件夹下添加一个dingBranchtest.txt文件,然后提交到本地仓库和远程仓库: git ad ...

随机推荐

  1. Interval Sum I && II

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  2. wordpress配置通过IP直接访问及apache的配置

    wordpress配置通过IP直接访问 环境: 操作系统:centos6.5 yum安装lamp环境: yum -y install mysql mysql-server php php-mysql ...

  3. MySQL root密码忘记后更优雅的解决方法

    MySQL root密码忘记后更优雅的解决方法 https://www.jb51.net/article/143453.htm /usr/bin/mysqld_safe --defaults-file ...

  4. ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、

    1.Request.ApplicationPath->当前应用的目录    Jsp中, ApplicationPath指的是当前的application(应用程序)的目录,ASP.NET中也是这 ...

  5. dubbo启动报错failed to bind nettyserver on

    dubbo报错 今天启动项目的时候,关掉了custom服务, <dubbo:consumer check="false"/> 并且关掉了spring的elastic-j ...

  6. 使用BigDecimal转换较长小数时候出现科学计数法的问题

    public static String divToString(double v1, double v2, int scale){ if (scale < 0) { throw new Ill ...

  7. ps不显示命令本身的进程号

    当我们查看某个服务的进程时候,它会把命令本身的进程显示出来.如下图: 进程号2383 就是我命令本身的进程号,和我实际想看的进程无关 特别是在我们写脚本,kill进程时候会报错: 解决办法可以优化脚本 ...

  8. Net Web Api Route

    Asp.Net Web Api Route   在目前的主流架构中,我们越来越多的看到web Api的存在,小巧,灵活,基于Http协议,使它在越来越多的微服务项目或者移动项目充当很好的service ...

  9. koa+orm2

    koa+orm2 koa是由 Express 原班人马打造的新的web框架.套用其官方的说法:Koa 应用是一个包含一系列中间件 generator 函数的对象. 这些中间件函数基于 request ...

  10. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...