1. 程式人生 > 其它 >根據程式碼中關鍵字查詢git提交人

根據程式碼中關鍵字查詢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();
        }
    }
}