Added set the tracking branch when pushing a new branch

This commit is contained in:
Abdelilah El Aissaoui 2024-09-14 01:19:13 +02:00
parent c92ccf1f6d
commit 87399eccf4
No known key found for this signature in database
GPG Key ID: 7587FC860F594869
3 changed files with 36 additions and 14 deletions

View File

@ -9,19 +9,23 @@ import javax.inject.Inject
class SetTrackingBranchUseCase @Inject constructor() {
operator fun invoke(git: Git, ref: Ref, remoteName: String?, remoteBranch: Ref?) {
invoke(git, ref.simpleName, remoteName, remoteBranch?.simpleName)
}
operator fun invoke(git: Git, refName: String, remoteName: String?, remoteBranchName: String?) {
val repository: Repository = git.repository
val config: StoredConfig = repository.config
if (remoteName == null || remoteBranch == null) {
config.unset("branch", ref.simpleName, "remote")
config.unset("branch", ref.simpleName, "merge")
if (remoteName == null || remoteBranchName == null) {
config.unset("branch", refName, "remote")
config.unset("branch", refName, "merge")
} else {
config.setString("branch", ref.simpleName, "remote", remoteName)
config.setString("branch", refName, "remote", remoteName)
config.setString(
"branch",
ref.simpleName,
refName,
"merge",
BranchesConstants.UPSTREAM_BRANCH_CONFIG_PREFIX + remoteBranch.simpleName
BranchesConstants.UPSTREAM_BRANCH_CONFIG_PREFIX + remoteBranchName
)
}

View File

@ -12,7 +12,7 @@ class HandleTransportUseCase @Inject constructor(
private val sessionManager: GSessionManager,
private val httpCredentialsProvider: HttpCredentialsFactory,
) {
suspend operator fun invoke(git: Git?, block: suspend CredentialsHandler.() -> Unit) {
suspend operator fun <R> invoke(git: Git?, block: suspend CredentialsHandler.() -> R): R {
var cache: CredentialsCache? = null
val credentialsHandler = object : CredentialsHandler {
@ -37,8 +37,10 @@ class HandleTransportUseCase @Inject constructor(
}
}
credentialsHandler.block()
val result = credentialsHandler.block()
cache?.cacheCredentialsIfNeeded()
return result
}
}

View File

@ -1,6 +1,7 @@
package com.jetpackduba.gitnuro.git.remote_operations
import com.jetpackduba.gitnuro.git.branches.GetTrackingBranchUseCase
import com.jetpackduba.gitnuro.git.branches.SetTrackingBranchUseCase
import com.jetpackduba.gitnuro.git.branches.TrackingBranch
import com.jetpackduba.gitnuro.git.isRejected
import com.jetpackduba.gitnuro.git.statusMessage
@ -14,24 +15,37 @@ import org.eclipse.jgit.lib.ProgressMonitor
import org.eclipse.jgit.transport.RefLeaseSpec
import org.eclipse.jgit.transport.RefSpec
import javax.inject.Inject
import kotlin.math.max
class PushBranchUseCase @Inject constructor(
private val handleTransportUseCase: HandleTransportUseCase,
private val getTrackingBranchUseCase: GetTrackingBranchUseCase,
private val setTrackingBranchUseCase: SetTrackingBranchUseCase,
private val appSettingsRepository: AppSettingsRepository,
) {
// TODO This use case should also set the tracking branch to the new remote branch
suspend operator fun invoke(git: Git, force: Boolean, pushTags: Boolean) = withContext(Dispatchers.IO) {
val currentBranch = git.repository.fullBranch
val tracking = getTrackingBranchUseCase(git, git.repository.branch)
val currentBranch = git.repository.branch
val fullCurrentBranch = git.repository.fullBranch
val tracking = getTrackingBranchUseCase(git, currentBranch)
val refSpecStr = if (tracking != null) {
"$currentBranch:${Constants.R_HEADS}${tracking.branch}"
"$fullCurrentBranch:${Constants.R_HEADS}${tracking.branch}"
} else {
currentBranch
fullCurrentBranch
}
handleTransportUseCase(git) {
val remoteRefUpdate = handleTransportUseCase(git) {
push(git, tracking, refSpecStr, force, pushTags)
}
if (tracking == null && remoteRefUpdate != null) {
// [remoteRefUpdate.trackingRefUpdate.localName] should have the following format: refs/remotes/REMOTE_NAME/BRANCH_NAME
val remoteBranchPathSplit = remoteRefUpdate.trackingRefUpdate.localName.split("/")
val remoteName = remoteBranchPathSplit.getOrNull(2)
val remoteBranchName = remoteBranchPathSplit.takeLast(max(0, remoteBranchPathSplit.count() - 3)).joinToString("/")
setTrackingBranchUseCase(git, currentBranch, remoteName, remoteBranchName)
}
}
private suspend fun CredentialsHandler.push(
@ -117,5 +131,7 @@ class PushBranchUseCase @Inject constructor(
throw Exception(error.toString())
}
return@withContext pushResult.firstOrNull()?.remoteUpdates?.firstOrNull()
}
}