From 3de2c6bd551248760edd06da77c6b23e8663974f Mon Sep 17 00:00:00 2001 From: Abdelilah El Aissaoui Date: Tue, 14 Mar 2023 10:53:38 +0100 Subject: [PATCH] Hidden rebase interactive on the last commit of the current branch Fixes #64 --- .../gitnuro/ui/context_menu/LogContextMenu.kt | 60 +++++++++++++------ .../com/jetpackduba/gitnuro/ui/log/Log.kt | 4 ++ 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/LogContextMenu.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/LogContextMenu.kt index 796d28f..a8a4b34 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/LogContextMenu.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/context_menu/LogContextMenu.kt @@ -1,5 +1,7 @@ package com.jetpackduba.gitnuro.ui.context_menu +import androidx.compose.runtime.Composable +import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.res.painterResource import com.jetpackduba.gitnuro.AppIcons @@ -11,39 +13,59 @@ fun logContextMenu( onCherryPickCommit: () -> Unit, onResetBranch: () -> Unit, onRebaseInteractive: () -> Unit, -) = listOf( - ContextMenuElement.ContextTextEntry( + isLastCommit: Boolean, +) = mutableListOf().apply { + addContextMenu( label = "Checkout commit", icon = { painterResource(AppIcons.START) }, onClick = onCheckoutCommit - ), - ContextMenuElement.ContextTextEntry( + ) + addContextMenu( label = "Create branch", icon = { painterResource(AppIcons.BRANCH) }, onClick = onCreateNewBranch - ), - ContextMenuElement.ContextTextEntry( + ) + addContextMenu( label = "Create tag", icon = { painterResource(AppIcons.TAG) }, onClick = onCreateNewTag - ), - ContextMenuElement.ContextSeparator, - ContextMenuElement.ContextTextEntry( - label = "Rebase interactive", - onClick = onRebaseInteractive - ), - ContextMenuElement.ContextTextEntry( + ) + + add(ContextMenuElement.ContextSeparator) + + if (!isLastCommit) { + addContextMenu( + label = "Rebase interactive", + onClick = onRebaseInteractive + ) + } + + addContextMenu( label = "Revert commit", icon = { painterResource(AppIcons.REVERT) }, onClick = onRevertCommit - ), - ContextMenuElement.ContextTextEntry( + ) + addContextMenu( label = "Cherry-pick commit", onClick = onCherryPickCommit - ), - ContextMenuElement.ContextTextEntry( + ) + addContextMenu( label = "Reset current branch to this commit", icon = { painterResource(AppIcons.UNDO) }, onClick = onResetBranch - ), -) \ No newline at end of file + ) +} + +fun MutableList.addContextMenu( + label: String, + icon: @Composable (() -> Painter)? = null, + onClick: () -> Unit = {} +) { + this.add( + ContextMenuElement.ContextTextEntry( + label, + icon, + onClick, + ) + ) +} \ No newline at end of file diff --git a/src/main/kotlin/com/jetpackduba/gitnuro/ui/log/Log.kt b/src/main/kotlin/com/jetpackduba/gitnuro/ui/log/Log.kt index 111eebe..5407b87 100644 --- a/src/main/kotlin/com/jetpackduba/gitnuro/ui/log/Log.kt +++ b/src/main/kotlin/com/jetpackduba/gitnuro/ui/log/Log.kt @@ -41,6 +41,7 @@ import com.jetpackduba.gitnuro.AppIcons import com.jetpackduba.gitnuro.extensions.* import com.jetpackduba.gitnuro.git.graph.GraphCommitList import com.jetpackduba.gitnuro.git.graph.GraphNode +import com.jetpackduba.gitnuro.git.graph.UncommitedChangesGraphNode import com.jetpackduba.gitnuro.git.workspace.StatusSummary import com.jetpackduba.gitnuro.keybindings.KeybindingOption import com.jetpackduba.gitnuro.keybindings.matchesBinding @@ -773,6 +774,8 @@ fun CommitLine( onRevCommitSelected: () -> Unit, onRebaseInteractive: () -> Unit, ) { + val isLastCommitOfCurrentBranch = currentBranch?.objectId?.name == graphNode.id.name + ContextMenu( items = { logContextMenu( @@ -783,6 +786,7 @@ fun CommitLine( onCherryPickCommit = { logViewModel.cherrypickCommit(graphNode) }, onRebaseInteractive = onRebaseInteractive, onResetBranch = { resetBranch() }, + isLastCommit = isLastCommitOfCurrentBranch ) }, ) {