Skip to content

Commit

Permalink
support setting upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Nov 21, 2019
1 parent b42202e commit 07cbae4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pkg/commands/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,3 +1087,7 @@ func (c *GitCommand) IsHeadDetached() bool {
func (c *GitCommand) DeleteRemoteBranch(remoteName string, branchName string) error {
return c.OSCommand.RunCommand(fmt.Sprintf("git push %s --delete %s", remoteName, branchName))
}

func (c *GitCommand) SetBranchUpstream(remoteName string, remoteBranchName string, branchName string) error {
return c.OSCommand.RunCommand(fmt.Sprintf("git branch --set-upstream-to=%s/%s %s", remoteName, remoteBranchName, branchName))
}
16 changes: 12 additions & 4 deletions pkg/gui/branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,16 @@ func (gui *Gui) handleCheckoutByName(g *gocui.Gui, v *gocui.View) error {
return nil
}

func (gui *Gui) getCheckedOutBranch() *commands.Branch {
if len(gui.State.Branches) == 0 {
return nil
}

return gui.State.Branches[0]
}

func (gui *Gui) handleNewBranch(g *gocui.Gui, v *gocui.View) error {
branch := gui.State.Branches[0]
branch := gui.getCheckedOutBranch()
message := gui.Tr.TemplateLocalize(
"NewBranchNameBranchOff",
Teml{
Expand Down Expand Up @@ -243,7 +251,7 @@ func (gui *Gui) deleteBranch(g *gocui.Gui, v *gocui.View, force bool) error {
if selectedBranch == nil {
return nil
}
checkedOutBranch := gui.State.Branches[0]
checkedOutBranch := gui.getCheckedOutBranch()
if checkedOutBranch.Name == selectedBranch.Name {
return gui.createErrorPanel(g, gui.Tr.SLocalize("CantDeleteCheckOutBranch"))
}
Expand Down Expand Up @@ -280,7 +288,7 @@ func (gui *Gui) mergeBranchIntoCheckedOutBranch(branchName string) error {
if gui.GitCommand.IsHeadDetached() {
return gui.createErrorPanel(gui.g, "Cannot merge branch in detached head state. You might have checked out a commit directly or a remote branch, in which case you should checkout the local branch you want to be on")
}
checkedOutBranchName := gui.State.Branches[0].Name
checkedOutBranchName := gui.getCheckedOutBranch().Name
if checkedOutBranchName == branchName {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("CantMergeBranchIntoItself"))
}
Expand Down Expand Up @@ -310,7 +318,7 @@ func (gui *Gui) handleRebaseOntoLocalBranch(g *gocui.Gui, v *gocui.View) error {
}

func (gui *Gui) handleRebaseOntoBranch(selectedBranchName string) error {
checkedOutBranch := gui.State.Branches[0].Name
checkedOutBranch := gui.getCheckedOutBranch().Name
if selectedBranchName == checkedOutBranch {
return gui.createErrorPanel(gui.g, gui.Tr.SLocalize("CantRebaseOntoSelf"))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/files_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func (gui *Gui) pushWithForceFlag(g *gocui.Gui, v *gocui.View, force bool, upstr
}
go func() {
unamePassOpend := false
branchName := gui.State.Branches[0].Name
branchName := gui.getCheckedOutBranch().Name
err := gui.GitCommand.Push(branchName, force, upstream, func(passOrUname string) string {
unamePassOpend = true
return gui.waitForPassUname(g, v, passOrUname)
Expand Down
8 changes: 8 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,14 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.handleRebaseOntoRemoteBranch,
Description: gui.Tr.SLocalize("rebaseBranch"),
},
{
ViewName: "branches",
Contexts: []string{"remote-branches"},
Key: 'u',
Modifier: gocui.ModNone,
Handler: gui.handleSetBranchUpstream,
Description: gui.Tr.SLocalize("setUpstream"),
},
{
ViewName: "commits",
Key: gocui.MouseLeft,
Expand Down
21 changes: 21 additions & 0 deletions pkg/gui/remote_branches_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,24 @@ func (gui *Gui) handleRebaseOntoRemoteBranch(g *gocui.Gui, v *gocui.View) error
selectedBranchName := gui.getSelectedRemoteBranch().Name
return gui.handleRebaseOntoBranch(selectedBranchName)
}

func (gui *Gui) handleSetBranchUpstream(g *gocui.Gui, v *gocui.View) error {
selectedBranch := gui.getSelectedRemoteBranch()
checkedOutBranch := gui.getCheckedOutBranch()

message := gui.Tr.TemplateLocalize(
"SetUpstreamMessage",
Teml{
"checkedOut": checkedOutBranch.Name,
"selected": selectedBranch.RemoteName + "/" + selectedBranch.Name,
},
)

return gui.createConfirmationPanel(g, v, true, gui.Tr.SLocalize("SetUpstreamTitle"), message, func(*gocui.Gui, *gocui.View) error {
if err := gui.GitCommand.SetBranchUpstream(selectedBranch.RemoteName, selectedBranch.Name, checkedOutBranch.Name); err != nil {
return err
}

return gui.refreshSidePanels(gui.g)
}, nil)
}
9 changes: 9 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,15 @@ func addEnglish(i18nObject *i18n.Bundle) error {
}, &i18n.Message{
ID: "DeleteRemoteBranchMessage",
Other: "Are you sure you want to delete remote branch",
}, &i18n.Message{
ID: "setUpstream",
Other: "set as upstream of checked-out branch",
}, &i18n.Message{
ID: "SetUpstreamTitle",
Other: "Set upstream branch",
}, &i18n.Message{
ID: "SetUpstreamMessage",
Other: "Are you sure you want to set the upstream branch of '{{.checkedOut}}' to '{{.selected}}'",
},
)
}

0 comments on commit 07cbae4

Please sign in to comment.