Improved display of branches' names in branch context menu

This commit is contained in:
Abdelilah El Aissaoui 2022-02-24 23:07:28 +01:00
parent dc80cadc1b
commit 41ff6a57b8
5 changed files with 41 additions and 30 deletions

View File

@ -60,6 +60,11 @@ val Ref.isBranch: Boolean
return this is ObjectIdRef.PeeledNonTag return this is ObjectIdRef.PeeledNonTag
} }
val Ref.isHead: Boolean
get() {
return this.name == Constants.HEAD
}
val Ref.isTag: Boolean val Ref.isTag: Boolean
get() = this.name.startsWith(Constants.R_TAGS) get() = this.name.startsWith(Constants.R_TAGS)

View File

@ -25,7 +25,9 @@ fun Branches(
branchesViewModel: BranchesViewModel, branchesViewModel: BranchesViewModel,
) { ) {
val branches by branchesViewModel.branches.collectAsState() val branches by branchesViewModel.branches.collectAsState()
val currentBranch by branchesViewModel.currentBranch.collectAsState() val currentBranchState = branchesViewModel.currentBranch.collectAsState()
val currentBranch = currentBranchState.value
val (mergeBranch, setMergeBranch) = remember { mutableStateOf<Ref?>(null) } val (mergeBranch, setMergeBranch) = remember { mutableStateOf<Ref?>(null) }
val (rebaseBranch, setRebaseBranch) = remember { mutableStateOf<Ref?>(null) } val (rebaseBranch, setRebaseBranch) = remember { mutableStateOf<Ref?>(null) }
@ -36,8 +38,8 @@ fun Branches(
itemContent = { branch -> itemContent = { branch ->
BranchLineEntry( BranchLineEntry(
branch = branch, branch = branch,
currentBranchName = currentBranch, currentBranch = currentBranch,
isCurrentBranch = currentBranch == branch.name, isCurrentBranch = currentBranch?.name == branch.name,
onBranchClicked = { branchesViewModel.selectBranch(branch) }, onBranchClicked = { branchesViewModel.selectBranch(branch) },
onCheckoutBranch = { branchesViewModel.checkoutRef(branch) }, onCheckoutBranch = { branchesViewModel.checkoutRef(branch) },
onMergeBranch = { setMergeBranch(branch) }, onMergeBranch = { setMergeBranch(branch) },
@ -49,18 +51,18 @@ fun Branches(
} }
) )
if (mergeBranch != null) { if (mergeBranch != null && currentBranch != null) {
MergeDialog( MergeDialog(
currentBranch, currentBranchName = currentBranch.simpleName,
mergeBranchName = mergeBranch.name, mergeBranchName = mergeBranch.name,
onReject = { setMergeBranch(null) }, onReject = { setMergeBranch(null) },
onAccept = { ff -> branchesViewModel.mergeBranch(mergeBranch, ff) } onAccept = { ff -> branchesViewModel.mergeBranch(mergeBranch, ff) }
) )
} }
if (rebaseBranch != null) { if (rebaseBranch != null && currentBranch != null) {
RebaseDialog( RebaseDialog(
currentBranch, currentBranchName = currentBranch.simpleName,
rebaseBranchName = rebaseBranch.name, rebaseBranchName = rebaseBranch.name,
onReject = { setRebaseBranch(null) }, onReject = { setRebaseBranch(null) },
onAccept = { branchesViewModel.rebaseBranch(rebaseBranch) } onAccept = { branchesViewModel.rebaseBranch(rebaseBranch) }
@ -72,7 +74,7 @@ fun Branches(
@Composable @Composable
private fun BranchLineEntry( private fun BranchLineEntry(
branch: Ref, branch: Ref,
currentBranchName: String, currentBranch: Ref?,
isCurrentBranch: Boolean, isCurrentBranch: Boolean,
onBranchClicked: () -> Unit, onBranchClicked: () -> Unit,
onCheckoutBranch: () -> Unit, onCheckoutBranch: () -> Unit,
@ -86,7 +88,7 @@ private fun BranchLineEntry(
items = { items = {
branchContextMenuItems( branchContextMenuItems(
branch = branch, branch = branch,
currentBranchName = currentBranchName, currentBranch = currentBranch,
isCurrentBranch = isCurrentBranch, isCurrentBranch = isCurrentBranch,
isLocal = branch.isLocal, isLocal = branch.isLocal,
onCheckoutBranch = onCheckoutBranch, onCheckoutBranch = onCheckoutBranch,

View File

@ -2,6 +2,8 @@ package app.ui.context_menu
import androidx.compose.foundation.ContextMenuItem import androidx.compose.foundation.ContextMenuItem
import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.ExperimentalFoundationApi
import app.extensions.isBranch
import app.extensions.isHead
import app.extensions.simpleLogName import app.extensions.simpleLogName
import org.eclipse.jgit.lib.Ref import org.eclipse.jgit.lib.Ref
@ -9,7 +11,7 @@ import org.eclipse.jgit.lib.Ref
fun branchContextMenuItems( fun branchContextMenuItems(
branch: Ref, branch: Ref,
isCurrentBranch: Boolean, isCurrentBranch: Boolean,
currentBranchName: String, currentBranch: Ref?,
isLocal: Boolean, isLocal: Boolean,
onCheckoutBranch: () -> Unit, onCheckoutBranch: () -> Unit,
onMergeBranch: () -> Unit, onMergeBranch: () -> Unit,
@ -26,18 +28,20 @@ fun branchContextMenuItems(
onClick = onCheckoutBranch onClick = onCheckoutBranch
) )
) )
add( if(currentBranch != null && !currentBranch.isHead) {
ContextMenuItem( add(
label = "Merge branch", ContextMenuItem(
onClick = onMergeBranch label = "Merge branch",
onClick = onMergeBranch
)
) )
) add(
add( ContextMenuItem(
ContextMenuItem( label = "Rebase branch",
label = "Rebase branch", onClick = onRebaseBranch
onClick = onRebaseBranch )
) )
) }
} }
if (isLocal && !isCurrentBranch) { if (isLocal && !isCurrentBranch) {
add( add(
@ -47,16 +51,16 @@ fun branchContextMenuItems(
) )
) )
} }
if (!isLocal) { if (!isLocal && currentBranch != null && !currentBranch.isHead) {
add( add(
ContextMenuItem( ContextMenuItem(
label = "Push $currentBranchName to ${branch.simpleLogName}", label = "Push ${currentBranch.simpleLogName} to ${branch.simpleLogName}",
onClick = onPushToRemoteBranch onClick = onPushToRemoteBranch
) )
) )
add( add(
ContextMenuItem( ContextMenuItem(
label = "Pull ${branch.simpleLogName} to $currentBranchName", label = "Pull ${branch.simpleLogName} to ${currentBranch.simpleLogName}",
onClick = onPullFromRemoteBranch onClick = onPullFromRemoteBranch
) )
) )

View File

@ -636,7 +636,7 @@ fun CommitMessage(
BranchChip( BranchChip(
ref = ref, ref = ref,
color = nodeColor, color = nodeColor,
currentBranch = currentBranch?.name.orEmpty(), currentBranch = currentBranch,
isCurrentBranch = ref.isSameBranch(currentBranch), isCurrentBranch = ref.isSameBranch(currentBranch),
onCheckoutBranch = { onCheckoutRef(ref) }, onCheckoutBranch = { onCheckoutRef(ref) },
onMergeBranch = { onMergeBranch(ref) }, onMergeBranch = { onMergeBranch(ref) },
@ -825,7 +825,7 @@ fun BranchChip(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
isCurrentBranch: Boolean = false, isCurrentBranch: Boolean = false,
ref: Ref, ref: Ref,
currentBranch: String, currentBranch: Ref?,
onCheckoutBranch: () -> Unit, onCheckoutBranch: () -> Unit,
onMergeBranch: () -> Unit, onMergeBranch: () -> Unit,
onDeleteBranch: () -> Unit, onDeleteBranch: () -> Unit,
@ -837,7 +837,7 @@ fun BranchChip(
val contextMenuItemsList = { val contextMenuItemsList = {
branchContextMenuItems( branchContextMenuItems(
branch = ref, branch = ref,
currentBranchName = currentBranch, currentBranch = currentBranch,
isCurrentBranch = isCurrentBranch, isCurrentBranch = isCurrentBranch,
isLocal = ref.isLocal, isLocal = ref.isLocal,
onCheckoutBranch = onCheckoutBranch, onCheckoutBranch = onCheckoutBranch,

View File

@ -18,17 +18,17 @@ class BranchesViewModel @Inject constructor(
val branches: StateFlow<List<Ref>> val branches: StateFlow<List<Ref>>
get() = _branches get() = _branches
private val _currentBranch = MutableStateFlow<String>("") private val _currentBranch = MutableStateFlow<Ref?>(null)
val currentBranch: StateFlow<String> val currentBranch: StateFlow<Ref?>
get() = _currentBranch get() = _currentBranch
suspend fun loadBranches(git: Git) { suspend fun loadBranches(git: Git) {
_currentBranch.value = branchesManager.currentBranchRef(git)?.name ?: "" _currentBranch.value = branchesManager.currentBranchRef(git)
val branchesList = branchesManager.getBranches(git) val branchesList = branchesManager.getBranches(git)
// set selected branch as the first one always // set selected branch as the first one always
val selectedBranch = branchesList.find { it.name == _currentBranch.value } val selectedBranch = branchesList.find { it.name == _currentBranch.value?.name }
if (selectedBranch != null) { if (selectedBranch != null) {
branchesList.remove(selectedBranch) branchesList.remove(selectedBranch)
branchesList.add(0, selectedBranch) branchesList.add(0, selectedBranch)