From 3a473142aecb80cc108d8f68f6a2a027adeb1cef Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Sat, 18 Mar 2023 18:56:14 +0100 Subject: [PATCH] Added option to set default pull with rebase as default Fixes #34 --- .../remote_operations/PullBranchUseCase.kt | 21 ++++++++++++++++--- .../gitnuro/preferences/AppSettings.kt | 16 ++++++++++++++ .../kotlin/com/jetpackduba/gitnuro/ui/Menu.kt | 17 ++++++++++++--- .../ui/context_menu/PullContextMenu.kt | 13 +++++++++--- .../ui/dialogs/settings/SettingsDialog.kt | 10 +++++++++ .../gitnuro/viewmodels/MenuViewModel.kt | 9 ++++++-- .../gitnuro/viewmodels/SettingsViewModel.kt | 7 +++++++ 7 files changed, 82 insertions(+), 11 deletions(-) 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 b5b6d03..c99ef74 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 @@ -1,5 +1,6 @@ package com.jetpackduba.gitnuro.git.remote_operations +import com.jetpackduba.gitnuro.preferences.AppSettings import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import org.eclipse.jgit.api.Git @@ -9,19 +10,26 @@ import javax.inject.Inject class PullBranchUseCase @Inject constructor( private val handleTransportUseCase: HandleTransportUseCase, + private val appSettings: AppSettings, ) { - suspend operator fun invoke(git: Git, rebase: Boolean) = withContext(Dispatchers.IO) { + suspend operator fun invoke(git: Git, pullType: PullType) = withContext(Dispatchers.IO) { + val pullWithRebase = when (pullType) { + PullType.REBASE -> true + PullType.MERGE -> false + PullType.DEFAULT -> appSettings.pullRebase + } + val pullResult = git .pull() .setTransportConfigCallback { handleTransportUseCase(it, git) } - .setRebase(rebase) + .setRebase(pullWithRebase) .setCredentialsProvider(CredentialsProvider.getDefault()) .call() if (!pullResult.isSuccessful) { var message = "Pull failed" - if (rebase) { + if (pullWithRebase) { message = when (pullResult.rebaseResult.status) { RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommited changes" RebaseResult.Status.CONFLICTS -> "Pull with rebase has conflicts, fix them to continue" @@ -32,4 +40,11 @@ class PullBranchUseCase @Inject constructor( throw Exception(message) } } +} + + +enum class PullType { + REBASE, + MERGE, + DEFAULT } \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/preferences/AppSettings.kt b/src/main/kotlin/com/jetpackduba/gitnuro/preferences/AppSettings.kt index 037bb74..fe055e7 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/preferences/AppSettings.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/preferences/AppSettings.kt @@ -30,6 +30,7 @@ private const val PREF_DIFF_TYPE = "diffType" private const val PREF_GIT_FF_MERGE = "gitFFMerge" +private const val PREF_GIT_PULL_REBASE = "gitPullRebase" private const val DEFAULT_COMMITS_LIMIT = 1000 private const val DEFAULT_COMMITS_LIMIT_ENABLED = true @@ -48,6 +49,9 @@ class AppSettings @Inject constructor() { private val _ffMergeFlow = MutableStateFlow(ffMerge) val ffMergeFlow: StateFlow = _ffMergeFlow + private val _pullRebaseFlow = MutableStateFlow(pullRebase) + val pullRebaseFlow: StateFlow = _pullRebaseFlow + private val _commitsLimitFlow = MutableSharedFlow() val commitsLimitFlow: SharedFlow = _commitsLimitFlow @@ -117,6 +121,18 @@ class AppSettings @Inject constructor() { _ffMergeFlow.value = value } + /** + * Property that decides if the merge should fast-forward when possible + */ + var pullRebase: Boolean + get() { + return preferences.getBoolean(PREF_GIT_PULL_REBASE, false) + } + set(value) { + preferences.putBoolean(PREF_GIT_PULL_REBASE, value) + _pullRebaseFlow.value = value + } + val commitsLimit: Int get() { return preferences.getInt(PREF_COMMITS_LIMIT, DEFAULT_COMMITS_LIMIT) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/Menu.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/Menu.kt index 308ccdb..2b97917 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/Menu.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/Menu.kt @@ -22,6 +22,7 @@ import com.jetpackduba.gitnuro.AppIcons import com.jetpackduba.gitnuro.extensions.handMouseClickable import com.jetpackduba.gitnuro.extensions.handOnHover import com.jetpackduba.gitnuro.extensions.ignoreKeyEvents +import com.jetpackduba.gitnuro.git.remote_operations.PullType import com.jetpackduba.gitnuro.ui.components.gitnuroViewModel import com.jetpackduba.gitnuro.ui.context_menu.* import com.jetpackduba.gitnuro.viewmodels.MenuViewModel @@ -37,6 +38,8 @@ fun Menu( onQuickActions: () -> Unit, onShowSettingsDialog: () -> Unit, ) { + val isPullWithRebaseDefault by menuViewModel.isPullWithRebaseDefault.collectAsState() + Row( modifier = modifier, horizontalArrangement = Arrangement.Center, @@ -56,10 +59,18 @@ fun Menu( modifier = Modifier.padding(end = 4.dp), title = "Pull", icon = painterResource(AppIcons.DOWNLOAD), - onClick = { menuViewModel.pull() }, + onClick = { menuViewModel.pull(PullType.DEFAULT) }, extendedListItems = pullContextMenuItems( - onPullRebase = { - menuViewModel.pull(true) + isPullWithRebaseDefault = isPullWithRebaseDefault, + onPullWith = { + // Do the reverse of the default + val pullType = if (isPullWithRebaseDefault) { + PullType.MERGE + } else { + PullType.REBASE + } + + menuViewModel.pull(pullType = pullType) }, onFetchAll = { menuViewModel.fetchAll() diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/PullContextMenu.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/PullContextMenu.kt index 98ce1df..4bfbf28 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/PullContextMenu.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/PullContextMenu.kt @@ -1,13 +1,20 @@ package com.jetpackduba.gitnuro.ui.context_menu fun pullContextMenuItems( - onPullRebase: () -> Unit, + onPullWith: () -> Unit, onFetchAll: () -> Unit, + isPullWithRebaseDefault: Boolean, ): List { + val pullWithText = if (isPullWithRebaseDefault) { + "Pull with merge" + } else { + "Pull with rebase" + } + return mutableListOf( DropDownContentData( - label = "Pull with rebase", - onClick = onPullRebase, + label = pullWithText, + onClick = onPullWith, ), DropDownContentData( label = "Fetch all", diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt index 9a6ed3f..9e5284b 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/dialogs/settings/SettingsDialog.kt @@ -116,6 +116,7 @@ fun SettingsDialog( fun GitSettings(settingsViewModel: SettingsViewModel) { val commitsLimitEnabled by settingsViewModel.commitsLimitEnabledFlow.collectAsState() val ffMerge by settingsViewModel.ffMergeFlow.collectAsState() + val pullRebase by settingsViewModel.pullRebaseFlow.collectAsState() var commitsLimit by remember { mutableStateOf(settingsViewModel.commitsLimit) } SettingToggle( @@ -146,6 +147,15 @@ fun GitSettings(settingsViewModel: SettingsViewModel) { settingsViewModel.ffMerge = value } ) + + SettingToggle( + title = "Pull with rebase as default", + subtitle = "Rebase changes instead of merging when pulling", + value = pullRebase, + onValueChanged = { value -> + settingsViewModel.pullRebase = value + } + ) } @Composable diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt index 7f2a9c3..2d377ea 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt @@ -4,10 +4,12 @@ import com.jetpackduba.gitnuro.git.RefreshType import com.jetpackduba.gitnuro.git.TabState import com.jetpackduba.gitnuro.git.remote_operations.FetchAllBranchesUseCase 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.preferences.AppSettings import javax.inject.Inject class MenuViewModel @Inject constructor( @@ -18,12 +20,15 @@ class MenuViewModel @Inject constructor( private val popLastStashUseCase: PopLastStashUseCase, private val stashChangesUseCase: StashChangesUseCase, private val stageUntrackedFileUseCase: StageUntrackedFileUseCase, + private val settings: AppSettings, ) { - fun pull(rebase: Boolean = false) = tabState.safeProcessing( + val isPullWithRebaseDefault = settings.pullRebaseFlow + + fun pull(pullType: PullType) = tabState.safeProcessing( refreshType = RefreshType.ALL_DATA, refreshEvenIfCrashes = true, ) { git -> - pullBranchUseCase(git, rebase) + pullBranchUseCase(git, pullType) } fun fetchAll() = tabState.safeProcessing( diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt index 3a73c43..06a6498 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SettingsViewModel.kt @@ -18,6 +18,7 @@ class SettingsViewModel @Inject constructor( val themeState = appSettings.themeState val ffMergeFlow = appSettings.ffMergeFlow + val pullRebaseFlow = appSettings.pullRebaseFlow val commitsLimitEnabledFlow = appSettings.commitsLimitEnabledFlow var scaleUi: Float @@ -38,6 +39,12 @@ class SettingsViewModel @Inject constructor( appSettings.ffMerge = value } + var pullRebase: Boolean + get() = appSettings.pullRebase + set(value) { + appSettings.pullRebase = value + } + var theme: Theme get() = appSettings.theme set(value) {