Skip to content

Commit e5899d0

Browse files
committed
Fixed pull result not being shown properly
Also fixed pull from specific branch using merge by default instead of the user's configuration
1 parent 169ed5a commit e5899d0

File tree

6 files changed

+66
-69
lines changed

6 files changed

+66
-69
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.jetpackduba.gitnuro.git.remote_operations
2+
3+
import org.eclipse.jgit.api.MergeResult
4+
import org.eclipse.jgit.api.PullResult
5+
import org.eclipse.jgit.api.RebaseResult
6+
import javax.inject.Inject
7+
8+
typealias PullHasConflicts = Boolean
9+
10+
class HasPullResultConflictsUseCase @Inject constructor() {
11+
operator fun invoke(isRebase: Boolean, pullResult: PullResult): PullHasConflicts {
12+
if (!pullResult.isSuccessful) {
13+
if (
14+
pullResult.mergeResult?.mergeStatus == MergeResult.MergeStatus.CONFLICTING ||
15+
pullResult.rebaseResult?.status == RebaseResult.Status.CONFLICTS ||
16+
pullResult.rebaseResult?.status == RebaseResult.Status.STOPPED
17+
) {
18+
return true
19+
}
20+
21+
if (isRebase) {
22+
val message = when (pullResult.rebaseResult.status) {
23+
RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommitted changes"
24+
else -> "Pull failed"
25+
}
26+
27+
throw Exception(message)
28+
}
29+
}
30+
31+
return false
32+
}
33+
34+
}

src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullBranchUseCase.kt

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import com.jetpackduba.gitnuro.repositories.AppSettingsRepository
44
import kotlinx.coroutines.Dispatchers
55
import kotlinx.coroutines.withContext
66
import org.eclipse.jgit.api.Git
7-
import org.eclipse.jgit.api.RebaseResult
87
import org.eclipse.jgit.transport.CredentialsProvider
98
import javax.inject.Inject
109

1110
class PullBranchUseCase @Inject constructor(
1211
private val handleTransportUseCase: HandleTransportUseCase,
1312
private val appSettingsRepository: AppSettingsRepository,
13+
private val hasPullResultConflictsUseCase: HasPullResultConflictsUseCase,
1414
) {
15-
suspend operator fun invoke(git: Git, pullType: PullType) = withContext(Dispatchers.IO) {
15+
suspend operator fun invoke(git: Git, pullType: PullType): PullHasConflicts = withContext(Dispatchers.IO) {
1616
val pullWithRebase = when (pullType) {
1717
PullType.REBASE -> true
1818
PullType.MERGE -> false
@@ -27,19 +27,7 @@ class PullBranchUseCase @Inject constructor(
2727
.setCredentialsProvider(CredentialsProvider.getDefault())
2828
.call()
2929

30-
if (!pullResult.isSuccessful) {
31-
var message = "Pull failed"
32-
33-
if (pullWithRebase) {
34-
message = when (pullResult.rebaseResult.status) {
35-
RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommitted changes"
36-
RebaseResult.Status.CONFLICTS -> "Pull with rebase has conflicts, fix them to continue"
37-
else -> message
38-
}
39-
}
40-
41-
throw Exception(message)
42-
}
30+
return@handleTransportUseCase hasPullResultConflictsUseCase(pullWithRebase, pullResult)
4331
}
4432
}
4533
}

src/main/kotlin/com/jetpackduba/gitnuro/git/remote_operations/PullFromSpecificBranchUseCase.kt

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,34 @@ package com.jetpackduba.gitnuro.git.remote_operations
22

33
import com.jetpackduba.gitnuro.extensions.remoteName
44
import com.jetpackduba.gitnuro.extensions.simpleName
5+
import com.jetpackduba.gitnuro.repositories.AppSettingsRepository
56
import kotlinx.coroutines.Dispatchers
67
import kotlinx.coroutines.withContext
78
import org.eclipse.jgit.api.Git
8-
import org.eclipse.jgit.api.RebaseResult
99
import org.eclipse.jgit.lib.Ref
1010
import org.eclipse.jgit.transport.CredentialsProvider
1111
import javax.inject.Inject
1212

1313
class PullFromSpecificBranchUseCase @Inject constructor(
1414
private val handleTransportUseCase: HandleTransportUseCase,
15+
private val hasPullResultConflictsUseCase: HasPullResultConflictsUseCase,
16+
private val appSettingsRepository: AppSettingsRepository,
1517
) {
16-
suspend operator fun invoke(git: Git, rebase: Boolean, remoteBranch: Ref) = withContext(Dispatchers.IO) {
17-
handleTransportUseCase(git) {
18-
val pullResult = git
19-
.pull()
20-
.setTransportConfigCallback { handleTransport(it) }
21-
.setRemote(remoteBranch.remoteName)
22-
.setRemoteBranchName(remoteBranch.simpleName)
23-
.setRebase(rebase)
24-
.setCredentialsProvider(CredentialsProvider.getDefault())
25-
.call()
18+
suspend operator fun invoke(git: Git, remoteBranch: Ref): PullHasConflicts =
19+
withContext(Dispatchers.IO) {
20+
val pullWithRebase = appSettingsRepository.pullRebase
2621

27-
if (!pullResult.isSuccessful) {
28-
var message =
29-
"Pull failed" // TODO Remove messages from here and pass the result to a custom exception type
22+
handleTransportUseCase(git) {
23+
val pullResult = git
24+
.pull()
25+
.setTransportConfigCallback { handleTransport(it) }
26+
.setRemote(remoteBranch.remoteName)
27+
.setRemoteBranchName(remoteBranch.simpleName)
28+
.setRebase(pullWithRebase)
29+
.setCredentialsProvider(CredentialsProvider.getDefault())
30+
.call()
3031

31-
if (rebase) {
32-
message = when (pullResult.rebaseResult.status) {
33-
RebaseResult.Status.UNCOMMITTED_CHANGES -> "The pull with rebase has failed because you have got uncommitted changes"
34-
RebaseResult.Status.CONFLICTS -> "Pull with rebase has conflicts, fix them to continue"
35-
else -> message
36-
}
37-
}
38-
39-
throw Exception(message)
32+
return@handleTransportUseCase hasPullResultConflictsUseCase(pullWithRebase, pullResult)
4033
}
4134
}
42-
}
4335
}

src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/GlobalMenuActionsViewModel.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ import com.jetpackduba.gitnuro.git.remote_operations.PullType
99
import com.jetpackduba.gitnuro.git.remote_operations.PushBranchUseCase
1010
import com.jetpackduba.gitnuro.git.stash.PopLastStashUseCase
1111
import com.jetpackduba.gitnuro.git.stash.StashChangesUseCase
12-
import com.jetpackduba.gitnuro.managers.AppStateManager
1312
import com.jetpackduba.gitnuro.models.errorNotification
1413
import com.jetpackduba.gitnuro.models.positiveNotification
15-
import com.jetpackduba.gitnuro.repositories.AppSettingsRepository
14+
import com.jetpackduba.gitnuro.models.warningNotification
1615
import com.jetpackduba.gitnuro.terminal.OpenRepositoryInTerminalUseCase
1716
import kotlinx.coroutines.Job
1817
import javax.inject.Inject
@@ -26,16 +25,14 @@ interface IGlobalMenuActionsViewModel {
2625
fun openTerminal(): Job
2726
}
2827

29-
class GlobalMenuActionsViewModel @Inject constructor(
28+
class GlobalMenuActionsViewModel @Inject constructor(
3029
private val tabState: TabState,
3130
private val pullBranchUseCase: PullBranchUseCase,
3231
private val pushBranchUseCase: PushBranchUseCase,
3332
private val fetchAllRemotesUseCase: FetchAllRemotesUseCase,
3433
private val popLastStashUseCase: PopLastStashUseCase,
3534
private val stashChangesUseCase: StashChangesUseCase,
3635
private val openRepositoryInTerminalUseCase: OpenRepositoryInTerminalUseCase,
37-
settings: AppSettingsRepository,
38-
appStateManager: AppStateManager,
3936
) : IGlobalMenuActionsViewModel {
4037
override fun pull(pullType: PullType) = tabState.safeProcessing(
4138
refreshType = RefreshType.ALL_DATA,
@@ -44,9 +41,11 @@ class GlobalMenuActionsViewModel @Inject constructor(
4441
refreshEvenIfCrashes = true,
4542
taskType = TaskType.PULL,
4643
) { git ->
47-
pullBranchUseCase(git, pullType)
48-
49-
positiveNotification("Pull completed")
44+
if (pullBranchUseCase(git, pullType)) {
45+
warningNotification("Pull produced conflicts, fix them to continue")
46+
} else {
47+
positiveNotification("Pull completed")
48+
}
5049
}
5150

5251
override fun fetchAll() = tabState.safeProcessing(

src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/MenuViewModel.kt

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
package com.jetpackduba.gitnuro.viewmodels
22

3-
import com.jetpackduba.gitnuro.TaskType
4-
import com.jetpackduba.gitnuro.git.RefreshType
5-
import com.jetpackduba.gitnuro.git.TabState
6-
import com.jetpackduba.gitnuro.git.remote_operations.FetchAllRemotesUseCase
7-
import com.jetpackduba.gitnuro.git.remote_operations.PullBranchUseCase
8-
import com.jetpackduba.gitnuro.git.remote_operations.PullType
9-
import com.jetpackduba.gitnuro.git.remote_operations.PushBranchUseCase
10-
import com.jetpackduba.gitnuro.git.stash.PopLastStashUseCase
11-
import com.jetpackduba.gitnuro.git.stash.StashChangesUseCase
12-
import com.jetpackduba.gitnuro.git.workspace.StageUntrackedFileUseCase
133
import com.jetpackduba.gitnuro.managers.AppStateManager
14-
import com.jetpackduba.gitnuro.models.errorNotification
15-
import com.jetpackduba.gitnuro.models.positiveNotification
16-
import com.jetpackduba.gitnuro.models.warningNotification
174
import com.jetpackduba.gitnuro.repositories.AppSettingsRepository
18-
import com.jetpackduba.gitnuro.terminal.OpenRepositoryInTerminalUseCase
195
import javax.inject.Inject
206

217
class MenuViewModel @Inject constructor(
22-
private val tabState: TabState,
238
private val globalMenuActionsViewModel: GlobalMenuActionsViewModel,
249
settings: AppSettingsRepository,
2510
appStateManager: AppStateManager,

src/main/kotlin/com/jetpackduba/gitnuro/viewmodels/SharedRemotesViewModel.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.jetpackduba.gitnuro.git.remote_operations.DeleteRemoteBranchUseCase
99
import com.jetpackduba.gitnuro.git.remote_operations.PullFromSpecificBranchUseCase
1010
import com.jetpackduba.gitnuro.git.remote_operations.PushToSpecificBranchUseCase
1111
import com.jetpackduba.gitnuro.models.positiveNotification
12+
import com.jetpackduba.gitnuro.models.warningNotification
1213
import kotlinx.coroutines.Job
1314
import org.eclipse.jgit.lib.Ref
1415
import javax.inject.Inject
@@ -69,12 +70,10 @@ class SharedRemotesViewModel @Inject constructor(
6970
subtitle = "Pulling changes from ${branch.simpleName} to the current branch",
7071
taskType = TaskType.PULL_FROM_BRANCH,
7172
) { git ->
72-
pullFromSpecificBranchUseCase(
73-
git = git,
74-
rebase = false,
75-
remoteBranch = branch,
76-
)
77-
78-
positiveNotification("Pulled from \"${branch.simpleName}\"")
73+
if (pullFromSpecificBranchUseCase(git = git, remoteBranch = branch)) {
74+
warningNotification("Pull produced conflicts, fix them to continue")
75+
} else {
76+
positiveNotification("Pulled from \"${branch.simpleName}\"")
77+
}
7978
}
8079
}

0 commit comments

Comments
 (0)