Checking out a remote branch create now a local branch with the same name

This commit is contained in:
Abdelilah El Aissaoui 2021-11-19 03:36:23 +01:00
parent 188a6a979f
commit 03201972e7
3 changed files with 26 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package app.extensions
import org.eclipse.jgit.lib.ObjectIdRef
import org.eclipse.jgit.lib.Ref
val Ref.simpleName: String
@ -7,5 +8,13 @@ val Ref.simpleName: String
return if (this.name.startsWith("refs/remotes/"))
name.replace("refs/remotes/", "")
else
this.name.split("/").last()
}
this.name.split("/").last() // TODO Do not take the last one, just remove the prefixes
}
val Ref.isBranch: Boolean
get() {
return this is ObjectIdRef.PeeledNonTag
}
val Ref.isTag: Boolean
get() = this is ObjectIdRef.PeeledTag

View File

@ -1,5 +1,7 @@
package app.git
import app.extensions.isBranch
import app.extensions.simpleName
import app.git.graph.GraphCommitList
import app.git.graph.GraphWalk
import kotlinx.coroutines.Dispatchers
@ -7,6 +9,7 @@ import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.CreateBranchCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.ResetCommand
import org.eclipse.jgit.lib.Constants
@ -59,10 +62,15 @@ class LogManager @Inject constructor(
}
suspend fun checkoutRef(git: Git, ref: Ref) = withContext(Dispatchers.IO) {
git
.checkout()
.setName(ref.name)
.call()
git.checkout().apply {
setName(ref.name)
if(ref.isBranch && ref.name.startsWith("refs/remotes/")) {
setCreateBranch(true)
setName(ref.simpleName)
setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
}
call()
}
}
suspend fun revertCommit(git: Git, revCommit: RevCommit) = withContext(Dispatchers.IO) {

View File

@ -28,9 +28,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import app.DialogManager
import app.extensions.md5
import app.extensions.simpleName
import app.extensions.toSmartSystemString
import app.extensions.*
import app.git.GitManager
import app.git.LogStatus
import app.git.ResetType
@ -353,14 +351,14 @@ fun CommitMessage(
verticalAlignment = Alignment.CenterVertically,
) {
refs.forEach { ref ->
if (ref is ObjectIdRef.PeeledTag) {
if (ref.isTag) {
TagChip(
ref = ref,
onCheckoutTag = {
onCheckoutRef(ref)
}
)
} else
} else if(ref.isBranch)
BranchChip(
ref = ref,
onCheckoutBranch = {