Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

PijulVcsLogRefManager.kt
/**
 * Dracon - An IntelliJ-Pijul integration.
 * Copyright 2021 JonathanxD <jhrldev@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package com.github.jonathanxd.dracon.repository

import com.github.jonathanxd.dracon.channel.PijulChannelManager
import com.github.jonathanxd.dracon.channel.PijulChannelType
import com.github.jonathanxd.dracon.i18n.DraconBundle
import com.github.jonathanxd.dracon.util.BiDiMap
import com.github.jonathanxd.dracon.util.biDiMap
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.vcs.log.*
import com.intellij.vcs.log.impl.SimpleRefGroup
import com.intellij.vcs.log.impl.SimpleRefType
import java.io.DataInput
import java.io.DataOutput

class PijulVcsLogRefManager(val project: Project) : VcsLogRefManager {
    companion object {
        val CURRENT: VcsRefType = SimpleRefType("CURRENT", false, VcsLogStandardColors.Refs.TIP)
        val CHANNEL: VcsRefType = SimpleRefType("CHANNEL", true, VcsLogStandardColors.Refs.BRANCH)
        val TAG: VcsRefType = SimpleRefType("TAG", false, VcsLogStandardColors.Refs.TAG)
        val OTHER: VcsRefType = SimpleRefType("OTHER", true, VcsLogStandardColors.Refs.TAG)

        private val REF_TYPE_INDEX: BiDiMap<VcsRefType, Int> = biDiMap(
            CURRENT to 0,
            CHANNEL to 1,
            TAG to 2,
            OTHER to 100
        )
    }

    private val labelSorter = Comparator<VcsRef> { l, r ->
        REF_TYPE_INDEX[l.type]!!.compareTo(REF_TYPE_INDEX[r.type]!!)
    }

    private val channelSorter = Comparator<VcsRef> { l, r ->
        REF_TYPE_INDEX[l.type]!!.compareTo(REF_TYPE_INDEX[r.type]!!)
    }

    val channelManager = project.service<PijulChannelManager>()
    val repositoryManager = project.service<PijulRepositoryManager>()

    override fun getBranchLayoutComparator(): Comparator<VcsRef> = this.channelSorter

    override fun getLabelsOrderComparator(): Comparator<VcsRef> = this.labelSorter

    override fun groupForBranchFilter(refs: MutableCollection<out VcsRef>): MutableList<RefGroup> {
        val current = mutableListOf<VcsRef>()
        val channels = mutableListOf<VcsRef>()
        val tags = mutableListOf<VcsRef>()
        val others = mutableListOf<VcsRef>()

        for (ref in refs) {
            when (ref.type) {
                CURRENT -> {
                    current.add(ref)
                }
                CHANNEL -> {
                    channels.add(ref)
                }
                TAG -> {
                    tags.add(ref)
                }
                else -> {
                    others.add(ref)
                }
            }
        }

        val groups = mutableListOf<RefGroup>()

        if (current.isNotEmpty()) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.current"), current.toMutableList()))
        }

        if (channels.isNotEmpty()) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.channel"), channels.toMutableList()))
        }

        if (tags.isNotEmpty()) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.tag"), tags.toMutableList()))
        }

        if (others.isNotEmpty()) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.other"), others.toMutableList()))
        }

        return groups
    }

    override fun groupForTable(
        refs: MutableCollection<out VcsRef>,
        compact: Boolean,
        showTagNames: Boolean
    ): MutableList<RefGroup> {
        // TODO: Support compact and showTagNames
        val mainChannels = mutableListOf<VcsRef>()
        val channels = mutableListOf<VcsRef>()
        val tags = mutableListOf<VcsRef>()
        val others = mutableListOf<VcsRef>()

        for (ref in refs) {
            when (ref.type) {
                CURRENT -> {}
                CHANNEL -> {
                    if (ref.name == "main") {
                        mainChannels.add(ref)
                    } else {
                        channels.add(ref)
                    }
                }
                TAG -> {
                    tags.add(ref)
                }
                else -> {
                    others.add(ref)
                }
            }
        }

        val hasCurrent = refs.any { it.type == CURRENT }

        val groups = mutableListOf<RefGroup>()

        if (mainChannels.isNotEmpty() && hasCurrent) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.mainChannel"), mainChannels.toMutableList()))
        }

        if (channels.isNotEmpty()) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.channel"), channels.toMutableList()))
        }

        if (tags.isNotEmpty()) {
            val f = tags.first()
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.tag_text", f.name), tags.toMutableList()))
        }

        if (others.isNotEmpty()) {
            groups.add(SimpleRefGroup(DraconBundle.message("pijul.log.refGroup.other"), others.toMutableList()))
        }

        return groups
    }

    override fun serialize(out: DataOutput, type: VcsRefType) {
        out.writeInt(REF_TYPE_INDEX[type]!!)
    }

    override fun deserialize(input: DataInput): VcsRefType {
        val id = input.readInt()
        return REF_TYPE_INDEX.otherDirection[id]!!
    }

    override fun isFavorite(reference: VcsRef): Boolean =
        when (reference.type) {
            CURRENT -> true
            CHANNEL -> this.channelManager.isFavorite(
                PijulChannelType.CHANNEL,
                this.repositoryManager.getRepositoryForFileQuick(reference.root),
                reference.name
            )
            else -> false
        }


    override fun setFavorite(reference: VcsRef, favorite: Boolean) {
        when (reference.type) {
            CURRENT -> return
            CHANNEL -> this.channelManager.setFavorite(
                PijulChannelType.CHANNEL,
                this.repositoryManager.getRepositoryForFileQuick(reference.root),
                reference.name,
                favorite
            )
            else -> return
        }
    }

}