/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package git4idea.test;

import com.intellij.execution.process.CapturingProcessHandler;
import com.intellij.execution.process.ProcessOutput;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;

/**
 * Environment to run Git.
 * @author Kirill Likhodedov
 */
public class GitTestRunEnv {

  public static final String GIT_EXECUTABLE_ENV = "IDEA_TEST_GIT_EXECUTABLE";
  private String gitExecutable;
  private File myRootDir;

  private int myRetryCount;
  private static final int MAX_RETRIES = 3;


  public GitTestRunEnv(@NotNull File rootDir) {
    gitExecutable = getExecutable();
    myRootDir = rootDir;
  }

  /**
   * Run Git command and return the output.
   * @param command Command to run with parameters
   * @return
   */
  public String run(@NotNull String command, String... params) throws IOException {
    return run(false, command, params);
  }

  public String run(boolean silent, @NotNull String command, String... params) throws IOException {
    String[] arguments = ArrayUtil.mergeArrays(new String[]{gitExecutable, command}, params);
    if (!silent) {
      log("# " + StringUtil.join(arguments, " "));
    }
    final ProcessBuilder builder = new ProcessBuilder().command(arguments);
    builder.directory(myRootDir);
    builder.redirectErrorStream(true);
    Process clientProcess = builder.start();

    CapturingProcessHandler handler = new CapturingProcessHandler(clientProcess, CharsetToolkit.getDefaultSystemCharset());
    ProcessOutput result = handler.runProcess(30*1000);
    if (result.isTimeout()) {
      throw new RuntimeException("Timeout waiting for Git execution");
    }

    if (result.getExitCode() != 0) {
      log("{" + result.getExitCode() + "}");
    }
    String stdout = result.getStdout();
    final String out = stdout.trim();
    if (out.length() > 0) {
      log(out);
    }
    if (stdout.contains("fatal")) {
      if (stdout.contains("Unable to create") && stdout.contains(".git/index.lock") && myRetryCount <= MAX_RETRIES){
        // retry
        myRetryCount++;
        return run(silent, command, params);
      }
      myRetryCount = 0;
      throw new RuntimeException("fatal error during execution of Git command: " + StringUtil.join(arguments, " "));
    }
    myRetryCount = 0;
    return stdout;
  }

  protected void log(String message) {
    System.out.println(message);
  }

  private String getExecutable() {
    String exec = System.getenv(GIT_EXECUTABLE_ENV);
    if (exec != null && new File(exec).exists()) {
      log("Using Git from GIT_EXECUTABLE_ENV: " + exec);
      return exec;
    }
    File pluginRoot = new File(PluginPathManager.getPluginHomePath("git4idea"));
    File dir = new File(pluginRoot, "tests/git4idea/tests/data/bin");
    File git = new File(dir, SystemInfo.isWindows ? "git.exe" : "git");
    if (!git.exists()) {
      throw new IllegalStateException("git executable not found");
    }
    log("Using Git from IDEA sources: " + git.getPath());
    return git.getPath();
  }

}

@@ -1247,24 +1247,34 @@
 private 
+static 
 String 
-g
+ourG
 itExecut
@@ -1279,16 +1279,17 @@
 utable;%0A
+%0A
   privat
@@ -1433,17 +1433,20 @@
 ) %7B%0A    
-g
+ourG
 itExecut
@@ -1908,17 +1908,20 @@
 tring%5B%5D%7B
-g
+ourG
 itExecut
@@ -1976,16 +1976,36 @@
   log(%22#
+ git %22 + command + %22
  %22 + Str
@@ -2013,32 +2013,29 @@
 ngUtil.join(
+p
 ar
-gument
+am
 s, %22 %22));%0A  
@@ -3285,24 +3285,97 @@
 cutable() %7B%0A
+    if (ourGitExecutable != null) %7B%0A      return ourGitExecutable;%0A    %7D%0A
     String e

save the detected executable in static field to reuse in this session; use more clean logging