override def load()

in src/main/scala/com/googlesource/gerrit/plugins/analytics/common/CommitsStatisticsLoader.scala [42:93]


  override def load(cacheKey: CommitsStatisticsCacheKey): CommitsStatistics = {
    import RevisionBrowsingSupport._

    val objectId = cacheKey.commitId
    val nameKey = Project.nameKey(cacheKey.projectName)
    val commentInfoList: Seq[CommentLinkInfo] =
      if(config.isExtractIssues) projectCache.get(nameKey).asScala.toList.flatMap(_.getCommentLinks.asScala) else Seq.empty
    val replacers = commentInfoList.map(info =>
      Replacer(
        info.`match`.r,
        Option(info.link).getOrElse(info.html)))

    use(gitRepositoryManager.openRepository(nameKey)) { repo =>

      // I can imagine this kind of statistics is already being available in Gerrit but couldn't understand how to access it
      // which Injection can be useful for this task?
        use(new RevWalk(repo)) { rw =>
          val reader = repo.newObjectReader()
          val commit = rw.parseCommit(objectId)
          val commitMessage = commit.getFullMessage

          val oldTree = {
            // protects against initial commit
            if (commit.getParentCount == 0)
              new EmptyTreeIterator
            else
              new CanonicalTreeParser(null, reader, rw.parseCommit(commit.getParent(0).getId).getTree)
          }

          val newTree = new CanonicalTreeParser(null, reader, commit.getTree)

          val df = new DiffFormatter(DisabledOutputStream.INSTANCE)
          df.setRepository(repo)
          df.setPathFilter(ignoreFileSuffixFilter)
          df.setDiffComparator(RawTextComparator.DEFAULT)
          df.setDetectRenames(true)
          val diffs = df.scan(oldTree, newTree).asScala

          val lines = (for {
            diff <- diffs
            edit <- df.toFileHeader(diff).toEditList.asScala
          } yield Lines(edit.getEndA - edit.getBeginA, edit.getEndB - edit.getBeginB)).fold(Lines(0, 0))(_ + _)

          val files: Set[String] = diffs.map(df.toFileHeader(_).getNewPath).toSet

          val commitInfo = CommitInfo(objectId.getName, commit.getAuthorIdent.getWhen.getTime, commit.isMerge, botLikeExtractor.isBotLike(files), files)
          val commitsStats = CommitsStatistics(lines.added, lines.deleted, commitInfo.merge, commitInfo.botLike, List(commitInfo), extractIssues(commitMessage, replacers).toList)

          commitsStats
        }
    }
  }