1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-06-15 00:15:32 +02:00

Use DisabledReason for upstream options items

This commit is contained in:
Stefan Haller
2023-09-06 09:40:28 +02:00
parent 75aed98c35
commit f2f50ccf75
3 changed files with 90 additions and 97 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers" "github.com/jesseduffield/lazygit/pkg/gui/controllers/helpers"
"github.com/jesseduffield/lazygit/pkg/gui/types" "github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
) )
type BranchesController struct { type BranchesController struct {
@ -140,8 +141,7 @@ func (self *BranchesController) GetOnRenderToMain() func() error {
} }
func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error { func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error {
options := []*types.MenuItem{ viewDivergenceItem := &types.MenuItem{
{
LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream}, LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream},
OnPress: func() error { OnPress: func() error {
branch := self.context().GetSelected() branch := self.context().GetSelected()
@ -149,9 +149,6 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
return nil return nil
} }
if !branch.RemoteBranchStoredLocally() {
return self.c.ErrorMsg(self.c.Tr.DivergenceNoUpstream)
}
return self.c.Helpers().SubCommits.ViewSubCommits(helpers.ViewSubCommitsOpts{ return self.c.Helpers().SubCommits.ViewSubCommits(helpers.ViewSubCommitsOpts{
Ref: branch, Ref: branch,
TitleRef: fmt.Sprintf("%s <-> %s", branch.RefName(), branch.ShortUpstreamRefName()), TitleRef: fmt.Sprintf("%s <-> %s", branch.RefName(), branch.ShortUpstreamRefName()),
@ -161,8 +158,9 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
}) })
}, },
Key: 'v', Key: 'v',
}, }
{
unsetUpstreamItem := &types.MenuItem{
LabelColumns: []string{self.c.Tr.UnsetUpstream}, LabelColumns: []string{self.c.Tr.UnsetUpstream},
OnPress: func() error { OnPress: func() error {
if err := self.c.Git().Branch.UnsetUpstream(selectedBranch.Name); err != nil { if err := self.c.Git().Branch.UnsetUpstream(selectedBranch.Name); err != nil {
@ -180,8 +178,9 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
return nil return nil
}, },
Key: 'u', Key: 'u',
}, }
{
setUpstreamItem := &types.MenuItem{
LabelColumns: []string{self.c.Tr.SetUpstream}, LabelColumns: []string{self.c.Tr.SetUpstream},
OnPress: func() error { OnPress: func() error {
return self.c.Helpers().Upstream.PromptForUpstreamWithoutInitialContent(selectedBranch, func(upstream string) error { return self.c.Helpers().Upstream.PromptForUpstreamWithoutInitialContent(selectedBranch, func(upstream string) error {
@ -206,11 +205,11 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
}) })
}, },
Key: 's', Key: 's',
},
} }
if selectedBranch.IsTrackingRemote() { upstream := lo.Ternary(selectedBranch.RemoteBranchStoredLocally(),
upstream := fmt.Sprintf("%s/%s", selectedBranch.UpstreamRemote, selectedBranch.Name) fmt.Sprintf("%s/%s", selectedBranch.UpstreamRemote, selectedBranch.Name),
self.c.Tr.UpstreamGenericName)
upstreamResetOptions := utils.ResolvePlaceholderString( upstreamResetOptions := utils.ResolvePlaceholderString(
self.c.Tr.ViewUpstreamResetOptions, self.c.Tr.ViewUpstreamResetOptions,
map[string]string{"upstream": upstream}, map[string]string{"upstream": upstream},
@ -220,14 +219,10 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
map[string]string{"upstream": upstream}, map[string]string{"upstream": upstream},
) )
options = append(options, &types.MenuItem{ upstreamResetItem := &types.MenuItem{
LabelColumns: []string{upstreamResetOptions}, LabelColumns: []string{upstreamResetOptions},
OpensMenu: true, OpensMenu: true,
OnPress: func() error { OnPress: func() error {
if selectedBranch.RemoteBranchNotStoredLocally() {
return self.c.ErrorMsg(self.c.Tr.UpstreamNotStoredLocallyError)
}
err := self.c.Helpers().Refs.CreateGitResetMenu(upstream) err := self.c.Helpers().Refs.CreateGitResetMenu(upstream)
if err != nil { if err != nil {
return self.c.Error(err) return self.c.Error(err)
@ -236,17 +231,19 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
}, },
Tooltip: upstreamResetTooltip, Tooltip: upstreamResetTooltip,
Key: 'g', Key: 'g',
}) }
} else {
options = append(options, &types.MenuItem{ if !selectedBranch.RemoteBranchStoredLocally() {
LabelColumns: []string{self.c.Tr.ViewUpstreamDisabledResetOptions}, viewDivergenceItem.DisabledReason = self.c.Tr.UpstreamNotSetError
OpensMenu: true, unsetUpstreamItem.DisabledReason = self.c.Tr.UpstreamNotSetError
OnPress: func() error { upstreamResetItem.DisabledReason = self.c.Tr.UpstreamNotSetError
return self.c.ErrorMsg(self.c.Tr.UpstreamNotSetError) }
},
Tooltip: self.c.Tr.UpstreamNotSetError, options := []*types.MenuItem{
Key: 'g', viewDivergenceItem,
}) unsetUpstreamItem,
setUpstreamItem,
upstreamResetItem,
} }
return self.c.Menu(types.CreateMenuOptions{ return self.c.Menu(types.CreateMenuOptions{

View File

@ -352,12 +352,11 @@ type TranslationSet struct {
SetUpstream string SetUpstream string
UnsetUpstream string UnsetUpstream string
ViewDivergenceFromUpstream string ViewDivergenceFromUpstream string
DivergenceNoUpstream string
DivergenceSectionHeaderLocal string DivergenceSectionHeaderLocal string
DivergenceSectionHeaderRemote string DivergenceSectionHeaderRemote string
ViewUpstreamResetOptions string ViewUpstreamResetOptions string
ViewUpstreamResetOptionsTooltip string ViewUpstreamResetOptionsTooltip string
ViewUpstreamDisabledResetOptions string UpstreamGenericName string
SetUpstreamTitle string SetUpstreamTitle string
SetUpstreamMessage string SetUpstreamMessage string
EditRemote string EditRemote string
@ -405,7 +404,6 @@ type TranslationSet struct {
ViewBranchUpstreamOptions string ViewBranchUpstreamOptions string
BranchUpstreamOptionsTitle string BranchUpstreamOptionsTitle string
ViewBranchUpstreamOptionsTooltip string ViewBranchUpstreamOptionsTooltip string
UpstreamNotStoredLocallyError string
UpstreamNotSetError string UpstreamNotSetError string
NewGitFlowBranchPrompt string NewGitFlowBranchPrompt string
RenameBranchWarning string RenameBranchWarning string
@ -1148,12 +1146,11 @@ func EnglishTranslationSet() TranslationSet {
SetUpstream: "Set upstream of selected branch", SetUpstream: "Set upstream of selected branch",
UnsetUpstream: "Unset upstream of selected branch", UnsetUpstream: "Unset upstream of selected branch",
ViewDivergenceFromUpstream: "View divergence from upstream", ViewDivergenceFromUpstream: "View divergence from upstream",
DivergenceNoUpstream: "Cannot show divergence of a branch that has no (locally tracked) upstream",
DivergenceSectionHeaderLocal: "Local", DivergenceSectionHeaderLocal: "Local",
DivergenceSectionHeaderRemote: "Remote", DivergenceSectionHeaderRemote: "Remote",
ViewUpstreamResetOptions: "Reset checked-out branch onto {{.upstream}}", ViewUpstreamResetOptions: "Reset checked-out branch onto {{.upstream}}",
ViewUpstreamResetOptionsTooltip: "View options for resetting the checked-out branch onto {{upstream}}. Note: this will not reset the selected branch onto the upstream, it will reset the checked-out branch onto the upstream", ViewUpstreamResetOptionsTooltip: "View options for resetting the checked-out branch onto {{upstream}}. Note: this will not reset the selected branch onto the upstream, it will reset the checked-out branch onto the upstream",
ViewUpstreamDisabledResetOptions: "Reset checked-out branch onto upstream of selected branch", UpstreamGenericName: "upstream of selected branch",
SetUpstreamTitle: "Set upstream branch", SetUpstreamTitle: "Set upstream branch",
SetUpstreamMessage: "Are you sure you want to set the upstream branch of '{{.checkedOut}}' to '{{.selected}}'", SetUpstreamMessage: "Are you sure you want to set the upstream branch of '{{.checkedOut}}' to '{{.selected}}'",
EditRemote: "Edit remote", EditRemote: "Edit remote",
@ -1197,8 +1194,7 @@ func EnglishTranslationSet() TranslationSet {
RenameBranch: "Rename branch", RenameBranch: "Rename branch",
BranchUpstreamOptionsTitle: "Upstream options", BranchUpstreamOptionsTitle: "Upstream options",
ViewBranchUpstreamOptionsTooltip: "View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream", ViewBranchUpstreamOptionsTooltip: "View options relating to the branch's upstream e.g. setting/unsetting the upstream and resetting to the upstream",
UpstreamNotStoredLocallyError: "Cannot reset to upstream branch because it is not stored locally", UpstreamNotSetError: "The selected branch has no upstream (or the upstream is not stored locally)",
UpstreamNotSetError: "The selected branch has no upstream",
ViewBranchUpstreamOptions: "View upstream options", ViewBranchUpstreamOptions: "View upstream options",
NewBranchNamePrompt: "Enter new branch name for branch", NewBranchNamePrompt: "Enter new branch name for branch",
RenameBranchWarning: "This branch is tracking a remote. This action will only rename the local branch name, not the name of the remote branch. Continue?", RenameBranchWarning: "This branch is tracking a remote. This action will only rename the local branch name, not the name of the remote branch. Continue?",

View File

@ -41,11 +41,11 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
t.ExpectPopup().Menu(). t.ExpectPopup().Menu().
Title(Equals("Upstream options")). Title(Equals("Upstream options")).
Select(Contains("Reset checked-out branch onto upstream of selected branch")). Select(Contains("Reset checked-out branch onto upstream of selected branch")).
Tooltip(Contains("The selected branch has no upstream")). Tooltip(Contains("Disabled: The selected branch has no upstream (or the upstream is not stored locally)")).
Confirm() Confirm()
t.ExpectPopup().Alert(). t.ExpectPopup().Alert().
Title(Equals("Error")). Title(Equals("Error")).
Content(Equals("The selected branch has no upstream")). Content(Equals("The selected branch has no upstream (or the upstream is not stored locally)")).
Confirm() Confirm()
}). }).
SelectNextItem(). SelectNextItem().