根据代码中关键字查找git提交人
工作中遇到一个需求,需要梳理业务日志文件中冗余日志,以便判断是否需要删除。梳理过程中把日志中打印的关键字筛选出来的,但这些关键字所在的代码是谁提交的,这个需要统计出来,以便后期与该提交人确认该行日志是否可以删除。于是写了个程序用来查找对应的git提交人。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 | 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(); } } } |
打印结果如下(只):
1 2 3 | 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 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统