PijulHistoryProvider.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.provider
import com.github.jonathanxd.dracon.context.PijulVcsContext
import com.github.jonathanxd.dracon.log.FileDelHunk
import com.github.jonathanxd.dracon.log.HunkWithPath
import com.github.jonathanxd.dracon.log.draconConsoleWriter
import com.github.jonathanxd.dracon.pijul.diff.PijulDiffFromHistoryHandler
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.revision.PijulRevisionNumber
import com.github.jonathanxd.dracon.revision.PijulVcsFileRevision
import com.intellij.openapi.actionSystem.ActionManager
import com.intellij.openapi.actionSystem.AnAction
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FilePath
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.VcsActions
import com.intellij.openapi.vcs.VcsException
import com.intellij.openapi.vcs.annotate.ShowAllAffectedGenericAction
import com.intellij.openapi.vcs.history.*
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.ui.ColumnInfo
import com.intellij.vcs.history.VcsHistoryProviderEx
import com.intellij.vcs.log.impl.VcsUserImpl
import java.nio.file.Files
import java.nio.file.Paths
import javax.swing.JComponent
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.isDirectory
import kotlin.io.path.name
import kotlin.io.path.relativeTo
@Service(Service.Level.PROJECT)
class PijulHistoryProvider(val project: Project) : VcsHistoryProvider,
VcsHistoryProviderEx {
override fun getUICustomization(
session: VcsHistorySession?,
forShortcutRegistration: JComponent?
): VcsDependentHistoryComponents {
return VcsDependentHistoryComponents(ColumnInfo.EMPTY_ARRAY, null, null)
}
override fun getAdditionalActions(refresher: Runnable?): Array<AnAction> {
return arrayOf(
ShowAllAffectedGenericAction.getInstance(),
ActionManager.getInstance().getAction(VcsActions.ACTION_COPY_REVISION_NUMBER)
)
}
override fun isDateOmittable(): Boolean =
false
override fun getHelpId(): String? = null
override fun createSessionFor(filePath: FilePath): VcsHistorySession? {
// TODO: Support multiple-channels
val root = ProjectLevelVcsManager.getInstance(this.project).getVcsRootFor(filePath)!!
val channels = pijul(this.project).channel(this.project, root)
val currentChannel = channels.result?.channels?.firstOrNull { it.current }?.name
val history = pijul(this.project).log(this.project, root).map {
it.entries.map {
PijulVcsFileRevision(
this.project,
root.toNioPath(),
filePath,
PijulRevisionNumber(it.changeHash, it.date),
it.authors.map { VcsUserImpl(it.name ?: "", it.email ?: "") }.filter { it.name.isNotEmpty() },
it.message,
currentChannel,
false
)
}
}
return if (history.result != null) {
PijulHistorySession(filePath, history.result.firstOrNull()?.revision, history.result)
} else {
draconConsoleWriter(this.project).logError("Failed to build history session")
null
}
}
override fun reportAppendableHistory(
path: FilePath,
startingRevision: VcsRevisionNumber?,
partner: VcsAppendableHistorySessionPartner
) {
reportAppendableHistory(path, partner)
}
override fun getLastRevision(filePath: FilePath): VcsFileRevision? {
val ctx = this.project.service<PijulVcsContext>()
val root = ctx.root
val revisions = pijul(project).revisions(this.project, root, ctx.resolveUnderVcs(filePath))
return revisions.result?.firstOrNull()
}
@OptIn(ExperimentalPathApi::class)
override fun reportAppendableHistory(path: FilePath, partner: VcsAppendableHistorySessionPartner) {
val root = ProjectLevelVcsManager.getInstance(this.project).getVcsRootFor(path)!!
val emptySession = createSession(path, emptyList(), null)
val ctx = this.project.service<PijulVcsContext>()
val pathToView = ctx.resolveUnderVcs(path)
partner.reportCreatedEmptySession(emptySession)
val channels = pijul(this.project).channel(this.project, root)
val currentChannel = channels.result?.channels?.firstOrNull { it.current }?.name
pijul(this.project).fileHistory(
this.project,
root,
path,
{
/*val deleted = it.hunks.filterIsInstance<HunkWithPath>().filter {
it.resolvePath(ctx.root) == path
}.filterIsInstance<FileDelHunk>().isNotEmpty()
partner.acceptRevision(
PijulVcsFileRevision(
this.project,
root.toNioPath(),
pathToView,
PijulRevisionNumber(it.changeHash, it.date),
it.authors.map { VcsUserImpl(it.name ?: "", it.email ?: "") }.filter { it.name.isNotEmpty() },
it.message,
currentChannel,
deleted
)
)*/
// TODO: Should we limit to show only one revision when introspecting directories instead of all revisions?
it.hunks.filterIsInstance<HunkWithPath>().groupBy {
it.resolvePath(root.toNioPath())
}.filter { pathToView.isDirectory() || it.key == pathToView }.forEach { path, hunks ->
val deleted = hunks.any { it is FileDelHunk }
if (!Files.isDirectory(path)) {
val msg = if (Files.isDirectory(pathToView)) {
"File ${path.name}"
} else {
it.message.replace("\n", " ").trim()
}
partner.acceptRevision(
PijulVcsFileRevision(
this.project,
root.toNioPath(),
path,
PijulRevisionNumber(it.changeHash, it.date),
it.authors.map { VcsUserImpl(it.name ?: "", it.email ?: "") }.filter { it.name.isNotEmpty() },
msg,
currentChannel,
deleted
)
)
}
}
},
{ partner.reportException(VcsException(it.toString())) }
)
}
override fun supportsHistoryForDirectories(): Boolean = true
override fun getHistoryDiffHandler(): DiffFromHistoryHandler =
PijulDiffFromHistoryHandler(this.project)
override fun canShowHistoryFor(file: VirtualFile): Boolean {
return true// TODO?
}
private fun createSession(
filePath: FilePath, revisions: List<VcsFileRevision>,
number: VcsRevisionNumber?
): VcsAbstractHistorySession {
return PijulHistorySession(filePath, number, revisions)
}
inner class PijulHistorySession(
val filePath: FilePath,
number: VcsRevisionNumber?,
revisions: List<VcsFileRevision>
) :
VcsAbstractHistorySession(revisions, number) {
override fun calcCurrentRevisionNumber(): VcsRevisionNumber? {
return try {
val root = ProjectLevelVcsManager.getInstance(project).getVcsRootFor(filePath)!!
val rev = pijul(project).latestRevisionNumber(project, root)
return rev.result
} catch (e: Throwable) {
draconConsoleWriter(project).logError(e.stackTraceToString())
null
}
}
override fun getHistoryAsTreeProvider(): HistoryAsTreeProvider? {
return null
}
override fun copy(): VcsHistorySession {
return createSession(filePath, revisionList, currentRevisionNumber)
}
}
}