<commit_before>package com.softwaremill.codebrag.service.commits.jgit

import org.eclipse.jgit.api._
import java.nio.file.{Paths, Path}
import org.eclipse.jgit.storage.file.FileRepository
import org.eclipse.jgit.lib.{Constants, ObjectId}
import org.eclipse.jgit.transport.CredentialsProvider
import com.typesafe.scalalogging.slf4j.Logging
import org.eclipse.jgit.util.FileUtils

class JgitFacade extends Logging {

  def clone(remote: String, branch: String, localPath: Path, credentials: CredentialsProvider): Git = {
    try {
      new CloneCommand().setURI(remote).setCredentialsProvider(credentials).setDirectory(localPath.toFile).setBranch(branch).call()
    } catch {
      case e: Exception => {
        logger.error("Could not clone. Cleanning up - removing partially initialized repository.")
        FileUtils.delete(localPath.toFile, FileUtils.RECURSIVE)
        throw e
      }
    }
  }

  def pull(localPath: Path, credentials: CredentialsProvider): Git = {

    val repository = getRepository(localPath)
    val git = new Git(repository)

    val pullResult = git.pull().setCredentialsProvider(credentials).call()
    if (!pullResult.isSuccessful) throw new IllegalStateException(s"Git pull to $localPath failed. Cause: $pullResult")
    git
  }

  def gitRepo(localPath : Path) : Git = {
    val repository = getRepository(localPath)
    new Git(repository)
  }

  def getHeadId(localPath: Path): ObjectId = {
    val repository = getRepository(localPath)
    repository.resolve(Constants.HEAD)
  }

  private def getRepository(localGitPath: Path): FileRepository = {
    val internalPath = internalGitPath(localGitPath)
    new FileRepository(internalPath.toFile)<commit_msg>Setting a timeout on jgit's pull. It seems this sometimes hangs, so a timeout can help.<commit_after>package com.softwaremill.codebrag.service.commits.jgit

import org.eclipse.jgit.api._
import java.nio.file.{Paths, Path}
import org.eclipse.jgit.storage.file.FileRepository
import org.eclipse.jgit.lib.{Constants, ObjectId}
import org.eclipse.jgit.transport.CredentialsProvider
import com.typesafe.scalalogging.slf4j.Logging
import org.eclipse.jgit.util.FileUtils

class JgitFacade extends Logging {
  private val JGitCommandTimeoutSeconds = 60 * 10

  def clone(remote: String, branch: String, localPath: Path, credentials: CredentialsProvider): Git = {
    try {
      new CloneCommand().setURI(remote).setCredentialsProvider(credentials).setDirectory(localPath.toFile).setBranch(branch).call()
    } catch {
      case e: Exception => {
        logger.error("Could not clone. Cleanning up - removing partially initialized repository.")
        FileUtils.delete(localPath.toFile, FileUtils.RECURSIVE)
        throw e
      }
    }
  }

  def pull(localPath: Path, credentials: CredentialsProvider): Git = {

    val repository = getRepository(localPath)
    val git = new Git(repository)

    val pullResult = git.pull().setCredentialsProvider(credentials).setTimeout(JGitCommandTimeoutSeconds).call()
    if (!pullResult.isSuccessful) throw new IllegalStateException(s"Git pull to $localPath failed. Cause: $pullResult")
    git
  }

  def gitRepo(localPath : Path) : Git = {
    val repository = getRepository(localPath)
    new Git(repository)
  }

  def getHeadId(localPath: Path): ObjectId = {
    val repository = getRepository(localPath)
    repository.resolve(Constants.HEAD)
  }

  private def getRepository(localGitPath: Path): FileRepository = {
    val internalPath = internalGitPath(localGitPath)
    new FileRepository(internalPath.toFile)
{'fraction_non_alphanumeric': 0.09949267192784668, 'fraction_numerical': 0.0016910935738444193, 'mean_word_length': 4.377272727272727, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1094590', 'n_tokens_mistral': 1056, 'n_tokens_neox': 1024, 'n_words': 233}