11package com.jetpackduba.gitnuro.git.remote_operations
22
3+ import com.jetpackduba.gitnuro.generated.resources.Res
4+ import com.jetpackduba.gitnuro.generated.resources.pull_with_merge_automatic_stash_description
5+ import com.jetpackduba.gitnuro.git.stash.DeleteStashUseCase
6+ import com.jetpackduba.gitnuro.git.stash.SnapshotStashCreateCommand
7+ import com.jetpackduba.gitnuro.git.workspace.CheckHasUncommittedChangesUseCase
38import com.jetpackduba.gitnuro.repositories.AppSettingsRepository
49import kotlinx.coroutines.Dispatchers
510import kotlinx.coroutines.withContext
611import org.eclipse.jgit.api.Git
712import org.eclipse.jgit.lib.ConfigConstants
813import org.eclipse.jgit.lib.Repository
14+ import org.eclipse.jgit.revwalk.RevCommit
915import org.eclipse.jgit.transport.CredentialsProvider
16+ import org.jetbrains.compose.resources.getString
1017import javax.inject.Inject
1118
1219class PullBranchUseCase @Inject constructor(
20+ private val checkHasUncommittedChangesUseCase : CheckHasUncommittedChangesUseCase ,
1321 private val handleTransportUseCase : HandleTransportUseCase ,
1422 private val appSettingsRepository : AppSettingsRepository ,
1523 private val hasPullResultConflictsUseCase : HasPullResultConflictsUseCase ,
24+ private val deleteStashUseCase : DeleteStashUseCase ,
1625) {
1726 suspend operator fun invoke (git : Git , pullType : PullType ): PullHasConflicts = withContext(Dispatchers .IO ) {
1827 useBuiltinLfs(git.repository) {
@@ -22,7 +31,26 @@ class PullBranchUseCase @Inject constructor(
2231 PullType .DEFAULT -> appSettingsRepository.pullRebase
2332 }
2433
25- handleTransportUseCase(git) {
34+ val pullWithMerge = ! pullWithRebase
35+ var backupStash: RevCommit ? = null
36+
37+ if (appSettingsRepository.mergeAutoStash && pullWithMerge) {
38+ val hasUncommitedChanges = checkHasUncommittedChangesUseCase(git)
39+ if (hasUncommitedChanges) {
40+ val snapshotStashCreateCommand = SnapshotStashCreateCommand (
41+ repository = git.repository,
42+ workingDirectoryMessage = getString(
43+ Res .string.pull_with_merge_automatic_stash_description,
44+ git.repository.branch
45+ ),
46+ includeUntracked = true
47+ )
48+
49+ backupStash = snapshotStashCreateCommand.call()
50+ }
51+ }
52+
53+ val pullHasConflicts = handleTransportUseCase(git) {
2654 val pullResult = git
2755 .pull()
2856 .setTransportConfigCallback { this .handleTransport(it) }
@@ -32,6 +60,13 @@ class PullBranchUseCase @Inject constructor(
3260
3361 return @handleTransportUseCase hasPullResultConflictsUseCase(pullWithRebase, pullResult)
3462 }
63+
64+ if (! pullHasConflicts && backupStash != null ) {
65+ deleteStashUseCase(git, backupStash)
66+ }
67+
68+ pullHasConflicts
69+
3570 }
3671 }
3772}
0 commit comments