-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove usage of VcsLogMultiRepoJoiner from IJ
Fixes: #142
- Loading branch information
Showing
2 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/kotlin/com/github/lppedd/cc/vcs/VcsLogMultiRepoJoiner.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |