diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 557e6a8c0..bf9aa6646 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -270,8 +270,12 @@ func (c *GitCommand) Pull() error { } // Push push to a branch -func (c *GitCommand) Push(branchName string) error { - return c.OSCommand.RunCommand("git push -u origin " + branchName) +func (c *GitCommand) Push(branchName string, force bool) error { + forceFlag := "" + if force { + forceFlag = "--force-with-lease " + } + return c.OSCommand.RunCommand("git push " + forceFlag + "-u origin " + branchName) } // SquashPreviousTwoCommits squashes a commit down to the one below it diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index d614cf5ef..5791a9d15 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -360,21 +360,35 @@ func (gui *Gui) pullFiles(g *gocui.Gui, v *gocui.View) error { return nil } -func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error { - gui.createMessagePanel(g, v, "", gui.Tr.SLocalize("PushWait")) +func (gui *Gui) pushWithForceFlag(currentView *gocui.View, force bool) error { + if err := gui.createMessagePanel(gui.g, currentView, "", gui.Tr.SLocalize("PushWait")); err != nil { + return err + } go func() { branchName := gui.State.Branches[0].Name - if err := gui.GitCommand.Push(branchName); err != nil { - gui.createErrorPanel(g, err.Error()) + if err := gui.GitCommand.Push(branchName, force); err != nil { + _ = gui.createErrorPanel(gui.g, err.Error()) } else { - gui.closeConfirmationPrompt(g) - gui.refreshCommits(g) - gui.refreshStatus(g) + _ = gui.closeConfirmationPrompt(gui.g) + _ = gui.refreshCommits(gui.g) + _ = gui.refreshStatus(gui.g) } }() return nil } +func (gui *Gui) pushFiles(g *gocui.Gui, v *gocui.View) error { + // if we have pullables we'll ask if the user wants to force push + _, pullables := gui.GitCommand.UpstreamDifferenceCount() + if pullables == "?" || pullables == "0" { + return gui.pushWithForceFlag(v, false) + } + err := gui.createConfirmationPanel(g, nil, gui.Tr.SLocalize("ForcePush"), gui.Tr.SLocalize("ForcePushPrompt"), func(g *gocui.Gui, v *gocui.View) error { + return gui.pushWithForceFlag(v, true) + }, nil) + return err +} + func (gui *Gui) handleSwitchToMerge(g *gocui.Gui, v *gocui.View) error { mergeView, err := g.View("main") if err != nil { diff --git a/pkg/i18n/english.go b/pkg/i18n/english.go index bebe9b282..c0384136b 100644 --- a/pkg/i18n/english.go +++ b/pkg/i18n/english.go @@ -300,6 +300,12 @@ func addEnglish(i18nObject *i18n.Bundle) error { }, &i18n.Message{ ID: "EditConfig", Other: "edit config file", + }, &i18n.Message{ + ID: "ForcePush", + Other: "Force push", + }, &i18n.Message{ + ID: "ForcePushPrompt", + Other: "Your branch has diverged from the remote branch. Press 'esc' to cancel, or 'enter' to force push.", }, ) }