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

import com.softwaremill.codebrag.service.commits.CommitsLoader
import com.softwaremill.codebrag.domain.{RepositoryStatus, CommitInfo}
import com.typesafe.scalalogging.slf4j.Logging
import com.softwaremill.codebrag.dao.repositorystatus.RepositoryStatusDAO
import org.eclipse.jgit.lib.ObjectId
import com.softwaremill.codebrag.repository.Repository
import com.softwaremill.codebrag.repository.config.RepoData


class JgitCommitsLoader(converter: JgitLogConverter, repoStatusDao: RepositoryStatusDAO) extends CommitsLoader with Logging {

  def loadNewCommits(repoData: RepoData) = {
    val repo = repoData.buildRepository
    val newCommits = updateAndGetCommits(repo)
    val commitInfos = converter.toCommitInfos(newCommits, repo.repo)
    LoadCommitsResult(commitInfos, repo.repoName, ObjectId.toString(repo.currentHead))
  }

  private def updateAndGetCommits(repo: Repository) = {
    val lastKnownCommitSHA = repoStatusDao.get(repo.repoName)
    try {
      repo.pullChanges()
      val newCommits = repo.getCommits(lastKnownCommitSHA)
      updateRepoReadyStatus(repo)
      newCommits
    } catch {
      case e: Exception => {
        updateRepoNotReadyStatus(repo, e)
        logger.error(s"Could not pull repo or load new commits: ${e.getMessage}")
        throw e
      }
    }
  }

  private def updateRepoNotReadyStatus(repo: Repository, cause: Exception) {
    logger.debug(s"Saving repository-not-ready status data to DB with message: ${cause.getMessage}")
    val repoNotReadyStatus = RepositoryStatus.notReady(repo.repoName, Some(cause.getMessage))
    repoStatusDao.updateRepoStatus(repoNotReadyStatus)
  }

  private def updateRepoReadyStatus(repo: Repository) {
    val currentHead = ObjectId.toString(repo.currentHead)
    val repoReadyStatus = RepositoryStatus.ready(repo.repoName).withHeadId(currentHead)
    repoStatusDao.updateRepoStatus(repoReadyStatus)
  }

}

case class LoadCommitsResult(commits: List[CommitInfo], repoName: String, currentRepoHeadSHA: String)<commit_msg>Minor - Logging of repo status saving<commit_after>package com.softwaremill.codebrag.service.commits.jgit

import com.softwaremill.codebrag.service.commits.CommitsLoader
import com.softwaremill.codebrag.domain.{RepositoryStatus, CommitInfo}
import com.typesafe.scalalogging.slf4j.Logging
import com.softwaremill.codebrag.dao.repositorystatus.RepositoryStatusDAO
import org.eclipse.jgit.lib.ObjectId
import com.softwaremill.codebrag.repository.Repository
import com.softwaremill.codebrag.repository.config.RepoData


class JgitCommitsLoader(converter: JgitLogConverter, repoStatusDao: RepositoryStatusDAO) extends CommitsLoader with Logging {

  def loadNewCommits(repoData: RepoData) = {
    val repo = repoData.buildRepository
    val newCommits = updateAndGetCommits(repo)
    val commitInfos = converter.toCommitInfos(newCommits, repo.repo)
    LoadCommitsResult(commitInfos, repo.repoName, ObjectId.toString(repo.currentHead))
  }

  private def updateAndGetCommits(repo: Repository) = {
    val lastKnownCommitSHA = repoStatusDao.get(repo.repoName)
    try {
      repo.pullChanges()
      val newCommits = repo.getCommits(lastKnownCommitSHA)
      updateRepoReadyStatus(repo)
      newCommits
    } catch {
      case e: Exception => {
        updateRepoNotReadyStatus(repo, e)
        logger.error(s"Could not pull repo or load new commits: ${e.getMessage}")
        throw e
      }
    }
  }

  private def updateRepoNotReadyStatus(repo: Repository, cause: Exception) {
    logger.debug(s"Saving repository-not-ready status data to DB with message: ${cause.getMessage}")
    val repoNotReadyStatus = RepositoryStatus.notReady(repo.repoName, Some(cause.getMessage))
    repoStatusDao.updateRepoStatus(repoNotReadyStatus)
  }

  private def updateRepoReadyStatus(repo: Repository) {
    logger.debug(s"Saving repository-ready status data to DB with HEAD: ${repo.currentHead}")
    val currentHead = ObjectId.toString(repo.currentHead)
    val repoReadyStatus = RepositoryStatus.ready(repo.repoName).withHeadId(currentHead)
    repoStatusDao.updateRepoStatus(repoReadyStatus)
  }

}

case class LoadCommitsResult(commits: List[CommitInfo], repoName: String, currentRepoHeadSHA: String)
{'fraction_non_alphanumeric': 0.08634443923040826, 'fraction_numerical': 0.0004692632566870014, 'mean_word_length': 5.038243626062323, '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': '6546646', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1300, 'n_words': 237}