Fork channel

Create a new channel as a copy of main.

Rename channel

Rename main to:

Delete channel

Delete main? This cannot be undone.

Pijul.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

import com.github.jonathanxd.dracon.changes.PijulCommittedChangeList
import com.github.jonathanxd.dracon.channel.ChannelInfo
import com.github.jonathanxd.dracon.editor.EditorServer
import com.github.jonathanxd.dracon.log.PijulLog
import com.github.jonathanxd.dracon.log.PijulLogEntry
import com.github.jonathanxd.dracon.log.PijulTag
import com.github.jonathanxd.dracon.pijul.credit.PijulCredit
import com.github.jonathanxd.dracon.revision.ChannelRevisions
import com.github.jonathanxd.dracon.revision.PijulRevisionNumber
import com.github.jonathanxd.dracon.revision.PijulVcsFileRevision
import com.github.jonathanxd.dracon.util.existsOrNull
import com.github.jonathanxd.dracon.util.isDirectoryOrNull
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FilePath
import com.intellij.openapi.vcs.FileStatus
import com.intellij.openapi.vfs.LocalFileSystem
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
import com.intellij.vcs.log.VcsUser
import com.intellij.vcsUtil.VcsUtil
import java.nio.file.Path
import java.nio.file.Paths

val PIJUL_DIR = ".pijul"

val PIJUL_INSTANCE get() = service<Pijul>()

fun pijul(project: Project): Pijul = project.service()

/**
 * Base interface for Pijul-IntelliJ communication.
 *
 * This interface provides all methods for communication with [Pijul], as well some basic implementations
 * for operations that does not requires communication with [Pijul] backend.
 *
 * However, some functionalities are not provided through this interface, for example, Pijul changes are stored in binary
 * format in the `.pijul/changes` directory and channels (like branches in git) are stored in `.pijul/pristine` directory,
 * as binary data as well, introspecting this data is done through [PijulChanges] and [PijulPristine] interfaces.
 */
interface Pijul {
    companion object {
        fun isPijulRepository(root: Path) =
            root.resolve(PIJUL_DIR).existsOrNull()?.isDirectoryOrNull() != null

        fun findPijulDirectory(root: VirtualFile): VirtualFile? =
            findPijulDirectory(Paths.get(VcsUtil.getFilePath(root).path))

        fun findPijulDirectory(root: Path): VirtualFile? {
            var dir: Path? = root

            while (dir != null) {
                if (isPijulRepository(dir)) {
                    return LocalFileSystem.getInstance().findFileByNioFile(dir)
                }
                dir = dir.parent
            }

            return null
        }

        fun findPijulDirectoryPath(root: Path): Path? {
            var dir: Path? = root

            while (dir != null) {
                if (isPijulRepository(dir)) {
                    return dir
                }
                dir = dir.parent
            }

            return null
        }

        fun isUnderPijul(root: VirtualFile): Boolean = findPijulDirectory(root) != null

        fun isUnderPijul(root: Path): Boolean = findPijulDirectory(root) != null
    }

    @RequiresBackgroundThread
    fun init(project: Project, root: VirtualFile): PijulOperationResult<Unit>

    @RequiresBackgroundThread
    fun init(project: Project, root: Path): PijulOperationResult<Unit>

    @RequiresBackgroundThread
    fun add(project: Project, root: VirtualFile, paths: List<FilePath>): PijulOperationResult<Unit>

    @RequiresBackgroundThread
    fun fileStatus(project: Project, file: VirtualFile): PijulOperationResult<FileStatus>

    @RequiresBackgroundThread
    fun fileStatus(project: Project, file: Path): PijulOperationResult<FileStatus>

    @RequiresBackgroundThread
    fun fileStatusMap(project: Project): PijulOperationResult<PijulFilesStatus>

    fun trackedFiles(project: Project, root: Path): PijulOperationResult<List<Path>>

    @RequiresBackgroundThread
    fun latestRevisionNumber(project: Project, root: VirtualFile): PijulOperationResult<PijulRevisionNumber>

    @RequiresBackgroundThread
    fun latestRevisionNumber(project: Project, root: Path): PijulOperationResult<PijulRevisionNumber>

    @RequiresBackgroundThread
    fun revisionByChannel(project: Project, root: Path): PijulOperationResult<ChannelRevisions>

    @RequiresBackgroundThread
    fun latestRevisionNumberForPath(project: Project, root: Path, filePath: Path): PijulOperationResult<PijulRevisionNumber>

    @RequiresBackgroundThread
    fun rollbackTo(hash: String, project: Project, root: Path): PijulOperationResult<Boolean>

    @RequiresBackgroundThread
    fun unrecord(project: Project, root: Path, hash: String): PijulOperationResult<Boolean>

    @RequiresBackgroundThread
    fun reset(project: Project, root: Path): PijulOperationResult<Boolean>

    @RequiresBackgroundThread
    fun record(project: Project, root: Path, files: List<Path>, author: VcsUser?, message: String): PijulOperationResult<Boolean>

    @RequiresBackgroundThread
    fun recordToString(project: Project, root: Path): PijulOperationResult<String>

    @RequiresBackgroundThread
    fun record(project: Project, root: Path, editorServerConsumer: (EditorServer) -> Unit): PijulOperationResult<String>

    @RequiresBackgroundThread
    fun push(project: Project, root: Path, editorServerConsumer: (EditorServer) -> Unit): PijulOperationResult<String>

    @RequiresBackgroundThread
    fun push(project: Project, root: Path,
             fromChannel: String? = null,
             toChannel: String? = null,
             repository: String? = null,
             editorServerConsumer: (EditorServer) -> Unit): PijulOperationResult<String>

    @RequiresBackgroundThread
    fun pushInTerminal(project: Project, root: Path,
             fromChannel: String? = null,
             toChannel: String? = null,
             repository: String? = null,
             editorServerConsumer: (EditorServer) -> Unit): PijulOperationResult<String>

    @RequiresBackgroundThread
    fun recordFromString(project: Project, root: Path, record: String): PijulOperationResult<String>

    fun revisions(project: Project, root: Path, file: Path): PijulOperationResult<List<PijulVcsFileRevision>>

    fun allRevisions(project: Project, root: Path): PijulOperationResult<List<PijulRevisionNumber>>

    /**
     * Retrieves info about pijul channels.
     */
    @RequiresBackgroundThread
    fun channel(project: Project, root: VirtualFile): PijulOperationResult<ChannelInfo>

    /**
     * Retrieves info about pijul channels.
     */
    @RequiresBackgroundThread
    fun channel(project: Project, root: Path): PijulOperationResult<ChannelInfo>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun log(project: Project, root: VirtualFile): PijulOperationResult<PijulLog>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun log(project: Project, root: Path, channel: String): PijulOperationResult<PijulLog>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun log(project: Project, root: Path): PijulOperationResult<PijulLog>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun tags(project: Project, root: Path): PijulOperationResult<List<PijulTag>>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun fileHistory(project: Project,
                    root: VirtualFile,
                    file: FilePath,
                    consumer: (PijulLogEntry) -> Unit,
                    errorConsumer:  (PijulOperationResult<Unit>) -> Unit): PijulOperationResult<Unit>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun fileHistory(project: Project,
                    root: Path,
                    file: Path,
                    consumer: (PijulLogEntry) -> Unit,
                    errorConsumer:  (PijulOperationResult<Unit>) -> Unit): PijulOperationResult<Unit>

    /**
     * Retrieves difference from current revision and local files.
     */
    @RequiresBackgroundThread
    fun diff(project: Project, root: VirtualFile): PijulOperationResult<PijulLogEntry>

    fun diff(project: Project, root: Path): PijulOperationResult<PijulLogEntry>

    /**
     * Retrieves change history
     */
    @RequiresBackgroundThread
    fun credit(project: Project, root: Path, file: Path): PijulOperationResult<PijulCredit>

    fun changes(
        project: Project,
        root: Path,
        revision: String?,
        maxCount: Int,
        filter: (PijulLogEntry) -> Boolean,
        consumer: (PijulCommittedChangeList) -> Unit
    ): PijulOperationResult<Unit>

    fun untrackedFiles(project: Project, root: Path): PijulOperationResult<List<Path>>
}