summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Haller <stefan@haller-berlin.de>2023-09-06 09:40:28 +0200
committerStefan Haller <stefan@haller-berlin.de>2023-09-18 10:15:11 +0200
commitf2f50ccf758033e817b20849a0175e7a383a3856 (patch)
tree4225e507be6711d9ac0998e0c478e106c81fd1d2
parent75aed98c3599afcb5791ec4a37319fe04db56736 (diff)
Use DisabledReason for upstream options items
-rw-r--r--pkg/gui/controllers/branches_controller.go171
-rw-r--r--pkg/i18n/english.go10
-rw-r--r--pkg/integration/tests/branch/reset_to_upstream.go4
3 files changed, 89 insertions, 96 deletions
diff --git a/pkg/gui/controllers/branches_controller.go b/pkg/gui/controllers/branches_controller.go
index e60e5c410..bd7b37700 100644
--- a/pkg/gui/controllers/branches_controller.go
+++ b/pkg/gui/controllers/branches_controller.go
@@ -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
+ }
- 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',
+ 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,
+ })
},
- {
- LabelColumns: []string{self.c.Tr.UnsetUpstream},
- OnPress: func() error {
- if err := self.c.Git().Branch.UnsetUpstream(selectedBranch.Name); err != nil {
+ 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 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)
- }
+ Key: 's',
+ }
- 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',
+ 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},
+ )
+
+ 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',
}
- 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},
- )
-
- options = append(options, &types.MenuItem{
- LabelColumns: []string{upstreamResetOptions},
- OpensMenu: true,
- OnPress: func() error {
- if selectedBranch.RemoteBranchNotStoredLocally() {
- return self.c.ErrorMsg(self.c.Tr.UpstreamNotStoredLocallyError)
- }
+ if !selectedBranch.RemoteBranchStoredLocally() {
+ viewDivergenceItem.DisabledReason = self.c.Tr.UpstreamNotSetError
+ unsetUpstreamItem.DisabledReason = self.c.Tr.UpstreamNotSetError
+ upstreamResetItem.DisabledReason = self.c.Tr.UpstreamNotSetError
+ }
- 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',
- })
+ options := []*types.MenuItem{
+ viewDivergenceItem,
+ unsetUpstreamItem,
+ setUpstreamItem,
+ upstreamResetItem,
}
return self.c.Menu(types.CreateMenuOptions{
diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go
index 0f788c317..3182dfa68 100644
--- a/pkg/i18n/english.go
+++ b/pkg/i18n/english.go
@@ -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?",
diff --git a/pkg/integration/tests/branch/reset_to_upstream.go b/pkg/integration/tests/branch/reset_to_upstream.go
index 0c749ee2d..1eecd689b 100644
--- a/pkg/integration/tests/branch/reset_to_upstream.go
+++ b/pkg/integration/tests/branch/reset_to_upstream.go
@@ -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().