PijulChangeProvider.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.cache.PijulLogEntryChangeCache
import com.github.jonathanxd.dracon.cache.PijulLogRevisionCache
import com.github.jonathanxd.dracon.content.PijulContentRevision
import com.github.jonathanxd.dracon.log.*
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.revision.PijulRevisionNumber
import com.intellij.openapi.components.service
import com.intellij.openapi.progress.ProgressIndicator
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.FileStatus
import com.intellij.openapi.vcs.LocalFilePath
import com.intellij.openapi.vcs.ProjectLevelVcsManager
import com.intellij.openapi.vcs.VcsKey
import com.intellij.openapi.vcs.changes.*
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
class PijulChangeProvider(val project: Project, val key: VcsKey) : ChangeProvider {
private val logEntryChangeCache = project.service<PijulLogEntryChangeCache>()
private val logRevisionCache = project.service<PijulLogRevisionCache>()
override fun getChanges(
dirtyScope: VcsDirtyScope,
builder: ChangelistBuilder,
progress: ProgressIndicator,
addGate: ChangeListManagerGate
) {
if (project.isDisposed) return
val project = dirtyScope.project
val vcsManager = ProjectLevelVcsManager.getInstance(project)
val revisionMap = mutableMapOf<Path, PijulRevisionNumber?>()
val changesMap = mutableMapOf<Path, PijulLogEntry?>()
for (dir in dirtyScope.recursivelyDirtyDirectories + dirtyScope.dirtyFilesNoExpand) {
val root = vcsManager.getVcsRootFor(dir)
val rootAbsolute = root!!.toNioPath().toAbsolutePath()
if (dir.path.startsWith(rootAbsolute.resolve(".pijul").toString()))
continue;
if (dir.path.startsWith(rootAbsolute.resolve(".idea").toString()))
continue;
if (!dir.path.startsWith(rootAbsolute.toString()))
continue;
if (!dir.path.startsWith(project.baseDir.toNioPath().toAbsolutePath().toString()))
continue;
val rootPath = Paths.get(root.path)
val head = this.logRevisionCache.loadRevision(rootAbsolute) {
pijul(this.project).latestRevisionNumber(this.project, rootPath)
}.result ?: PijulRevisionNumber.notCommitted()
val change = this.logEntryChangeCache.load(rootAbsolute) {
pijul(this.project).diff(this.project, root)
}.result ?: continue
val knownPaths = mutableSetOf<Pair<Path, String>>()
for (hunk in change.hunks) {
when (hunk) {
is FileAddHunk -> {
val path = hunk.resolvePath(rootPath)
if (knownPaths.contains(path to "ADDED")) continue
knownPaths.add(path to "ADDED")
val filePath = LocalFilePath(path, Files.isDirectory(path))
builder.processChange(
Change(
null, CurrentContentRevision.create(filePath),
FileStatus.ADDED
), this.key)
}
is FileDelHunk -> {
val path = hunk.resolvePath(rootPath)
if (knownPaths.contains(path to "DELETED")) continue
knownPaths.add(path to "DELETED")
val filePath = LocalFilePath(path, Files.isDirectory(path))
builder.processChange(
Change(
PijulContentRevision(
rootPath,
path,
head,
this.project
), null,
FileStatus.DELETED
), this.key)
}
is ReplacementHunk -> {
val path = hunk.resolvePath(rootPath)
val filePath = LocalFilePath(path, Files.isDirectory(path))
if (knownPaths.contains(path to "MODIFIED")) continue
knownPaths.add(path to "MODIFIED")
builder.processChange(
Change(
PijulContentRevision(
rootPath,
filePath,
head,
this.project
), CurrentContentRevision.create(filePath),
FileStatus.MODIFIED
), this.key)
}
is EditHunk -> {
val path = hunk.resolvePath(rootPath)
val filePath = LocalFilePath(path, Files.isDirectory(path))
if (knownPaths.contains(path to "MODIFIED")) continue
knownPaths.add(path to "MODIFIED")
builder.processChange(
Change(
PijulContentRevision(
rootPath,
filePath,
head,
this.project
), CurrentContentRevision.create(filePath),
FileStatus.MODIFIED
), this.key)
}
else -> Unit
}
}
}
}
override fun isModifiedDocumentTrackingRequired(): Boolean = true
}