From f53c3060995ce52689befebffd4536079a47dd58 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 28 Oct 2023 20:16:07 +0200 Subject: [PATCH] Greatly improved performance of "stage all" in huge workspaces. This specially noticable in repositories that contain binary blobs or huge files count. Fixes #129 --- .../gitnuro/git/workspace/StageAllUseCase.kt | 46 +++++++++++++++---- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/StageAllUseCase.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/StageAllUseCase.kt index c85f3a4..0b1e738 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/StageAllUseCase.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/workspace/StageAllUseCase.kt @@ -5,17 +5,45 @@ import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git import javax.inject.Inject -class StageAllUseCase @Inject constructor() { + +class StageAllUseCase @Inject constructor( + private val getStatusUseCase: GetStatusUseCase, + private val getUnstagedUseCase: GetUnstagedUseCase, +) { suspend operator fun invoke(git: Git): Unit = withContext(Dispatchers.IO) { - git + val status = getStatusUseCase(git) + val unstaged = getUnstagedUseCase(status) + + addAllExceptNew(git, unstaged.filter { it.statusType != StatusType.ADDED }) + addNewFiles(git, unstaged.filter { it.statusType == StatusType.ADDED }) + } + + /** + * The setUpdate flag of the addCommand adds deleted files but not newly added when active + */ + private fun addAllExceptNew(git: Git, allExceptNew: List) { + val addCommand = git .add() - .addFilepattern(".") - .setUpdate(true) // Modified and deleted files - .call() - git + + for (entry in allExceptNew) { + addCommand.addFilepattern(entry.filePath) + } + + addCommand.setUpdate(true) + + addCommand.call() + } + + private fun addNewFiles(git: Git, newFiles: List) { + val addCommand = git .add() - .addFilepattern(".") - .setUpdate(false) // For newly added files - .call() + + for (path in newFiles) { + addCommand.addFilepattern(path.filePath) + } + + addCommand.setUpdate(false) + + addCommand.call() } } \ No newline at end of file