package de.retest.recheck.persistence;

import static java.util.concurrent.TimeUnit.MILLISECONDS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GitExecutor {

	protected static final String GIT_COMMAND_VERSION = "git --version";
	protected static final String GIT_COMMAND_BRANCH_NAME = "git rev-parse --abbrev-ref HEAD";
	protected static final String GIT_COMMAND_COMMIT_HASH = "git rev-parse HEAD";
	protected static final String GIT_COMMAND_STATUS = "git status";

	private final boolean localGit;

	public GitExecutor() {
		localGit = isGitInstalled() && isGitUsed();
		log.debug( "Looks like git is {}used", localGit ? "" : "not " );
	}

	protected boolean isGitInstalled() {
		try {
			final String result = executeGitCommand( GIT_COMMAND_VERSION );
			return result != null && result.contains( "git version" );
		} catch ( final Exception e ) {
			log.debug( "`{}` resulted in exception {}, assuming no git installed.", GIT_COMMAND_VERSION,
					e.getMessage() );
			return false;
		}
	}

	protected boolean isGitUsed() {
		try {
			final String result = executeGitCommand( GIT_COMMAND_STATUS );
			return result != null && !result.contains( "fatal: not a git repository" );
		} catch ( final Exception e ) {
			log.debug( "`{}` resulted in exception {}, assuming no git used.", GIT_COMMAND_STATUS, e.getMessage() );
			return false;
		}
	}

	protected String executeGitCommand( final String command ) throws IOException, InterruptedException {
		final Process process = Runtime.getRuntime().exec( command );
		try ( final BufferedReader reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) ) {
			process.waitFor( 500, MILLISECONDS );
			return reader.readLine();
		} finally {
			process.destroy();
		}
	}

	public String getCurrentBranch() {
		if ( localGit ) {
			try {
				final String result = executeGitCommand( GIT_COMMAND_BRANCH_NAME );
				if ( result.contains( " " ) ) {
					return null;
				}
				return result;
			} catch ( final Exception e ) {
				log.error( "Exception retrieving Git branch.", e );
			}
		}
		return null;
	}

	public String getCurrentCommit() {
		if ( localGit ) {
			try {
				final String result = executeGitCommand( GIT_COMMAND_COMMIT_HASH );
				if ( result.contains( " " ) ) {
					return null;
				}
				return result;
			} catch ( final Exception e ) {
				log.error( "Exception retrieving Git branch.", e );
			}
		}
		return null;
	}
}

@@ -1882,68 +1882,22 @@
 %7B%0A%09%09
-if ( localGit ) %7B%0A%09%09%09try %7B%0A%09%09%09%09final String result =
+return
  execute
 GitC
@@ -1884,35 +1884,32 @@
 %09%09return execute
-Git
 Command( GIT_COM
@@ -1933,212 +1933,140 @@
 );%0A%09
-%09%09%09if ( result.contains( %22 %22 ) ) %7B%0A%09%09%09%09%09return null;%0A%09%09%09%09%7D%0A%09%09%09%09r
+%7D%0A%0A%09public String g
 et
+C
 ur
-n result;%0A%09%09%09%7D catch ( final Exception e ) %7B%0A%09%09%09%09log.error( %22Exception retrieving Git branch.%22, e
+rentCommit() %7B%0A%09%09return executeCommand( GIT_COMMAND_COMMIT_HASH
  );%0A
-%09%09
 %09%7D%0A
-%09%09%7D%0A%09%09return null;%0A%09%7D%0A%0A%09public
+%0A%09private String executeCommand( final
  Str
@@ -2070,32 +2070,26 @@
 String g
-etCurren
+i
 tComm
-it(
+and 
 ) %7B%0A%09%09if
@@ -2158,39 +2158,26 @@
 ommand( 
-GIT_COMMAND_COMMIT_HASH
+gitCommand
  );%0A%09%09%09%09

Simplify duplicate code