diff --git a/src/main/kotlin/app/git/BranchesManager.kt b/src/main/kotlin/app/git/BranchesManager.kt index d730995..039fa33 100644 --- a/src/main/kotlin/app/git/BranchesManager.kt +++ b/src/main/kotlin/app/git/BranchesManager.kt @@ -20,6 +20,9 @@ class BranchesManager @Inject constructor() { val currentBranch: StateFlow 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 diff --git a/src/main/kotlin/app/git/LogManager.kt b/src/main/kotlin/app/git/LogManager.kt index 3c8cc87..e2d540b 100644 --- a/src/main/kotlin/app/git/LogManager.kt +++ b/src/main/kotlin/app/git/LogManager.kt @@ -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 } diff --git a/src/main/kotlin/app/git/StatusManager.kt b/src/main/kotlin/app/git/StatusManager.kt index a754a19..9853833 100644 --- a/src/main/kotlin/app/git/StatusManager.kt +++ b/src/main/kotlin/app/git/StatusManager.kt @@ -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.Loaded(listOf(), listOf())) val stageStatus: StateFlow @@ -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()