Added option to set default pull with rebase as default

Fixes #34
This commit is contained in:
Abdelilah El Aissaoui 2023-03-18 18:56:14 +01:00
parent b62684e29d
commit 3a473142ae
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
7 changed files with 82 additions and 11 deletions

View File

@ -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
}

View File

@ -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<Boolean> = _ffMergeFlow
private val _pullRebaseFlow = MutableStateFlow(pullRebase)
val pullRebaseFlow: StateFlow<Boolean> = _pullRebaseFlow
private val _commitsLimitFlow = MutableSharedFlow<Int>()
val commitsLimitFlow: SharedFlow<Int> = _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)

View File

@ -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()

View File

@ -1,13 +1,20 @@
package com.jetpackduba.gitnuro.ui.context_menu
fun pullContextMenuItems(
onPullRebase: () -> Unit,
onPullWith: () -> Unit,
onFetchAll: () -> Unit,
isPullWithRebaseDefault: Boolean,
): List<DropDownContentData> {
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",

View File

@ -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

View File

@ -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(

View File

@ -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) {