PijulRepository.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.repository
import com.github.jonathanxd.dracon.config.PijulConfig
import com.github.jonathanxd.dracon.context.PijulVcsContext
import com.github.jonathanxd.dracon.ignore.PijulLocalIgnoredFileHolder
import com.github.jonathanxd.dracon.pijul.pijul
import com.github.jonathanxd.dracon.pijulVcs
import com.intellij.dvcs.ignore.VcsIgnoredHolderUpdateListener
import com.intellij.dvcs.ignore.VcsRepositoryIgnoredFilesHolder
import com.intellij.dvcs.repo.Repository
import com.intellij.dvcs.repo.RepositoryImpl
import com.intellij.openapi.Disposable
import com.intellij.openapi.components.service
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.Disposer
import com.intellij.openapi.vcs.AbstractVcs
import com.intellij.openapi.vcs.FilePath
import com.intellij.openapi.vcs.changes.ChangesViewI
import com.intellij.openapi.vcs.changes.ChangesViewManager
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.messages.Topic
import org.jetbrains.annotations.NotNull
class PijulRepository(project: @NotNull Project, dir: @NotNull VirtualFile, parentDisposable: @NotNull Disposable) :
RepositoryImpl(project, dir, parentDisposable) {
private val vcs = pijulVcs(project)
val pijulConfig: PijulConfig by lazy {
PijulConfig(project.service<PijulVcsContext>().root)
}
private val localIgnoredHolder: PijulLocalIgnoredFileHolder by lazy {
PijulLocalIgnoredFileHolder(this, project.service())
}
init {
localIgnoredHolder.setupListeners()
Disposer.register(this, localIgnoredHolder)
localIgnoredHolder.addUpdateStateListener(
MyIgnoredHolderAsyncListener(
getProject()
)
)
update()
}
companion object {
val PIJUL_REPO_CHANGE: Topic<PijulRepositoryChangeListener> = Topic.create(
"PijulRepository change",
PijulRepositoryChangeListener::class.java
)
fun getInstance(
root: VirtualFile,
project: Project,
parentDisposable: Disposable
): PijulRepository {
val repository = PijulRepository(project, root, parentDisposable)
return repository
}
}
override fun getState(): Repository.State = Repository.State.NORMAL
override fun getCurrentBranchName(): String? =
pijul(this.project).channel(project, project.baseDir).result?.channels?.firstOrNull { it.current }?.name?.trim()
override fun getVcs(): AbstractVcs =
this.vcs
override fun getCurrentRevision(): String? =
pijul(this.project).latestRevisionNumber(project, project.baseDir).result?.hash
override fun update() {
}
fun getIgnoredFilesHolder(): VcsRepositoryIgnoredFilesHolder =
this.localIgnoredHolder
override fun toLogString(): String =
"PijulRepository ${this.root}"
private class MyIgnoredHolderAsyncListener(project: Project) :
VcsIgnoredHolderUpdateListener {
private val changesViewI: ChangesViewI
private val project: Project
override fun updateStarted() {
changesViewI.scheduleRefresh()
}
override fun updateFinished(ignoredPaths: Collection<FilePath>, isFullRescan: Boolean) {
if (project.isDisposed) return
changesViewI.scheduleRefresh()
}
init {
changesViewI = ChangesViewManager.getInstance(project)
this.project = project
}
}
}