<commit_before>package edu.nyu.cs.javagit.api;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import edu.nyu.cs.javagit.api.commands.CommandResponse;
import edu.nyu.cs.javagit.client.cli.IParser;
import edu.nyu.cs.javagit.client.cli.ProcessUtilities;
import edu.nyu.cs.javagit.utilities.ExceptionMessageMap;
import edu.nyu.cs.javagit.utilities.CheckUtilities;

/**
 * This class encapsulates our global API options that client code may want to get or set.
 * 
 * TODO (rs2705): Create a more generalized/modularized setup where variables and methods related to
 * specific implementation types are self-contained in their own separate classes, instead of
 * floating around in here.
 */
public final class JavaGitConfiguration {

  /*
   * The path to our git binaries. Default to null, which means that the git command is available
   * via the system PATH environment variable.
   */
  private static File gitPath = null;

  /*
   * The version string fpr the locally-installed git binaries.
   */
  private static String gitVersion = null;

  /**
   * Constructor - private because this is an all-static class.
   */
  private JavaGitConfiguration() {

  }

  /**
   * This function gets called in one of two ways:
   * 
   * 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.
   * 
   * 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;
  }

  /**
   * Return the complete string necessary to invoke git on the command line. Could be an absolute
   * path to git, or if the path was never set explicitly, just "git".
   * 
   * @return <code>String</code> containing the path to git, ending with the git executable's name
   *         itself.
   */
  public static String getGitCommand() {
    return getGitCommandPrefix() + "git";
  }

  /**
   * Return an absolute path capable of being dropped in front of the command-line git invocation.
   * If the path hasn't been set explicitly, just return the empty string and assume git is in this
   * process' PATH environment variable.
   * 
   * @return The <code>String</code> that points to the git executable.
   */
  public static String getGitCommandPrefix() {
    return ((gitPath == null) ? "" : (gitPath.getAbsolutePath() + File.pathSeparator));
  }

  /**
   * Accessor method for the <code>File</code> object representing the path to git. If the git
   * path is never set explicitly, this will return null.
   * 
   * @return <code>File</code> object pointing at the directory containing git.
   */
  public static File getGitPath() {
    return gitPath;
  }

  /**
   * Returns the version number of the underlying git binaries. If this method is called and we
   * don't know the version yet, it tries to figure it out. (The version gets set if
   * setGitPath() was previously called.)
   * 
   * @return The git version <code>String</code>.
   */
  public static String getGitVersion() throws JavaGitException {
    // If the version hasn't been found yet, let's do some lazy initialization here.
    if (gitVersion == null) {
      gitVersion = determineGitVersion(gitPath);
    }
<commit_msg>Incorporating comments from review javagit-14.  Javadocs changes.<commit_after>package edu.nyu.cs.javagit.api;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;

import edu.nyu.cs.javagit.api.commands.CommandResponse;
import edu.nyu.cs.javagit.client.cli.IParser;
import edu.nyu.cs.javagit.client.cli.ProcessUtilities;
import edu.nyu.cs.javagit.utilities.ExceptionMessageMap;
import edu.nyu.cs.javagit.utilities.CheckUtilities;

/**
 * This class encapsulates our global API options that client code may want to get or set.
 * 
 * TODO (rs2705): Create a more generalized/modularized setup where variables and methods related to
 * specific implementation types are self-contained in their own separate classes, instead of
 * floating around in here.
 */
public final class JavaGitConfiguration {

  /*
   * The path to our git binaries. Default to null, which means that the git command is available
   * via the system PATH environment variable.
   */
  private static File gitPath = null;

  /*
   * The version string fpr the locally-installed git binaries.
   */
  private static String gitVersion = null;

  /**
   * Constructor - private because this is an all-static class.
   */
  private JavaGitConfiguration() {

  }

  /**
   * This function gets called in one of two ways:
   * 
   * 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.
   * 
   * 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;
  }

  /**
   * Return the complete string necessary to invoke git on the command line. Could be an absolute
   * path to git, or if the path was never set explicitly, just "git".
   * 
   * @return <code>String</code> containing the path to git, ending with the git executable's name
   *         itself.
   */
  public static String getGitCommand() {
    return getGitCommandPrefix() + "git";
  }

  /**
   * Return an absolute path capable of being dropped in front of the command-line git invocation.
   * If the path hasn't been set explicitly, just return the empty string and assume git is in this
   * process' PATH environment variable.
   * 
   * @return The <code>String</code> that points to the git executable.
   */
  public static String getGitCommandPrefix() {
    return ((gitPath == null) ? "" : (gitPath.getAbsolutePath() + File.pathSeparator));
  }

  /**
   * Accessor method for the <code>File</code> object representing the path to git. If the git
   * path is never set explicitly, this will return null.
   * 
   * @return <code>File</code> object pointing at the directory containing git.
   */
  public static File getGitPath() {
    return gitPath;
  }

  /**
   * Returns the version number of the underlying git binaries. If this method is called and we
   * don't know the version yet, it tries to figure it out. (The version gets set if
   * {@link #setGitPath(File) setGitPath} was previously called.)
   * 
   * @return The git version <code>String</code>.
   */
  public static String getGitVersion() throws JavaGitException {
    // If the version hasn't been found yet, let's do some lazy initialization here.
    if (gitVersion == null) {
      gitVersion = determineGitVersion(gitPath);
    }

{'fraction_non_alphanumeric': 0.09014359156177983, 'fraction_numerical': 0.007622761921645098, 'mean_word_length': 3.107389879868948, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2770649', 'n_tokens_mistral': 3238, 'n_tokens_neox': 3050, 'n_words': 1266}