fun getBranchingCommit()

in src/main/kotlin/com/jetbrains/interactiveRebase/services/GraphService.kt [83:107]


    fun getBranchingCommit(graphInfo: GraphInfo): CommitInfo {
        if (graphInfo.mainBranch.currentCommits.isEmpty() || graphInfo.addedBranch == null) {
            throw IRInaccessibleException(
                "Branching-off commit cannot be displayed. Cannot find the added branch or the commits on the primary branch",
            )
        }
        val addedBranch: BranchInfo = graphInfo.addedBranch!!
        val lastInPrimary = graphInfo.mainBranch.currentCommits.last()
        val primaryParents: List<Hash> = lastInPrimary.commit.parents

        if (primaryParents.isEmpty()) {
            throw IRInaccessibleException("Trying to display parents of initial commit")
        }
        var branchingHash: Hash = primaryParents[0]

        // if there are multiple options for a branching commit, compare with added branch and choose the common parent
        if (primaryParents.size > 1 && addedBranch.currentCommits.isNotEmpty()) {
            val addedParents: Set<Hash> = addedBranch.currentCommits.last().commit.parents.toSet()
            val intersection: Set<Hash> = primaryParents.intersect(addedParents)
            branchingHash = if (intersection.isEmpty()) branchingHash else intersection.first()
        }
        val parentCommit = commitService.turnHashToCommit(branchingHash.asString())

        return commitService.getCommitInfoForBranch(listOf(parentCommit)).first()
    }