Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

PijulDiffJson.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.pijul.diff

import com.google.gson.GsonBuilder
import com.google.gson.annotations.SerializedName
import com.intellij.openapi.vcs.FileStatus
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import kotlin.reflect.javaType
import kotlin.reflect.typeOf

object PijulDiffJson {
    val GSON = GsonBuilder().disableHtmlEscaping().create()

    // TODO: Use Kotlin Flow and Flow-based Json reading
    @OptIn(ExperimentalStdlibApi::class)
    fun parseJson(json: String): List<ChangeEntry> =
        if (json.isEmpty()) emptyList()
        else GSON.fromJson<Map<String, List<ChangeData>>>(json, typeOf<Map<String, List<ChangeData>>>().javaType).entries.map {
            ChangeEntry(it.key, it.value)
        }
}

fun List<ChangeEntry>.toFileStatusMap(): Map<String, List<FileStatus>> =
    this.associateBy { it.path }.mapValues {
        it.value.data.map { it.toFileStatus() }
    }

fun ChangeData.toFileStatus(): FileStatus =
    when (this.operation) {
        "replacement", "edit" -> FileStatus.MODIFIED
        "file move" -> FileStatus.ADDED
        "file del" -> FileStatus.DELETED
        "file undel" -> FileStatus.ADDED
        "file add" -> FileStatus.ADDED
        // "solve name conflict" -> ?
        // "unsolve name conflict" -> ?
        // "solve order conflict" -> ?
        // "unsolve order conflict" -> ?
        // "resurrect zombies" -> ????????????????????
        else -> FileStatus.UNKNOWN
    }

data class ChangeEntry(val path: String, val data: List<ChangeData>)
data class ChangeData(@SerializedName("operation") val operation: String, @SerializedName("line") val line: Any?)