I need to get name of the current Git branch inside a Java test. Probably I can parse git status results. Is any other approach or a ready code snippet is available?
Works fine for me.
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();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With