1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-26 05:37:18 +02:00

genericise creating new branches off things

This commit is contained in:
Jesse Duffield 2020-08-22 09:55:49 +10:00
parent f858c8e750
commit f63ec38aae
6 changed files with 71 additions and 75 deletions

View File

@ -221,22 +221,6 @@ func (gui *Gui) getCheckedOutBranch() *commands.Branch {
return gui.State.Branches[0] return gui.State.Branches[0]
} }
func (gui *Gui) handleNewBranch(g *gocui.Gui, v *gocui.View) error {
branch := gui.getSelectedBranch()
if branch == nil {
return nil
}
message := gui.Tr.TemplateLocalize(
"NewBranchNameBranchOff",
Teml{
"branchName": branch.Name,
},
)
return gui.prompt(v, message, "", func(response string) error {
return gui.createNewBranchWithName(response)
})
}
func (gui *Gui) createNewBranchWithName(newBranchName string) error { func (gui *Gui) createNewBranchWithName(newBranchName string) error {
branch := gui.getSelectedBranch() branch := gui.getSelectedBranch()
if branch == nil { if branch == nil {

View File

@ -675,25 +675,35 @@ func (gui *Gui) handleClipboardCopyCommit(g *gocui.Gui, v *gocui.View) error {
return gui.OSCommand.CopyToClipboard(commit.Sha) return gui.OSCommand.CopyToClipboard(commit.Sha)
} }
func (gui *Gui) handleNewBranchOffCommit() error { func (gui *Gui) handleNewBranchOffCurrentItem() error {
commit := gui.getSelectedLocalCommit() context := gui.currentSideContext()
if commit == nil {
return nil itemId := context.GetSelectedItemId()
}
message := gui.Tr.TemplateLocalize( message := gui.Tr.TemplateLocalize(
"NewBranchNameBranchOff", "NewBranchNameBranchOff",
Teml{ Teml{
"branchName": commit.NameWithSha(), "branchName": itemId, // TODO: add 'long name' field on ListItem interface (right now we just get an ugly commit SHA for commits)
}, },
) )
return gui.prompt(gui.getCurrentSideView(), message, "", func(response string) error { return gui.prompt(gui.getCurrentSideView(), message, "", func(response string) error {
if err := gui.GitCommand.NewBranch(response, commit.Sha); err != nil { if err := gui.GitCommand.NewBranch(response, itemId); err != nil {
return err return err
} }
gui.State.Panels.Commits.SelectedLineIdx = 0
gui.State.Panels.Branches.SelectedLineIdx = 0 // if we're currently in the branch commits context then the selected commit
// is about to go to the top of the list
if context.GetKey() == BRANCH_COMMITS_CONTEXT_KEY {
context.GetPanelState().SetSelectedLineIdx(0)
}
if context.GetKey() != gui.Contexts.Branches.Context.GetKey() {
if err := gui.switchContext(gui.Contexts.Branches.Context); err != nil {
return err
}
gui.State.Panels.Branches.SelectedLineIdx = 0
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC}) return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}) })

View File

@ -46,6 +46,7 @@ type Context interface {
SetWindowName(string) SetWindowName(string)
GetKey() string GetKey() string
GetSelectedItemId() string GetSelectedItemId() string
GetSelectedItem() ListItem
SetParentContext(Context) SetParentContext(Context)
GetParentContext() Context GetParentContext() Context
} }
@ -63,6 +64,10 @@ func (c BasicContext) SetWindowName(windowName string) {
panic("can't set window name on basic context") panic("can't set window name on basic context")
} }
func (c BasicContext) GetSelectedItem() ListItem {
return nil
}
func (c BasicContext) GetWindowName() string { func (c BasicContext) GetWindowName() string {
// TODO: fix this up // TODO: fix this up
return c.GetViewName() return c.GetViewName()
@ -677,3 +682,14 @@ func (gui *Gui) rerenderView(viewName string) error {
return context.HandleRender() return context.HandleRender()
} }
func (gui *Gui) getCurrentSideView() *gocui.View {
currentSideContext := gui.currentSideContext()
if currentSideContext == nil {
return nil
}
view, _ := gui.g.View(currentSideContext.GetViewName())
return view
}

View File

@ -496,7 +496,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "branches", ViewName: "branches",
Contexts: []string{LOCAL_BRANCHES_CONTEXT_KEY}, Contexts: []string{LOCAL_BRANCHES_CONTEXT_KEY},
Key: gui.getKey("universal.new"), Key: gui.getKey("universal.new"),
Handler: gui.handleNewBranch, Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem),
Description: gui.Tr.SLocalize("newBranch"), Description: gui.Tr.SLocalize("newBranch"),
}, },
{ {
@ -801,7 +801,7 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Contexts: []string{BRANCH_COMMITS_CONTEXT_KEY}, Contexts: []string{BRANCH_COMMITS_CONTEXT_KEY},
Key: gui.getKey("universal.new"), Key: gui.getKey("universal.new"),
Modifier: gocui.ModNone, Modifier: gocui.ModNone,
Handler: gui.wrappedHandler(gui.handleNewBranchOffCommit), Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem),
Description: gui.Tr.SLocalize("createNewBranchFromCommit"), Description: gui.Tr.SLocalize("createNewBranchFromCommit"),
}, },
{ {
@ -860,6 +860,13 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.wrappedHandler(gui.handleCreateSubCommitResetMenu), Handler: gui.wrappedHandler(gui.handleCreateSubCommitResetMenu),
Description: gui.Tr.SLocalize("viewResetOptions"), Description: gui.Tr.SLocalize("viewResetOptions"),
}, },
{
ViewName: "branches",
Contexts: []string{SUB_COMMITS_CONTEXT_KEY},
Key: gui.getKey("universal.new"),
Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem),
Description: gui.Tr.SLocalize("newBranch"),
},
{ {
ViewName: "stash", ViewName: "stash",
Key: gui.getKey("universal.goInto"), Key: gui.getKey("universal.goInto"),
@ -1358,10 +1365,9 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
ViewName: "branches", ViewName: "branches",
Contexts: []string{REMOTE_BRANCHES_CONTEXT_KEY}, Contexts: []string{REMOTE_BRANCHES_CONTEXT_KEY},
Key: gui.getKey("universal.new"), Key: gui.getKey("universal.new"),
Handler: gui.handleNewBranchOffRemote, Handler: gui.wrappedHandler(gui.handleNewBranchOffCurrentItem),
Description: gui.Tr.SLocalize("newBranch"), Description: gui.Tr.SLocalize("newBranch"),
}, },
{ {
ViewName: "branches", ViewName: "branches",
Contexts: []string{REMOTE_BRANCHES_CONTEXT_KEY}, Contexts: []string{REMOTE_BRANCHES_CONTEXT_KEY},

View File

@ -21,7 +21,7 @@ type ListContext struct {
OnFocus func() error OnFocus func() error
OnFocusLost func() error OnFocusLost func() error
OnClickSelectedItem func() error OnClickSelectedItem func() error
GetSelectedItem func() ListItem SelectedItem func() ListItem
GetPanelState func() IListPanelState GetPanelState func() IListPanelState
Gui *Gui Gui *Gui
@ -36,6 +36,10 @@ type ListItem interface {
ID() string ID() string
} }
func (lc *ListContext) GetSelectedItem() ListItem {
return lc.SelectedItem()
}
func (lc *ListContext) GetContains() int { func (lc *ListContext) GetContains() int {
return lc.Contains return lc.Contains
} }
@ -64,7 +68,7 @@ func (lc *ListContext) GetParentContext() Context {
} }
func (lc *ListContext) GetSelectedItemId() string { func (lc *ListContext) GetSelectedItemId() string {
item := lc.GetSelectedItem() item := lc.SelectedItem()
if item == nil { if item == nil {
return "" return ""
@ -269,8 +273,8 @@ func (gui *Gui) filesListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Diff.Ref) return presentation.GetFileListDisplayStrings(gui.State.Files, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_NOTHING, Contains: CONTAINS_NOTHING,
GetSelectedItem: func() ListItem { return gui.getSelectedFile() }, SelectedItem: func() ListItem { return gui.getSelectedFile() },
} }
} }
@ -287,8 +291,8 @@ func (gui *Gui) branchesListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref) return presentation.GetBranchListDisplayStrings(gui.State.Branches, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
GetSelectedItem: func() ListItem { return gui.getSelectedBranch() }, SelectedItem: func() ListItem { return gui.getSelectedBranch() },
} }
} }
@ -306,8 +310,8 @@ func (gui *Gui) remotesListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Diff.Ref) return presentation.GetRemoteListDisplayStrings(gui.State.Remotes, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_BRANCHES, Contains: CONTAINS_BRANCHES,
GetSelectedItem: func() ListItem { return gui.getSelectedRemote() }, SelectedItem: func() ListItem { return gui.getSelectedRemote() },
} }
} }
@ -324,8 +328,8 @@ func (gui *Gui) remoteBranchesListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Diff.Ref) return presentation.GetRemoteBranchListDisplayStrings(gui.State.RemoteBranches, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
GetSelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() }, SelectedItem: func() ListItem { return gui.getSelectedRemoteBranch() },
} }
} }
@ -342,8 +346,8 @@ func (gui *Gui) tagsListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Diff.Ref) return presentation.GetTagListDisplayStrings(gui.State.Tags, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
GetSelectedItem: func() ListItem { return gui.getSelectedTag() }, SelectedItem: func() ListItem { return gui.getSelectedTag() },
} }
} }
@ -361,8 +365,8 @@ func (gui *Gui) branchCommitsListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref) return presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref)
}, },
Contains: CONTAINS_FILES, Contains: CONTAINS_FILES,
GetSelectedItem: func() ListItem { return gui.getSelectedLocalCommit() }, SelectedItem: func() ListItem { return gui.getSelectedLocalCommit() },
} }
} }
@ -379,8 +383,8 @@ func (gui *Gui) reflogCommitsListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref) return presentation.GetReflogCommitListDisplayStrings(gui.State.FilteredReflogCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_FILES, Contains: CONTAINS_FILES,
GetSelectedItem: func() ListItem { return gui.getSelectedReflogCommit() }, SelectedItem: func() ListItem { return gui.getSelectedReflogCommit() },
} }
} }
@ -397,8 +401,8 @@ func (gui *Gui) subCommitsListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref) return presentation.GetCommitListDisplayStrings(gui.State.SubCommits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref)
}, },
Contains: CONTAINS_COMMITS, Contains: CONTAINS_COMMITS,
GetSelectedItem: func() ListItem { return gui.getSelectedSubCommit() }, SelectedItem: func() ListItem { return gui.getSelectedSubCommit() },
} }
} }
@ -415,8 +419,8 @@ func (gui *Gui) stashListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Diff.Ref) return presentation.GetStashEntryListDisplayStrings(gui.State.StashEntries, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_FILES, Contains: CONTAINS_FILES,
GetSelectedItem: func() ListItem { return gui.getSelectedStashEntry() }, SelectedItem: func() ListItem { return gui.getSelectedStashEntry() },
} }
} }
@ -434,8 +438,8 @@ func (gui *Gui) commitFilesListContext() *ListContext {
GetDisplayStrings: func() [][]string { GetDisplayStrings: func() [][]string {
return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Diff.Ref) return presentation.GetCommitFileListDisplayStrings(gui.State.CommitFiles, gui.State.Diff.Ref)
}, },
Contains: CONTAINS_NOTHING, Contains: CONTAINS_NOTHING,
GetSelectedItem: func() ListItem { return gui.getSelectedCommitFile() }, SelectedItem: func() ListItem { return gui.getSelectedCommitFile() },
} }
} }

View File

@ -122,27 +122,3 @@ func (gui *Gui) handleCreateResetToRemoteBranchMenu(g *gocui.Gui, v *gocui.View)
return gui.createResetMenu(selectedBranch.FullName()) return gui.createResetMenu(selectedBranch.FullName())
} }
func (gui *Gui) handleNewBranchOffRemote(g *gocui.Gui, v *gocui.View) error {
branch := gui.getSelectedRemoteBranch()
if branch == nil {
return nil
}
message := gui.Tr.TemplateLocalize(
"NewBranchNameBranchOff",
Teml{
"branchName": branch.FullName(),
},
)
return gui.prompt(v, message, branch.FullName(), func(response string) error {
if err := gui.GitCommand.NewBranch(response, branch.FullName()); err != nil {
return gui.surfaceError(err)
}
gui.State.Panels.Branches.SelectedLineIdx = 0
if err := gui.switchContext(gui.Contexts.Branches.Context); err != nil {
return err
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
})
}