1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-21 12:16:54 +02:00

feat: add a menu to reset current branch to a target branch upstream

This commit is contained in:
AzraelSec 2023-08-16 16:19:29 +02:00 committed by Stefan Haller
parent 47d422bb8a
commit 2b7b6f71ee
4 changed files with 209 additions and 54 deletions

View File

@ -140,9 +140,7 @@ func (self *BranchesController) GetOnRenderToMain() func() error {
} }
func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error { func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error {
return self.c.Menu(types.CreateMenuOptions{ options := []*types.MenuItem{
Title: self.c.Tr.BranchUpstreamOptionsTitle,
Items: []*types.MenuItem{
{ {
LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream}, LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream},
OnPress: func() error { OnPress: func() error {
@ -209,7 +207,51 @@ func (self *BranchesController) setUpstream(selectedBranch *models.Branch) error
}, },
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},
)
options = append(options, &types.MenuItem{
LabelColumns: []string{upstreamResetOptions},
OpensMenu: true,
OnPress: func() error {
if selectedBranch.RemoteBranchNotStoredLocally() {
return self.c.ErrorMsg(self.c.Tr.UpstreamNotStoredLocallyError)
}
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',
})
}
return self.c.Menu(types.CreateMenuOptions{
Title: self.c.Tr.BranchUpstreamOptionsTitle,
Items: options,
}) })
} }

View File

@ -352,6 +352,9 @@ type TranslationSet struct {
DivergenceNoUpstream string DivergenceNoUpstream string
DivergenceSectionHeaderLocal string DivergenceSectionHeaderLocal string
DivergenceSectionHeaderRemote string DivergenceSectionHeaderRemote string
ViewUpstreamResetOptions string
ViewUpstreamResetOptionsTooltip string
ViewUpstreamDisabledResetOptions string
SetUpstreamTitle string SetUpstreamTitle string
SetUpstreamMessage string SetUpstreamMessage string
EditRemote string EditRemote string
@ -399,6 +402,8 @@ type TranslationSet struct {
ViewBranchUpstreamOptions string ViewBranchUpstreamOptions string
BranchUpstreamOptionsTitle string BranchUpstreamOptionsTitle string
ViewBranchUpstreamOptionsTooltip string ViewBranchUpstreamOptionsTooltip string
UpstreamNotStoredLocallyError string
UpstreamNotSetError string
NewGitFlowBranchPrompt string NewGitFlowBranchPrompt string
RenameBranchWarning string RenameBranchWarning string
OpenMenu string OpenMenu string
@ -1138,6 +1143,9 @@ func EnglishTranslationSet() TranslationSet {
DivergenceNoUpstream: "Cannot show divergence of a branch that has no (locally tracked) 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}}",
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",
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",
@ -1181,6 +1189,8 @@ 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",
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

@ -0,0 +1,102 @@
package branch
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Hard reset the current branch to the selected branch upstream",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.
CloneIntoRemote("origin").
NewBranch("hard-branch").
EmptyCommit("hard commit").
PushBranch("origin", "hard-branch").
NewBranch("soft-branch").
EmptyCommit("soft commit").
PushBranch("origin", "soft-branch").
NewBranch("base").
EmptyCommit("base-branch commit").
CreateFile("file-1", "content").
GitAdd("file-1").
Commit("commit with file").
CreateFile("file-2", "content").
GitAdd("file-2")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
// soft reset
t.Views().Branches().
Focus().
Lines(
Contains("base").IsSelected(),
Contains("soft-branch"),
Contains("hard-branch"),
).
Press(keys.Branches.SetUpstream).
Tap(func() {
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")).
Confirm()
t.ExpectPopup().Alert().
Title(Equals("Error")).
Content(Equals("The selected branch has no upstream")).
Confirm()
}).
SelectNextItem().
Lines(
Contains("base"),
Contains("soft-branch").IsSelected(),
Contains("hard-branch"),
).
Press(keys.Branches.SetUpstream).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Upstream options")).
Select(Contains("Reset checked-out branch onto origin/soft-branch...")).
Confirm()
t.ExpectPopup().Menu().
Title(Equals("Reset to origin/soft-branch")).
Select(Contains("Soft reset")).
Confirm()
})
t.Views().Commits().Lines(
Contains("soft commit"),
Contains("hard commit"),
)
t.Views().Files().Lines(
Contains("file-1").Contains("A"),
Contains("file-2").Contains("A"),
)
// hard reset
t.Views().Branches().
Focus().
Lines(
Contains("base"),
Contains("soft-branch").IsSelected(),
Contains("hard-branch"),
).
NavigateToLine(Contains("hard-branch")).
Press(keys.Branches.SetUpstream).
Tap(func() {
t.ExpectPopup().Menu().
Title(Equals("Upstream options")).
Select(Contains("Reset checked-out branch onto origin/hard-branch...")).
Confirm()
t.ExpectPopup().Menu().
Title(Equals("Reset to origin/hard-branch")).
Select(Contains("Hard reset")).
Confirm()
})
t.Views().Commits().Lines(Contains("hard commit"))
t.Views().Files().IsEmpty()
},
})

View File

@ -47,6 +47,7 @@ var tests = []*components.IntegrationTest{
branch.RebaseDoesNotAutosquash, branch.RebaseDoesNotAutosquash,
branch.RebaseFromMarkedBase, branch.RebaseFromMarkedBase,
branch.Reset, branch.Reset,
branch.ResetToUpstream,
branch.ResetUpstream, branch.ResetUpstream,
branch.SetUpstream, branch.SetUpstream,
branch.ShowDivergenceFromUpstream, branch.ShowDivergenceFromUpstream,