Fixed crashes when opening an empty repository

This commit is contained in:
Abdelilah El Aissaoui 2021-12-18 02:23:39 +01:00
parent 6c550960a1
commit 1943bd6906
3 changed files with 34 additions and 20 deletions

View File

@ -20,6 +20,9 @@ class BranchesManager @Inject constructor() {
val currentBranch: StateFlow<String>
get() = _currentBranch
/**
* Returns the current branch in [Ref]. If the repository is new, the current branch will be null.
*/
suspend fun currentBranchRef(git: Git): Ref? {
val branchList = getBranches(git)
val branchName = git

View File

@ -30,26 +30,30 @@ class LogManager @Inject constructor(
suspend fun loadLog(git: Git) = withContext(Dispatchers.IO) {
_logStatus.value = LogStatus.Loading
val logList = git.log().setMaxCount(2).call().toList()
val currentBranch = branchesManager.currentBranchRef(git)
val commitList = GraphCommitList()
val walk = GraphWalk(git.repository)
walk.use {
walk.markStartAllRefs(Constants.R_HEADS)
walk.markStartAllRefs(Constants.R_REMOTES)
walk.markStartAllRefs(Constants.R_TAGS)
if(currentBranch != null) { // Current branch is null when there is no log (new repo)
val logList = git.log().setMaxCount(2).call().toList()
if (statusManager.checkHasUncommitedChanges(git))
commitList.addUncommitedChangesGraphCommit(logList.first())
val walk = GraphWalk(git.repository)
walk.use {
walk.markStartAllRefs(Constants.R_HEADS)
walk.markStartAllRefs(Constants.R_REMOTES)
walk.markStartAllRefs(Constants.R_TAGS)
if (statusManager.checkHasUncommitedChanges(git))
commitList.addUncommitedChangesGraphCommit(logList.first())
commitList.source(walk)
commitList.fillTo(1000) // TODO: Limited commits to show to 1000, add a setting to let the user adjust this
}
ensureActive()
commitList.source(walk)
commitList.fillTo(1000) // TODO: Limited commits to show to 1000, add a setting to let the user adjust this
}
ensureActive()
val loadedStatus = LogStatus.Loaded(commitList, branchesManager.currentBranchRef(git))
val loadedStatus = LogStatus.Loaded(commitList, currentBranch)
_logStatus.value = loadedStatus
}

View File

@ -9,9 +9,13 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.diff.DiffEntry
import org.eclipse.jgit.dircache.DirCacheIterator
import org.eclipse.jgit.treewalk.EmptyTreeIterator
import javax.inject.Inject
class StatusManager @Inject constructor() {
class StatusManager @Inject constructor(
private val branchesManager: BranchesManager,
) {
private val _stageStatus = MutableStateFlow<StageStatus>(StageStatus.Loaded(listOf(), listOf()))
val stageStatus: StateFlow<StageStatus>
@ -39,11 +43,14 @@ class StatusManager @Inject constructor() {
try {
loadHasUncommitedChanges(git)
val currentBranch = branchesManager.currentBranchRef(git)
val staged = git
.diff()
.setCached(true)
.call()
val staged = git.diff().apply {
if(currentBranch == null)
setOldTree(EmptyTreeIterator()) // Required if the repository is empty
setCached(true)
}.call()
ensureActive()