1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-02-03 13:21:56 +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/types"
"github.com/jesseduffield/lazygit/pkg/utils"
"github.com/samber/lo"
)
type BranchesController struct {
@ -140,32 +141,55 @@ func (self *BranchesController) GetOnRenderToMain() func() error {
}
func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error {
options := []*types.MenuItem{
{
LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream},
OnPress: func() error {
branch := self.context().GetSelected()
if branch == nil {
return nil
viewDivergenceItem := &types.MenuItem{
LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream},
OnPress: func() error {
branch := self.context().GetSelected()
if branch == nil {
return nil
}
return self.c.Helpers().SubCommits.ViewSubCommits(helpers.ViewSubCommitsOpts{
Ref: branch,
TitleRef: fmt.Sprintf("%s <-> %s", branch.RefName(), branch.ShortUpstreamRefName()),
RefToShowDivergenceFrom: branch.FullUpstreamRefName(),
Context: self.context(),
ShowBranchHeads: false,
})
},
Key: 'v',
}
unsetUpstreamItem := &types.MenuItem{
LabelColumns: []string{self.c.Tr.UnsetUpstream},
OnPress: func() error {
if err := self.c.Git().Branch.UnsetUpstream(selectedBranch.Name); err != nil {
return self.c.Error(err)
}
if err := self.c.Refresh(types.RefreshOptions{
Mode: types.SYNC,
Scope: []types.RefreshableView{
types.BRANCHES,
types.COMMITS,
},
}); err != nil {
return self.c.Error(err)
}
return nil
},
Key: 'u',
}
setUpstreamItem := &types.MenuItem{
LabelColumns: []string{self.c.Tr.SetUpstream},
OnPress: func() error {
return self.c.Helpers().Upstream.PromptForUpstreamWithoutInitialContent(selectedBranch, func(upstream string) error {
upstreamRemote, upstreamBranch, err := self.c.Helpers().Upstream.ParseUpstream(upstream)
if err != nil {
return self.c.Error(err)
}
if !branch.RemoteBranchStoredLocally() {
return self.c.ErrorMsg(self.c.Tr.DivergenceNoUpstream)
}
return self.c.Helpers().SubCommits.ViewSubCommits(helpers.ViewSubCommitsOpts{
Ref: branch,
TitleRef: fmt.Sprintf("%s <-> %s", branch.RefName(), branch.ShortUpstreamRefName()),
RefToShowDivergenceFrom: branch.FullUpstreamRefName(),
Context: self.context(),
ShowBranchHeads: false,
})
},
Key: 'v',
},
{
LabelColumns: []string{self.c.Tr.UnsetUpstream},
OnPress: func() error {
if err := self.c.Git().Branch.UnsetUpstream(selectedBranch.Name); err != nil {
if err := self.c.Git().Branch.SetUpstream(upstreamRemote, upstreamBranch, selectedBranch.Name); err != nil {
return self.c.Error(err)
}
if err := self.c.Refresh(types.RefreshOptions{
@ -178,75 +202,48 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
return self.c.Error(err)
}
return nil
},
Key: 'u',
},
{
LabelColumns: []string{self.c.Tr.SetUpstream},
OnPress: func() error {
return self.c.Helpers().Upstream.PromptForUpstreamWithoutInitialContent(selectedBranch, func(upstream string) error {
upstreamRemote, upstreamBranch, err := self.c.Helpers().Upstream.ParseUpstream(upstream)
if err != nil {
return self.c.Error(err)
}
if err := self.c.Git().Branch.SetUpstream(upstreamRemote, upstreamBranch, selectedBranch.Name); err != nil {
return self.c.Error(err)
}
if err := self.c.Refresh(types.RefreshOptions{
Mode: types.SYNC,
Scope: []types.RefreshableView{
types.BRANCHES,
types.COMMITS,
},
}); err != nil {
return self.c.Error(err)
}
return nil
})
},
Key: 's',
})
},
Key: 's',
}
if selectedBranch.IsTrackingRemote() {
upstream := fmt.Sprintf("%s/%s", selectedBranch.UpstreamRemote, selectedBranch.Name)
upstreamResetOptions := utils.ResolvePlaceholderString(
self.c.Tr.ViewUpstreamResetOptions,
map[string]string{"upstream": upstream},
)
upstreamResetTooltip := utils.ResolvePlaceholderString(
self.c.Tr.ViewUpstreamResetOptionsTooltip,
map[string]string{"upstream": upstream},
)
upstream := lo.Ternary(selectedBranch.RemoteBranchStoredLocally(),
fmt.Sprintf("%s/%s", selectedBranch.UpstreamRemote, selectedBranch.Name),
self.c.Tr.UpstreamGenericName)
upstreamResetOptions := utils.ResolvePlaceholderString(
self.c.Tr.ViewUpstreamResetOptions,
map[string]string{"upstream": upstream},
)
upstreamResetTooltip := utils.ResolvePlaceholderString(
self.c.Tr.ViewUpstreamResetOptionsTooltip,
map[string]string{"upstream": upstream},
)
options = append(options, &types.MenuItem{
LabelColumns: []string{upstreamResetOptions},
OpensMenu: true,
OnPress: func() error {
if selectedBranch.RemoteBranchNotStoredLocally() {
return self.c.ErrorMsg(self.c.Tr.UpstreamNotStoredLocallyError)
}
upstreamResetItem := &types.MenuItem{
LabelColumns: []string{upstreamResetOptions},
OpensMenu: true,
OnPress: func() error {
err := self.c.Helpers().Refs.CreateGitResetMenu(upstream)
if err != nil {
return self.c.Error(err)
}
return nil
},
Tooltip: upstreamResetTooltip,
Key: 'g',
}
err := self.c.Helpers().Refs.CreateGitResetMenu(upstream)
if err != nil {
return self.c.Error(err)
}
return nil
},
Tooltip: upstreamResetTooltip,
Key: 'g',
})
} else {
options = append(options, &types.MenuItem{
LabelColumns: []string{self.c.Tr.ViewUpstreamDisabledResetOptions},
OpensMenu: true,
OnPress: func() error {
return self.c.ErrorMsg(self.c.Tr.UpstreamNotSetError)
},
Tooltip: self.c.Tr.UpstreamNotSetError,
Key: 'g',
})
if !selectedBranch.RemoteBranchStoredLocally() {
viewDivergenceItem.DisabledReason = self.c.Tr.UpstreamNotSetError
unsetUpstreamItem.DisabledReason = self.c.Tr.UpstreamNotSetError
upstreamResetItem.DisabledReason = self.c.Tr.UpstreamNotSetError
}
options := []*types.MenuItem{
viewDivergenceItem,
unsetUpstreamItem,
setUpstreamItem,
upstreamResetItem,
}
return self.c.Menu(types.CreateMenuOptions{

View File

@ -352,12 +352,11 @@ type TranslationSet struct {
SetUpstream string
UnsetUpstream string
ViewDivergenceFromUpstream string
DivergenceNoUpstream string
DivergenceSectionHeaderLocal string
DivergenceSectionHeaderRemote string
ViewUpstreamResetOptions string
ViewUpstreamResetOptionsTooltip string
ViewUpstreamDisabledResetOptions string
UpstreamGenericName string
SetUpstreamTitle string
SetUpstreamMessage string
EditRemote string
@ -405,7 +404,6 @@ type TranslationSet struct {
ViewBranchUpstreamOptions string
BranchUpstreamOptionsTitle string
ViewBranchUpstreamOptionsTooltip string
UpstreamNotStoredLocallyError string
UpstreamNotSetError string
NewGitFlowBranchPrompt string
RenameBranchWarning string
@ -1148,12 +1146,11 @@ func EnglishTranslationSet() TranslationSet {
SetUpstream: "Set upstream of selected branch",
UnsetUpstream: "Unset upstream of selected branch",
ViewDivergenceFromUpstream: "View divergence from upstream",
DivergenceNoUpstream: "Cannot show divergence of a branch that has no (locally tracked) upstream",
DivergenceSectionHeaderLocal: "Local",
DivergenceSectionHeaderRemote: "Remote",
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",
ViewUpstreamDisabledResetOptions: "Reset checked-out branch onto upstream of selected branch",
UpstreamGenericName: "upstream of selected branch",
SetUpstreamTitle: "Set upstream branch",
SetUpstreamMessage: "Are you sure you want to set the upstream branch of '{{.checkedOut}}' to '{{.selected}}'",
EditRemote: "Edit remote",
@ -1197,8 +1194,7 @@ func EnglishTranslationSet() TranslationSet {
RenameBranch: "Rename branch",
BranchUpstreamOptionsTitle: "Upstream options",
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",
UpstreamNotSetError: "The selected branch has no upstream (or the upstream is not stored locally)",
ViewBranchUpstreamOptions: "View upstream options",
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?",

View File

@ -41,11 +41,11 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
t.ExpectPopup().Menu().
Title(Equals("Upstream options")).
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()
t.ExpectPopup().Alert().
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()
}).
SelectNextItem().