我需要在 Java 测试中获取当前 Git 分支的名称。也许我可以解析git status结果。是否有其他方法或现成的代码片段可用?
对我来说很好用。
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2045 次 |
| 最近记录: |