Added branch creaton/deletion methods and changes hasUncommitedChanges to be stateful

This commit is contained in:
Abdelilah El Aissaoui 2021-09-26 21:34:15 +02:00
parent b2a93cd339
commit 9a76d13483
7 changed files with 52 additions and 12 deletions

View File

@ -52,9 +52,6 @@ class GitManager {
private var git: Git? = null
var lastTimeStatusChanged: Long = 0 // TODO Add file watcher to the repository
private set
val safeGit: Git
get() {
val git = this.git
@ -110,11 +107,6 @@ class GitManager {
statusManager.loadStatus(safeGit)
}
fun updateStatus() {
lastTimeStatusChanged = System.currentTimeMillis()
loadStatus()
}
fun stage(diffEntry: DiffEntry) = managerScope.launch {
statusManager.stage(safeGit, diffEntry)
}
@ -168,10 +160,20 @@ class GitManager {
fun stash() = managerScope.launch {
stashManager.stash(safeGit)
loadStatus()
}
fun popStash() = managerScope.launch {
stashManager.popStash(safeGit)
loadStatus()
}
fun createBranch(branchName: String) = managerScope.launch {
branchesManager.createBranch(safeGit, branchName)
}
fun deleteBranch(branch: Ref) = managerScope.launch {
branchesManager.deleteBranch(safeGit, branch)
}
}

View File

@ -51,6 +51,8 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
onRevCommitSelected = { commit ->
uncommitedChangesSelected = false
gitManager.loadStatus()
val parent = if (commit.parentCount == 0) {
null
} else
@ -70,7 +72,7 @@ fun RepositorySelected(gitManager: GitManager, repository: Repository) {
},
onUncommitedChangesSelected = {
uncommitedChangesSelected = true
gitManager.updateStatus()
gitManager.loadStatus()
}
)
}

View File

@ -80,7 +80,7 @@ private fun StashRow(stash: RevCommit) {
)
Text(
text = stash.name,
text = stash.shortMessage,
modifier = Modifier.weight(1f, fill = true),
maxLines = 1,
overflow = TextOverflow.Ellipsis,

View File

@ -0,0 +1,7 @@
package extensions
import org.eclipse.jgit.api.Status
fun Status.hasUntrackedChanges(): Boolean {
return this.untracked.isNotEmpty()
}

View File

@ -19,4 +19,20 @@ class BranchesManager {
_branches.value = branchList
}
suspend fun createBranch(git: Git, branchName: String) = withContext(Dispatchers.IO) {
git
.branchCreate()
.setName(branchName)
.call()
loadBranches(git)
}
suspend fun deleteBranch(git: Git, branch: Ref) = withContext(Dispatchers.IO) {
git
.branchDelete()
.setBranchNames(branch.name)
.call()
}
}

View File

@ -6,6 +6,8 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.revwalk.RevCommit
import java.text.DateFormat
import java.util.*
class StashManager {
private val _stashStatus = MutableStateFlow<StashStatus>(StashStatus.Loaded(listOf()))
@ -15,16 +17,23 @@ class StashManager {
suspend fun stash(git: Git) = withContext(Dispatchers.IO) {
git
.stashCreate()
.setIncludeUntracked(true)
.call()
loadStashList(git)
}
suspend fun popStash(git: Git) = withContext(Dispatchers.IO) {
// val firstStash = git.stashList().call().firstOrNull() ?: return@withContext
git
.stashApply()
// .setStashRef(firstStash.)
.call()
// git.stashDrop()
// .setStashRef(firstStash.)
loadStashList(git)
}

View File

@ -1,6 +1,7 @@
package git
import extensions.filePath
import extensions.hasUntrackedChanges
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
@ -20,10 +21,11 @@ class StatusManager {
get() = _hasUncommitedChanges
suspend fun loadHasUncommitedChanges(git: Git) = withContext(Dispatchers.IO) {
val hasUncommitedChanges = git
val status = git
.status()
.call()
.hasUncommittedChanges()
val hasUncommitedChanges = status.hasUncommittedChanges() || status.hasUntrackedChanges()
_hasUncommitedChanges.value = hasUncommitedChanges
}
@ -31,6 +33,8 @@ class StatusManager {
suspend fun loadStatus(git: Git) = withContext(Dispatchers.IO) {
_stageStatus.value = StageStatus.Loading
loadHasUncommitedChanges(git)
val staged = git
.diff()
.setCached(true)