Skip to content

Commit

Permalink
refactor: remove usage of VcsLogMultiRepoJoiner from IJ
Browse files Browse the repository at this point in the history
Fixes: #142
  • Loading branch information
lppedd committed May 16, 2024
1 parent 0261d17 commit 09e009d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.EmptyConsumer
import com.intellij.vcs.log.*
import com.intellij.vcs.log.data.VcsLogMultiRepoJoiner
import com.intellij.vcs.log.impl.VcsLogManager
import com.intellij.vcs.log.impl.VcsProjectLog
import com.intellij.vcs.log.visible.filters.VcsLogFilterObject
Expand Down
66 changes: 66 additions & 0 deletions src/main/kotlin/com/github/lppedd/cc/vcs/VcsLogMultiRepoJoiner.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.github.lppedd.cc.vcs

import com.intellij.vcs.log.graph.GraphCommit

/**
* This is a copy of the IJ `VcsLogMultiRepoJoiner` class,
* that has been made package private starting from 2024.2.
*/
internal class VcsLogMultiRepoJoiner<CommitId, Commit : GraphCommit<CommitId>> {
fun join(logsFromRepos: Collection<List<Commit>>): List<Commit> {
if (logsFromRepos.size == 1) {
return logsFromRepos.first()
}

val result = ArrayList<Commit>(getInitialSize(logsFromRepos))
val nextCommits = HashMap<Commit, Iterator<Commit>>()

for (log in logsFromRepos) {
val iterator = log.iterator()

if (iterator.hasNext()) {
nextCommits[iterator.next()] = iterator
}
}

while (nextCommits.isNotEmpty()) {
val lastCommit = findLatestCommit(nextCommits.keys)
val iterator = nextCommits.getValue(lastCommit)

result.add(lastCommit)
nextCommits.remove(lastCommit)

if (iterator.hasNext()) {
nextCommits[iterator.next()] = iterator
}
}

return result
}

private fun findLatestCommit(commits: Set<Commit>): Commit {
var maxTimeStamp = Long.MIN_VALUE
var lastCommit: Commit? = null

for (commit in commits) {
val timestamp = commit.timestamp

if (timestamp >= maxTimeStamp) {
maxTimeStamp = timestamp
lastCommit = commit
}
}

return checkNotNull(lastCommit)
}

private fun getInitialSize(logsFromRepos: Collection<List<Commit>>): Int {
var size = 0

for (repo in logsFromRepos) {
size += repo.size
}

return size
}
}

0 comments on commit 09e009d

Please sign in to comment.