<commit_before>/*
 * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 */
package git4idea.config;

import com.intellij.execution.configurations.PathEnvironmentVariableUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.util.ProgressIndicatorUtils;
import com.intellij.openapi.util.Ref;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @param <T> test result type
 */
abstract class CachingFileTester<T> {
  private static final int FILE_TEST_TIMEOUT_MS = 30000;

  private final ReentrantLock LOCK = new ReentrantLock();
  @NotNull private final ConcurrentMap<String, TestResult> myFileTestMap = new ConcurrentHashMap<>();

  @NotNull
  final TestResult getResultForFile(@NotNull String filePath) {
    return ProgressIndicatorUtils.computeWithLockAndCheckingCanceled(LOCK, 50, TimeUnit.MILLISECONDS, () -> {
      TestResult result = myFileTestMap.get(filePath);
      long currentLastModificationDate = 0L;

      try {
        currentLastModificationDate = Files.getLastModifiedTime(Paths.get(resolveAgainstEnvPath(filePath))).toMillis();
        if (result == null || result.getFileLastModifiedTimestamp() != currentLastModificationDate) {
          result = new TestResult(testFileOrAbort(filePath), currentLastModificationDate);
        }
      }
      catch (ProcessCanceledException pce) {
        throw pce;
      }
      catch (Exception e) {
        result = new TestResult(e, currentLastModificationDate);
      }

      myFileTestMap.put(filePath, result);
      return result;
    });
  }

  @NotNull
  private static String resolveAgainstEnvPath(@NotNull String filePath) {
    if (!filePath.contains(File.separator)) {
      File exeFile = PathEnvironmentVariableUtil.findInPath(filePath);
      if (exeFile != null) {
        return exeFile.getPath();
      }
    }
    return filePath;
  }

  @NotNull
  private T testFileOrAbort(@NotNull String filePath) throws Exception {
    EmptyProgressIndicator indicator = new EmptyProgressIndicator();
    Ref<Exception> exceptionRef = new Ref<>();
    Ref<T> resultRef = new Ref<>();

    Semaphore semaphore = new Semaphore(0);

    ApplicationManager.getApplication().executeOnPooledThread(<commit_msg>git: don't cache the error of git version check<commit_after>/*
 * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
 */
package git4idea.config;

import com.intellij.execution.configurations.PathEnvironmentVariableUtil;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.util.ProgressIndicatorUtils;
import com.intellij.openapi.util.Ref;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @param <T> test result type
 */
abstract class CachingFileTester<T> {
  private static final int FILE_TEST_TIMEOUT_MS = 30000;

  private final ReentrantLock LOCK = new ReentrantLock();
  @NotNull private final ConcurrentMap<String, TestResult> myFileTestMap = new ConcurrentHashMap<>();

  @NotNull
  final TestResult getResultForFile(@NotNull String filePath) {
    return ProgressIndicatorUtils.computeWithLockAndCheckingCanceled(LOCK, 50, TimeUnit.MILLISECONDS, () -> {
      TestResult result = myFileTestMap.get(filePath);
      long currentLastModificationDate = 0L;

      try {
        currentLastModificationDate = Files.getLastModifiedTime(Paths.get(resolveAgainstEnvPath(filePath))).toMillis();
        if (result == null || result.getFileLastModifiedTimestamp() != currentLastModificationDate) {
          result = new TestResult(testFileOrAbort(filePath), currentLastModificationDate);
        }

        myFileTestMap.put(filePath, result);
      }
      catch (ProcessCanceledException pce) {
        throw pce;
      }
      catch (Exception e) {
        result = new TestResult(e, currentLastModificationDate);
      }

      return result;
    });
  }

  @NotNull
  private static String resolveAgainstEnvPath(@NotNull String filePath) {
    if (!filePath.contains(File.separator)) {
      File exeFile = PathEnvironmentVariableUtil.findInPath(filePath);
      if (exeFile != null) {
        return exeFile.getPath();
      }
    }
    return filePath;
  }

  @NotNull
  private T testFileOrAbort(@NotNull String filePath) throws Exception {
    EmptyProgressIndicator indicator = new EmptyProgressIndicator();
    Ref<Exception> exceptionRef = new Ref<>();
    Ref<T> resultRef = new Ref<>();

    Semaphore semaphore = new Semaphore(0);

    ApplicationManager.getApplication().executeOnPooledThread(
{'fraction_non_alphanumeric': 0.09434958634043303, 'fraction_numerical': 0.0070410139060024645, 'mean_word_length': 4.42175572519084, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, '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': '6645199', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1623, 'n_words': 361}