写了一个自动抓取更新文件并生成patches的工具

本文介绍了一个用于自动创建多种脚本语言(如Shell、Ruby、Perl、Python等)补丁的Java工具。该工具通过解析指定文本文件中的指令来复制源目录下的文件到目标目录,适用于自动化测试场景。
部署运行你感兴趣的模型镜像
/*
* Author: sambafeng
* date:2011-04-25
*/
/*
* TODO:auto create patches for script language programe,such as shell,ruby,perl,python
*/
package util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class AutoCreatePatch{
/*
* TODO:create the patches by the key words:sourcedir,destdir,patchTxtName
*/
@SuppressWarnings("unchecked")
public void createPatches(String pre_sourcedir,String pre_destdir,String patchTxtName) throws FileNotFoundException, IOException{
List num=new ArrayList();
HashMap hm=new HashMap();
String suff_pos_name;
/*empty the destdir */
deletefile(pre_destdir);
System.out.println("Cleaning:"+pre_destdir);
new File(pre_destdir).mkdir();
System.out.println("Mkdir:"+pre_destdir+" finished!");
try{
BufferedReader in = new BufferedReader(new FileReader(patchTxtName));
String str;
int row = 0;
int row_pre=0;
while((str = in.readLine())!= null){
row++;
row_pre=row-1;
hm.put(row,str);
if(str.equals("===================================================================")){
num.add(row_pre);
}
}
for(int i=0;i<num.size();i++){
suff_pos_name=(String) hm.get(num.get(i));//Index: basefunction/base_resources.rb
String fullfilename=suff_pos_name.substring(suff_pos_name.indexOf("Index: ")+7);//ct/fd/
File fullsourceFile=new File(pre_sourcedir+"/"+fullfilename);//source file
File fulldestFile=new File(pre_destdir+"/"+fullfilename);//dest file
// System.out.println(fulldestFile.getParentFile());
String fulldestDir=fulldestFile.getParent().toString();
new File(fulldestDir).mkdirs();
copyFile(fullsourceFile,fulldestFile);
System.out.println("Copy "+fullsourceFile+" to "+fulldestFile);
}
in.close();
System.out.println("Make Patches finished! Count "+num.size()+" Files");
}catch(IOException e){
System.out.println("error");
}
}
/*
* copy the file from sourceFile to destFile
*
*/
public static void copyFile(File sourceFile,File destFile) throws IOException{
FileInputStream input = new FileInputStream(sourceFile);
BufferedInputStream inBuff=new BufferedInputStream(input);
FileOutputStream output = new FileOutputStream(destFile);
BufferedOutputStream outBuff=new BufferedOutputStream(output);
byte[] b = new byte[1024 * 5];
int len;
while ((len =inBuff.read(b))!=-1){
outBuff.write(b,0,len);
}
outBuff.flush();
inBuff.close();
outBuff.close();
output.close();
input.close();
}



public static boolean deletefile(String destDir)throws FileNotFoundException, IOException {
try {
File file = new File(destDir);
if (!file.isDirectory()) {
file.delete();
} else if (file.isDirectory()) {
String[] filelist = file.list();
for (int i=0;i<filelist.length; i++) {
File delfile = new File(destDir+"\\"+filelist[i]);
if (!delfile.isDirectory()){
delfile.delete();
}
else if(delfile.isDirectory()) {
deletefile(destDir+"\\"+filelist[i]);
}
}
file.delete();
}
} catch (FileNotFoundException e) {
System.out.println("Exception:" + e.getMessage());
}
return true;
}

public static void main(String args[]) throws IOException{
AutoCreatePatch ap=new AutoCreatePatch();
ap.createPatches("D:\\workspace\\Ctrip_AutomationTest","E:/patch/Ctrip_AutomationTest","E:\\patch.txt");

}
}

您可能感兴趣的与本文相关的镜像

Yolo-v8.3

Yolo-v8.3

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

<think> 我们要生成某个文件夹(目录)的patch文件应用到另一个分支。这里的关键是生成整个目录的变更补丁,而不是单个文件。 注意:Git本身没有直接生成目录补丁的命令,但我们可以通过组合多个文件的补丁或者使用git diff来生成包含目录下所有变更的补丁。 步骤: 1. 生成补丁:我们可以使用git diff命令,指定目录路径,重定向输出到patch文件。 2. 应用补丁:使用git apply命令应用该patch文件到另一个分支。 具体操作如下: </think> 要在 Git 中生成指定文件夹的补丁文件应用到另一个分支,请按以下步骤操作: ### 完整操作流程 1. **生成文件夹补丁** 在源分支执行(支持通配符): ```bash # 生成最近一次提交的文件夹补丁 git format-patch -1 -o patches/ -- <文件夹路径> # 生成特定提交范围的文件夹补丁 git format-patch <起始提交>..<结束提交> -o patches/ -- <文件夹路径> ``` 示例(生成 src/utils 文件夹的补丁): ```bash git format-patch HEAD~1 -o patches/ -- src/utils ``` 2. **切换到目标分支** ```bash git checkout target-branch ``` 3. **应用补丁** ```bash git am patches/*.patch ``` - 使用 `-3` 参数启用三方合解决冲突 ```bash git am -3 patches/*.patch ``` ### 关键注意事项 1. **路径处理** - 确保源分支和目标分支的文件夹路径**完全一致** - 如果路径不同,需手动编辑补丁文件: ```diff --- a/old_path/utils +++ b/new_path/utils # 修改此行 ``` 2. **冲突解决方案** ```bash # 发生冲突时 git am --skip # 跳过当前补丁 git am --abort # 终止整个应用过程 git am --resolved # 手动解决冲突后继续 ``` 3. **替代方法** 使用 `git diff` + `git apply` 组合: ```bash # 生成补丁 git diff commitA..commitB -- <文件夹路径> > folder.patch # 应用补丁 git apply --directory=<新路径> folder.patch ``` ### 文件夹补丁特点 | 特性 | 说明 | |------|------| | 完整历史 | 保留所有提交信息和元数据 | | 批量处理 | 一次性传输多个文件变更 | | 权限保留 | 保持文件执行权限等属性 | | 依赖处理 | 自动处理文件间依赖关系 |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值