在java测试中获取当前的git分支

问题描述 投票:1回答:1

我需要在Java测试中获得当前Git分支的名称。可能我可以解析git status的结果。是否有其他方法或准备好的代码段?

java git
1个回答
3
投票

对我来说很好。

public static String getCurrentGitBranch() throws IOException, InterruptedException {
    Process process = Runtime.getRuntime().exec( "git rev-parse --abbrev-ref HEAD" );
    process.waitFor();

    BufferedReader reader = new BufferedReader(
            new InputStreamReader( process.getInputStream() ) );

    return reader.readLine();
}
© www.soinside.com 2019 - 2024. All rights reserved.