1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-07-07 01:09:45 +02:00

Fix reset/rebase to upstream (#4151)

- **PR Description**

Resetting to the upstream branch was broken when the remote branch has a
different name than the local branch.

Rebasing onto the upstream worked fine, but also displayed the wrong
branch name in the menu.

Fixes #4148.
This commit is contained in:
Stefan Haller
2025-01-06 09:31:44 +01:00
committed by GitHub
4 changed files with 19 additions and 12 deletions

View File

@ -194,6 +194,10 @@ func (self *BranchesController) GetOnRenderToMain() func() {
} }
func (self *BranchesController) viewUpstreamOptions(selectedBranch *models.Branch) error { func (self *BranchesController) viewUpstreamOptions(selectedBranch *models.Branch) error {
upstream := lo.Ternary(selectedBranch.RemoteBranchStoredLocally(),
selectedBranch.ShortUpstreamRefName(),
self.c.Tr.UpstreamGenericName)
viewDivergenceItem := &types.MenuItem{ viewDivergenceItem := &types.MenuItem{
LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream}, LabelColumns: []string{self.c.Tr.ViewDivergenceFromUpstream},
OnPress: func() error { OnPress: func() error {
@ -204,7 +208,7 @@ func (self *BranchesController) viewUpstreamOptions(selectedBranch *models.Branc
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(), upstream),
RefToShowDivergenceFrom: branch.FullUpstreamRefName(), RefToShowDivergenceFrom: branch.FullUpstreamRefName(),
Context: self.context(), Context: self.context(),
ShowBranchHeads: false, ShowBranchHeads: false,
@ -293,9 +297,6 @@ func (self *BranchesController) viewUpstreamOptions(selectedBranch *models.Branc
Key: 's', Key: 's',
} }
upstream := lo.Ternary(selectedBranch.RemoteBranchStoredLocally(),
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},
@ -332,7 +333,7 @@ func (self *BranchesController) viewUpstreamOptions(selectedBranch *models.Branc
LabelColumns: []string{upstreamRebaseOptions}, LabelColumns: []string{upstreamRebaseOptions},
OpensMenu: true, OpensMenu: true,
OnPress: func() error { OnPress: func() error {
if err := self.c.Helpers().MergeAndRebase.RebaseOntoRef(selectedBranch.ShortUpstreamRefName()); err != nil { if err := self.c.Helpers().MergeAndRebase.RebaseOntoRef(upstream); err != nil {
return err return err
} }
return nil return nil

View File

@ -142,6 +142,10 @@ func (self *Shell) NewBranchFrom(name string, from string) *Shell {
return self.RunCommand([]string{"git", "checkout", "-b", name, from}) return self.RunCommand([]string{"git", "checkout", "-b", name, from})
} }
func (self *Shell) RenameCurrentBranch(newName string) *Shell {
return self.RunCommand([]string{"git", "branch", "-m", newName})
}
func (self *Shell) Checkout(name string) *Shell { func (self *Shell) Checkout(name string) *Shell {
return self.RunCommand([]string{"git", "checkout", name}) return self.RunCommand([]string{"git", "checkout", name})
} }

View File

@ -16,8 +16,9 @@ var RebaseToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
EmptyCommit("ensure-master"). EmptyCommit("ensure-master").
EmptyCommit("to-be-added"). // <- this will only exist remotely EmptyCommit("to-be-added"). // <- this will only exist remotely
PushBranchAndSetUpstream("origin", "master"). PushBranchAndSetUpstream("origin", "master").
RenameCurrentBranch("master-local").
HardReset("HEAD~1"). HardReset("HEAD~1").
NewBranchFrom("base-branch", "master"). NewBranchFrom("base-branch", "master-local").
EmptyCommit("base-branch-commit"). EmptyCommit("base-branch-commit").
NewBranch("target"). NewBranch("target").
EmptyCommit("target-commit") EmptyCommit("target-commit")
@ -34,13 +35,13 @@ var RebaseToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
Lines( Lines(
Contains("target").IsSelected(), Contains("target").IsSelected(),
Contains("base-branch"), Contains("base-branch"),
Contains("master"), Contains("master-local"),
). ).
SelectNextItem(). SelectNextItem().
Lines( Lines(
Contains("target"), Contains("target"),
Contains("base-branch").IsSelected(), Contains("base-branch").IsSelected(),
Contains("master"), Contains("master-local"),
). ).
Press(keys.Branches.SetUpstream). Press(keys.Branches.SetUpstream).
Tap(func() { Tap(func() {
@ -58,7 +59,7 @@ var RebaseToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
Lines( Lines(
Contains("target"), Contains("target"),
Contains("base-branch"), Contains("base-branch"),
Contains("master").IsSelected(), Contains("master-local").IsSelected(),
). ).
Press(keys.Branches.SetUpstream). Press(keys.Branches.SetUpstream).
Tap(func() { Tap(func() {

View File

@ -19,6 +19,7 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
NewBranch("soft-branch"). NewBranch("soft-branch").
EmptyCommit("soft commit"). EmptyCommit("soft commit").
PushBranchAndSetUpstream("origin", "soft-branch"). PushBranchAndSetUpstream("origin", "soft-branch").
RenameCurrentBranch("soft-branch-local").
NewBranch("base"). NewBranch("base").
EmptyCommit("base-branch commit"). EmptyCommit("base-branch commit").
CreateFile("file-1", "content"). CreateFile("file-1", "content").
@ -33,7 +34,7 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
Focus(). Focus().
Lines( Lines(
Contains("base").IsSelected(), Contains("base").IsSelected(),
Contains("soft-branch"), Contains("soft-branch-local"),
Contains("hard-branch"), Contains("hard-branch"),
). ).
Press(keys.Branches.SetUpstream). Press(keys.Branches.SetUpstream).
@ -51,7 +52,7 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
SelectNextItem(). SelectNextItem().
Lines( Lines(
Contains("base"), Contains("base"),
Contains("soft-branch").IsSelected(), Contains("soft-branch-local").IsSelected(),
Contains("hard-branch"), Contains("hard-branch"),
). ).
Press(keys.Branches.SetUpstream). Press(keys.Branches.SetUpstream).
@ -80,7 +81,7 @@ var ResetToUpstream = NewIntegrationTest(NewIntegrationTestArgs{
Focus(). Focus().
Lines( Lines(
Contains("base"), Contains("base"),
Contains("soft-branch").IsSelected(), Contains("soft-branch-local").IsSelected(),
Contains("hard-branch"), Contains("hard-branch"),
). ).
NavigateToLine(Contains("hard-branch")). NavigateToLine(Contains("hard-branch")).