热门标签 | HotTags
当前位置:  开发笔记 > 开发工具 > 正文

根据代码中关键字查找git提交人

工作中遇到一个需求,需要梳理业务日志文件中冗余日志,以便判断是否需要删除。梳理过程中把日志中打印的关键字筛选出来的,但这些关键字所在的代码是谁提交的,这个需求统计出来,以便后期与该

工作中遇到一个需求,需要梳理业务日志文件中冗余日志,以便判断是否需要删除。梳理过程中把日志中打印的关键字筛选出来的,但这些关键字所在的代码是谁提交的,这个需求统计出来,以便后期与该提交人确认该行日志是否可以删除。于是写了个程序用来查找对应的git提交人。

import com.google.common.io.LineReader;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class ReadCodeFile {
/**
* 根据关键字搜索文件,以及对应的最后一次提交人
*
* @param systemPath
* @param keyword
* @param file
*/
public static void singleFile(String systemPath, String keyword, File file) {
// 读取文件
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
String line;
int lineNo = 1;
while ((line = br.readLine()) != null) {
if (line.indexOf(keyword) >= 0) {
String filePath = file.getPath().replace(systemPath + "\\", "");
String[] cmds = new String[3];
cmds[0] = "cmd.exe" ; // 调用外部程序
cmds[1] = "/C" ; // 外部程序参数,是执行完命令后关闭命令窗口。 /K是不关闭窗口,这里导致进程不结束,java主线程挂起
cmds[2] = "cd /d " + systemPath + " & git --git-dir " + systemPath + "\\.git blame " + filePath + " -L" +
lineNo+"," + lineNo;//cmd调用的命令
System.out.println(cmds[2]);
Process process = Runtime.getRuntime().exec(cmds);
int waitFor = process.waitFor();
// System.out.println("waitFor:" + waitFor);
// System.out.println("process.exitValue():" + process.exitValue());
LineReader lineReader = new LineReader(new InputStreamReader(process.getInputStream()));
String outputLine;
while ((outputLine = lineReader.readLine()) != null){
System.out.println(outputLine);
// 获取提交人名称
String commiter = outputLine.split(" ")[1];
System.out.println("最近一次提交人:" + commiter.replace("(", ""));
}
LineReader lineReader2 = new LineReader(new InputStreamReader(process.getErrorStream()));
String outputLine2;
while ((outputLine2 = lineReader2.readLine()) != null){
System.out.println();
System.out.println("++++++++++++++++++++++++++ 执行异常:" + outputLine2);
}
}
lineNo++;
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 读取文件,如果是目录则遍历该目录所有文件和子目录
*
* @param systemPath
* @param keyword
* @param file
*/
public static void fileDictory(String systemPath, String keyword, File file) {
try {
if (file != null && file.isDirectory()) {
// 当前路径是目录,遍历该目录
File[] fileArray = file.listFiles();
for (File f : fileArray) {
fileDictory(systemPath, keyword, f);
}
} else if (file.getName().endsWith("java")) {
// 当前路径是文件,只搜索java文件
singleFile(systemPath, keyword, file);
}
} catch (Exception e) {
e.printStackTrace();
;
}
}
public static void main(String[] args) {
// 项目本地路径
String systemPath = "D:\\project\\myproject";
// 查找关键字
String[] keywordArray = {"业务报警通知未配置接收人"};
try {
for (String keyword : keywordArray) {
fileDictory(systemPath, keyword, new File(systemPath));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

 打印结果如下(只):

cd /d D:\project\myproject & git --git-dir D:\project\myproject\.git blame myproject\pj-alarm\src\main\java\com\liang\alarm\record\service\AlarmRecordServiceImpl.java -L127,127
d960fdc499 (atai555 2020-04-15 19:35:49 +0800 127) LoggerUtils.logBizInfo("业务报警通知未配置接收人");
最近一次提交人:atai555

  



推荐阅读
author-avatar
mobiledu2502911073
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有