Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get current git branch inside a java test

Tags:

java

git

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?

like image 662
Alex T Avatar asked Mar 05 '18 08:03

Alex T


1 Answers

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();
}
like image 52
Alex T Avatar answered Oct 12 '22 23:10

Alex T