Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

PijulPushChanges.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.push

import com.github.jonathanxd.dracon.log.Author
import com.intellij.vcs.log.VcsUser
import org.tomlj.Toml
import java.io.Serializable
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.ZonedDateTime

import java.time.format.DateTimeFormatter




data class PijulPushChanges(val entries: List<PijulPushEntry>): Serializable {
    companion object {
        const val serialVersionUID = 1L
    }
}


data class PijulPushEntry(val hash: String,
                          val dependencies: List<String>,
                          val authors: List<Author>,
                          // TODO: Parse date
                          val date: ZonedDateTime,
                          val message: String): Serializable {


    companion object {
        const val serialVersionUID = 1L
    }
}

fun String.parseDate(): ZonedDateTime {
    val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.SSSSSSSSS] z")
    return ZonedDateTime.parse(this, formatter)
}

fun String.parsePijulPushChanges(): PijulPushChanges {
    val dependenciesPrefix = "  Dependencies: "
    val authorPrefix = "  Author: "
    val datePrefix = "  Date: "
    val spacePrefix = "  "
    val iter = this.lines().listIterator()
    val entries = mutableListOf<PijulPushEntry>()

    while (iter.hasNext()) {
        val line = iter.next()
        if (line.startsWith("#") || line.isEmpty()) {
            continue
        }

        if (line[0] != ' ') {
            val hash = line
            // Skip empty line
            iter.next()

            var next = iter.next()
            val dependencies = mutableListOf<String>()

            if (next.startsWith(dependenciesPrefix)) {
                dependencies.addAll(next.substring(dependenciesPrefix.length).split(" "))
                next = iter.next()
            }

            val authors = mutableListOf<Author>()
            if (next.startsWith(authorPrefix)) {
                val authorsToml = "authors = ${next.substring(authorPrefix.length)}"
                val toml = Toml.parse(authorsToml)

                val authorsArray = toml.getArray("authors")
                for (i in 0 until authorsArray!!.size()) {
                    val authorTable = authorsArray.getTable(i)
                    authors.add(
                        Author(
                            authorTable.getString("name"),
                            authorTable.getString("full_name"),
                            authorTable.getString("email")
                        )
                    )
                }

                next = iter.next()
            }


            val date = if (next.startsWith(datePrefix)) {
                val date = next.substring(datePrefix.length)
                next = iter.next()
                date
            } else {
                ""
            }

            val messages = mutableListOf<String>()
            if (next.trim().isEmpty()) {
                if (iter.hasNext()) {
                    next = iter.next()

                    while (next.startsWith(spacePrefix) || next.trim().isEmpty()) {
                        messages += if (next.trim().isEmpty()) {
                            ""
                        } else {
                            next.substring(spacePrefix.length)
                        }
                        if (!iter.hasNext()) break
                        next = iter.next()
                    }

                    if (next.isNotEmpty() && next[0] != ' ') {
                        iter.previous()
                    }
                }

            }

            entries.add(PijulPushEntry(
                hash,
                dependencies,
                authors,
                date.parseDate(),
                messages.joinToString("\n")
            ))
        }
    }

    return PijulPushChanges(entries)
}