/**
   * This function gets called in one of two ways:
   *
   * <p>1) When client code sets the path to the git binaries explicitly by calling {@link
   * setGitPath}, this function is used to determine if the path is usable or not. In this case, the
   * <code>File</code> path argument will be the new path to the git binaries.
   *
   * <p>2) When client code calls {@link getGitVersion} and the path has not been set explicitly, we
   * call this function to figure out the version. In this case, the <code>File</code> path argument
   * will be null.
   *
   * @param path <code>File</code> object representing the directory containing the git binaries. If
   *     null, the previously-set path is used (if any). It must contain either an absolute path, or
   *     a valid path relative to the current working directory.
   * @return The git version string.
   * @throws JavaGitException Thrown if git is not found at the provided path.
   */
  private static String determineGitVersion(File path) throws JavaGitException {

    /*
     * If they already set the path explicitly, or are in the process of doing so (via the path
     * argument), make sure to prefix the git call with it. If they didn't, assume it's blank.
     */
    String gitPrefix = "";
    if (path != null) {
      // We got a path passed in as an argument.
      gitPrefix = path.getAbsolutePath() + File.pathSeparator;
    } else if (gitPath != null) {
      // They didn't pass in a path, but previously set it explicitly via setGitPath.
      gitPrefix = getGitCommandPrefix();
    }

    String gitCommand = gitPrefix + "git";
    if (!(gitPrefix.equals(""))) {
      // If we've got a full path to the git binary here, ensure it actually exists.
      if (!(new File(gitCommand).exists())) {
        throw new JavaGitException(100002, ExceptionMessageMap.getMessage("100002"));
      }
    }

    List<String> commandLine = new ArrayList<String>();
    commandLine.add(gitCommand);
    commandLine.add("--version");

    // Now run the actual git version command.
    GitVersionResponse response;
    try {
      // We're passing in a working directory of null, which is "don't care" to runCommand
      response =
          (GitVersionResponse)
              ProcessUtilities.runCommand(null, commandLine, new GitVersionParser());
    } catch (Exception e) {
      throw new JavaGitException(100001, ExceptionMessageMap.getMessage("100001"));
    }

    String version = response.getVersion();
    if (!(isValidVersionString(version))) {
      throw new JavaGitException(100001, ExceptionMessageMap.getMessage("100001"));
    }

    return version;
  }
  /**
   * Called when client code wants to explicitly tell us where to find git on their filesystem. If
   * never called, we assume that git is in a directory in the PATH environment variable for this
   * process. Passing null as the path argument will unset an explicitly-set path and revert to
   * looking for git in the PATH.
   *
   * @param path <code>File</code> object representing the directory containing the git binaries. It
   *     must contain either an absolute path, or a valid path relative to the current working
   *     directory.
   * @throws IOException Thrown if the provided path does not exist.
   * @throws JavaGitException Thrown if git does not exist at the provided path, or the provided
   *     path is not a directory.
   */
  public static void setGitPath(File path) throws IOException, JavaGitException {
    if (path != null) {
      CheckUtilities.checkFileValidity(path);

      if (!(path.isDirectory())) {
        throw new JavaGitException(
            020002,
            ExceptionMessageMap.getMessage("020002") + " { path=[" + path.getPath() + "] }");
      }
    }

    try {
      gitVersion = determineGitVersion(path);
    } catch (Exception e) {
      // The path that was passed in doesn't work. Catch any errors and throw this one instead.
      throw new JavaGitException(
          100002,
          ExceptionMessageMap.getMessage("100002") + " { path=[" + path.getPath() + "] }",
          e);
    }

    // Make sure we're hanging onto an absolute path.
    gitPath = (path != null) ? path.getAbsoluteFile() : null;
  }
 /**
  * Returns the <code>GitVersionResponse</code> object that's essentially just a wrapper around
  * the git version number.
  *
  * @return The response object containing the version number.
  */
 public CommandResponse getResponse() throws JavaGitException {
   if (!(parsedCorrectly)) {
     throw new JavaGitException(100001, ExceptionMessageMap.getMessage("100001"));
   }
   return new GitVersionResponse(version);
 }