<commit_before>package org.approvaltests.legacycode;

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

import javax.swing.JOptionPane;

import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.rules.TestWatchman;
import org.junit.runners.model.FrameworkMethod;

import com.spun.util.ObjectUtils;
import com.spun.util.StringUtils;
import com.spun.util.io.FileUtils;

public class TestCommitRevert
{
  public static boolean PRINT_ONLY = false;
  static int            failures   = 0;
  @Rule
  public MethodRule     watchman   = new TestWatchman()
                                   {
                                     public void starting(FrameworkMethod method)
                                     {
                                       // nothing
                                     }
                                     public void succeeded(FrameworkMethod method)
                                     {
                                       // nothing
                                     }
                                     public void failed(Throwable e, FrameworkMethod method)
                                     {
                                       failures++;
                                     }
                                   };
  @AfterClass
  public static void after() throws IOException
  {
    File gitDir = getHeadOfGit();
    if (gitDir == null)
    {
      System.out.println("No .git repo found at " + new File(".").getAbsolutePath());
      return;
    }
    if (failures == 0)
    {
      commit(gitDir);
    }
    else
    {
      revertGit(gitDir);
    }
  }
  private static File getHeadOfGit() throws IOException
  {
    File file = new File(".").getCanonicalFile();
    while (true)
    {
      //System.out.println("checking " + file.getAbsolutePath());
      File gitFile = new File(file, ".git");
      if (gitFile.exists())
      {
        return file;
      }
      else
      {
        file = file.getParentFile();
        if (file == null) { return null; }
      }
    }
  }
  private static void commit(File gitDir)
  {
    if (isGitEmpty(gitDir))
    {
      System.out.println("Nothing to commit");
      return;
    }
    String message = JOptionPane.showInputDialog("Test Passed! Please enter a commit message ");
    if (!StringUtils.isEmpty(message))
    {
      runOnConsole(gitDir, "git", "add", "-A");
      runOnConsole(gitDir, "git", "commit", "-m", '"' + message + '"');
    }
  }
  private static boolean isGitEmpty(File gitDir)
  {
    runOnConsole(gitDir, new String[]{"git", "status"});
    try
    {
      Process p = Runtime.getRuntime().exec(new String[]{"git", "status"}, null, gitDir);
      p.waitFor();
      String result = FileUtils.readStream(p.getInputStream());
      return result.contains("nothing to commit");
    }
    catch (Exception e)
    {
      ObjectUtils.throwAsError(e);
    }
    return false;
  }
  private static void revertGit(File gitDir)
  {
    runOnConsole(gitDir, "git", "reset", "--hard", "HEAD");
    String helpMessage = "Test Failed, reverting...\n\n Remember to auto refresh \n preferences > general>  workspace > ✓ refresh using native Hooks";
    System.out.println(helpMessage);
  }
  private static void runOnConsole(File workingDir, String... cmdArgs) throws Error
  {
    //System.out.println(Arrays.toString(cmdArgs));
    if (PRINT_ONLY) { return; }
    try
    {
      Process p = Runtime.getRuntime().exec(cmdArgs, null, workingDir);
      p.waitFor();<commit_msg>B added git clean on failure for new files<commit_after>package org.approvaltests.legacycode;

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

import javax.swing.JOptionPane;

import org.junit.AfterClass;
import org.junit.Rule;
import org.junit.rules.MethodRule;
import org.junit.rules.TestWatchman;
import org.junit.runners.model.FrameworkMethod;

import com.spun.util.ObjectUtils;
import com.spun.util.StringUtils;
import com.spun.util.io.FileUtils;

public class TestCommitRevert
{
  public static boolean PRINT_ONLY = false;
  static int            failures   = 0;
  @Rule
  public MethodRule     watchman   = new TestWatchman()
                                   {
                                     public void starting(FrameworkMethod method)
                                     {
                                       // nothing
                                     }
                                     public void succeeded(FrameworkMethod method)
                                     {
                                       // nothing
                                     }
                                     public void failed(Throwable e, FrameworkMethod method)
                                     {
                                       failures++;
                                     }
                                   };
  @AfterClass
  public static void after() throws IOException
  {
    File gitDir = getHeadOfGit();
    if (gitDir == null)
    {
      System.out.println("No .git repo found at " + new File(".").getAbsolutePath());
      return;
    }
    if (failures == 0)
    {
      commit(gitDir);
    }
    else
    {
      revertGit(gitDir);
    }
  }
  private static File getHeadOfGit() throws IOException
  {
    File file = new File(".").getCanonicalFile();
    while (true)
    {
      //System.out.println("checking " + file.getAbsolutePath());
      File gitFile = new File(file, ".git");
      if (gitFile.exists())
      {
        return file;
      }
      else
      {
        file = file.getParentFile();
        if (file == null) { return null; }
      }
    }
  }
  private static void commit(File gitDir)
  {
    if (isGitEmpty(gitDir))
    {
      System.out.println("Nothing to commit");
      return;
    }
    String message = JOptionPane.showInputDialog("Test Passed! Please enter a commit message ");
    if (!StringUtils.isEmpty(message))
    {
      runOnConsole(gitDir, "git", "add", "-A");
      runOnConsole(gitDir, "git", "commit", "-m", '"' + message + '"');
    }
  }
  private static boolean isGitEmpty(File gitDir)
  {
    runOnConsole(gitDir, new String[]{"git", "status"});
    try
    {
      Process p = Runtime.getRuntime().exec(new String[]{"git", "status"}, null, gitDir);
      p.waitFor();
      String result = FileUtils.readStream(p.getInputStream());
      return result.contains("nothing to commit");
    }
    catch (Exception e)
    {
      ObjectUtils.throwAsError(e);
    }
    return false;
  }
  private static void revertGit(File gitDir)
  {
    runOnConsole(gitDir, "git", "clean", "-fd");
    runOnConsole(gitDir, "git", "reset", "--hard", "HEAD");
    String helpMessage = "Test Failed, reverting...\n\n Remember to auto refresh \n preferences > general>  workspace > ✓ refresh using native Hooks";
    System.out.println(helpMessage);
  }
  private static void runOnConsole(File workingDir, String... cmdArgs) throws Error
  {
    //System.out.println(Arrays.toString(cmdArgs));
    if (PRINT_ONLY) { return; }
    try
    {
      Process p = Runtime.getRuntime().exec(cmdArgs, null, workingDir);
      p.waitFor();
{'fraction_non_alphanumeric': 0.11342657342657343, 'fraction_numerical': 0.0005594405594405594, 'mean_word_length': 1.773855702094647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1376258', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1950, 'n_words': 467}