From e5899d02d670e493b1d30124bf5841024580e068 Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Thu, 19 Sep 2024 00:29:53 +0200 Subject: [PATCH] Fixed pull result not being shown properly Also fixed pull from specific branch using merge by default instead of the user's configuration --- .../HasPullResultConflictsUseCase.kt | 34 ++++++++++++++++ .../remote_operations/PullBranchUseCase.kt | 18 ++------- .../PullFromSpecificBranchUseCase.kt | 40 ++++++++----------- .../viewmodels/GlobalMenuActionsViewModel.kt | 15 ++++--- .../gitnuro/viewmodels/MenuViewModel.kt | 15 ------- .../viewmodels/SharedRemotesViewModel.kt | 13 +++--- 6 files changed, 66 insertions(+), 69 deletions(-) create mode 100644 src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/HasPullResultConflictsUseCase.kt diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/HasPullResultConflictsUseCase.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/HasPullResultConflictsUseCase.kt new file mode 100644 index 0000000..3f58755 --- /dev/null +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/HasPullResultConflictsUseCase.kt @@ -0,0 +1,34 @@ +package com.jetpackduba.gitnuro.git.remote_operations + +import org.eclipse.jgit.api.MergeResult +import org.eclipse.jgit.api.PullResult +import org.eclipse.jgit.api.RebaseResult +import javax.inject.Inject + +typealias PullHasConflicts = Boolean + +class HasPullResultConflictsUseCase @Inject constructor() { + operator fun invoke(isRebase: Boolean, pullResult: PullResult): PullHasConflicts { + if (!pullResult.isSuccessful) { + if ( + pullResult.mergeResult?.mergeStatus == MergeResult.MergeStatus.CONFLICTING || + pullResult.rebaseResult?.status == RebaseResult.Status.CONFLICTS || + pullResult.rebaseResult?.status == RebaseResult.Status.STOPPED + ) { + return true + } + + if (isRebase) { + val message = when (pullResult.rebaseResult.status) { + RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommitted changes" + else -> "Pull failed" + } + + throw Exception(message) + } + } + + return false + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullBranchUseCase.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullBranchUseCase.kt index eacac9b..3f308e1 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullBranchUseCase.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullBranchUseCase.kt @@ -4,15 +4,15 @@ import com.jetpackduba.gitnuro.repositories.AppSettingsRepository import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git -import org.eclipse.jgit.api.RebaseResult import org.eclipse.jgit.transport.CredentialsProvider import javax.inject.Inject class PullBranchUseCase @Inject constructor( private val handleTransportUseCase: HandleTransportUseCase, private val appSettingsRepository: AppSettingsRepository, + private val hasPullResultConflictsUseCase: HasPullResultConflictsUseCase, ) { - suspend operator fun invoke(git: Git, pullType: PullType) = withContext(Dispatchers.IO) { + suspend operator fun invoke(git: Git, pullType: PullType): PullHasConflicts = withContext(Dispatchers.IO) { val pullWithRebase = when (pullType) { PullType.REBASE -> true PullType.MERGE -> false @@ -27,19 +27,7 @@ class PullBranchUseCase @Inject constructor( .setCredentialsProvider(CredentialsProvider.getDefault()) .call() - if (!pullResult.isSuccessful) { - var message = "Pull failed" - - if (pullWithRebase) { - message = when (pullResult.rebaseResult.status) { - RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommitted changes" - RebaseResult.Status.CONFLICTS -> "Pull with rebase has conflicts, fix them to continue" - else -> message - } - } - - throw Exception(message) - } + return@handleTransportUseCase hasPullResultConflictsUseCase(pullWithRebase, pullResult) } } } diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullFromSpecificBranchUseCase.kt b/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullFromSpecificBranchUseCase.kt index 52a71ba..93a9e42 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullFromSpecificBranchUseCase.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullFromSpecificBranchUseCase.kt @@ -2,42 +2,34 @@ package com.jetpackduba.gitnuro.git.remote_operations import com.jetpackduba.gitnuro.extensions.remoteName import com.jetpackduba.gitnuro.extensions.simpleName +import com.jetpackduba.gitnuro.repositories.AppSettingsRepository import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git -import org.eclipse.jgit.api.RebaseResult import org.eclipse.jgit.lib.Ref import org.eclipse.jgit.transport.CredentialsProvider import javax.inject.Inject class PullFromSpecificBranchUseCase @Inject constructor( private val handleTransportUseCase: HandleTransportUseCase, + private val hasPullResultConflictsUseCase: HasPullResultConflictsUseCase, + private val appSettingsRepository: AppSettingsRepository, ) { - suspend operator fun invoke(git: Git, rebase: Boolean, remoteBranch: Ref) = withContext(Dispatchers.IO) { - handleTransportUseCase(git) { - val pullResult = git - .pull() - .setTransportConfigCallback { handleTransport(it) } - .setRemote(remoteBranch.remoteName) - .setRemoteBranchName(remoteBranch.simpleName) - .setRebase(rebase) - .setCredentialsProvider(CredentialsProvider.getDefault()) - .call() + suspend operator fun invoke(git: Git, remoteBranch: Ref): PullHasConflicts = + withContext(Dispatchers.IO) { + val pullWithRebase = appSettingsRepository.pullRebase - if (!pullResult.isSuccessful) { - var message = - "Pull failed" // TODO Remove messages from here and pass the result to a custom exception type + handleTransportUseCase(git) { + val pullResult = git + .pull() + .setTransportConfigCallback { handleTransport(it) } + .setRemote(remoteBranch.remoteName) + .setRemoteBranchName(remoteBranch.simpleName) + .setRebase(pullWithRebase) + .setCredentialsProvider(CredentialsProvider.getDefault()) + .call() - if (rebase) { - message = when (pullResult.rebaseResult.status) { - RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommitted changes" - RebaseResult.Status.CONFLICTS -> "Pull with rebase has conflicts, fix them to continue" - else -> message - } - } - - throw Exception(message) + return@handleTransportUseCase hasPullResultConflictsUseCase(pullWithRebase, pullResult) } } - } } \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/GlobalMenuActionsViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/GlobalMenuActionsViewModel.kt index 95b856d..2f99cce 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/GlobalMenuActionsViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/GlobalMenuActionsViewModel.kt @@ -9,10 +9,9 @@ import com.jetpackduba.gitnuro.git.remote_operations.PullType import com.jetpackduba.gitnuro.git.remote_operations.PushBranchUseCase import com.jetpackduba.gitnuro.git.stash.PopLastStashUseCase import com.jetpackduba.gitnuro.git.stash.StashChangesUseCase -import com.jetpackduba.gitnuro.managers.AppStateManager import com.jetpackduba.gitnuro.models.errorNotification import com.jetpackduba.gitnuro.models.positiveNotification -import com.jetpackduba.gitnuro.repositories.AppSettingsRepository +import com.jetpackduba.gitnuro.models.warningNotification import com.jetpackduba.gitnuro.terminal.OpenRepositoryInTerminalUseCase import kotlinx.coroutines.Job import javax.inject.Inject @@ -26,7 +25,7 @@ interface IGlobalMenuActionsViewModel { fun openTerminal(): Job } -class GlobalMenuActionsViewModel @Inject constructor( +class GlobalMenuActionsViewModel @Inject constructor( private val tabState: TabState, private val pullBranchUseCase: PullBranchUseCase, private val pushBranchUseCase: PushBranchUseCase, @@ -34,8 +33,6 @@ class GlobalMenuActionsViewModel @Inject constructor( private val popLastStashUseCase: PopLastStashUseCase, private val stashChangesUseCase: StashChangesUseCase, private val openRepositoryInTerminalUseCase: OpenRepositoryInTerminalUseCase, - settings: AppSettingsRepository, - appStateManager: AppStateManager, ) : IGlobalMenuActionsViewModel { override fun pull(pullType: PullType) = tabState.safeProcessing( refreshType = RefreshType.ALL_DATA, @@ -44,9 +41,11 @@ class GlobalMenuActionsViewModel @Inject constructor( refreshEvenIfCrashes = true, taskType = TaskType.PULL, ) { git -> - pullBranchUseCase(git, pullType) - - positiveNotification("Pull completed") + if (pullBranchUseCase(git, pullType)) { + warningNotification("Pull produced conflicts, fix them to continue") + } else { + positiveNotification("Pull completed") + } } override fun fetchAll() = tabState.safeProcessing( diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt index dfb9681..adb2ba2 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt @@ -1,25 +1,10 @@ package com.jetpackduba.gitnuro.viewmodels -import com.jetpackduba.gitnuro.TaskType -import com.jetpackduba.gitnuro.git.RefreshType -import com.jetpackduba.gitnuro.git.TabState -import com.jetpackduba.gitnuro.git.remote_operations.FetchAllRemotesUseCase -import com.jetpackduba.gitnuro.git.remote_operations.PullBranchUseCase -import com.jetpackduba.gitnuro.git.remote_operations.PullType -import com.jetpackduba.gitnuro.git.remote_operations.PushBranchUseCase -import com.jetpackduba.gitnuro.git.stash.PopLastStashUseCase -import com.jetpackduba.gitnuro.git.stash.StashChangesUseCase -import com.jetpackduba.gitnuro.git.workspace.StageUntrackedFileUseCase import com.jetpackduba.gitnuro.managers.AppStateManager -import com.jetpackduba.gitnuro.models.errorNotification -import com.jetpackduba.gitnuro.models.positiveNotification -import com.jetpackduba.gitnuro.models.warningNotification import com.jetpackduba.gitnuro.repositories.AppSettingsRepository -import com.jetpackduba.gitnuro.terminal.OpenRepositoryInTerminalUseCase import javax.inject.Inject class MenuViewModel @Inject constructor( - private val tabState: TabState, private val globalMenuActionsViewModel: GlobalMenuActionsViewModel, settings: AppSettingsRepository, appStateManager: AppStateManager, diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SharedRemotesViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SharedRemotesViewModel.kt index acddb53..f524239 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SharedRemotesViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SharedRemotesViewModel.kt @@ -9,6 +9,7 @@ import com.jetpackduba.gitnuro.git.remote_operations.DeleteRemoteBranchUseCase import com.jetpackduba.gitnuro.git.remote_operations.PullFromSpecificBranchUseCase import com.jetpackduba.gitnuro.git.remote_operations.PushToSpecificBranchUseCase import com.jetpackduba.gitnuro.models.positiveNotification +import com.jetpackduba.gitnuro.models.warningNotification import kotlinx.coroutines.Job import org.eclipse.jgit.lib.Ref import javax.inject.Inject @@ -69,12 +70,10 @@ class SharedRemotesViewModel @Inject constructor( subtitle = "Pulling changes from ${branch.simpleName} to the current branch", taskType = TaskType.PULL_FROM_BRANCH, ) { git -> - pullFromSpecificBranchUseCase( - git = git, - rebase = false, - remoteBranch = branch, - ) - - positiveNotification("Pulled from \"${branch.simpleName}\"") + if (pullFromSpecificBranchUseCase(git = git, remoteBranch = branch)) { + warningNotification("Pull produced conflicts, fix them to continue") + } else { + positiveNotification("Pulled from \"${branch.simpleName}\"") + } } }