1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/pkg/gui/sub_commits_panel.go

133 lines
3.4 KiB
Go
Raw Normal View History

2020-08-22 00:49:02 +02:00
package gui
import (
2021-12-30 04:35:10 +02:00
"github.com/jesseduffield/lazygit/pkg/commands/loaders"
"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/gui/controllers"
"github.com/jesseduffield/lazygit/pkg/gui/types"
2020-08-22 00:49:02 +02:00
)
// list panel functions
2020-09-29 10:36:54 +02:00
func (gui *Gui) getSelectedSubCommit() *models.Commit {
2020-08-22 00:49:02 +02:00
selectedLine := gui.State.Panels.SubCommits.SelectedLineIdx
2022-01-31 13:11:34 +02:00
commits := gui.State.Model.SubCommits
2020-08-22 00:49:02 +02:00
if selectedLine == -1 || len(commits) == 0 {
return nil
}
return commits[selectedLine]
}
func (gui *Gui) subCommitsRenderToMain() error {
2020-08-22 00:49:02 +02:00
commit := gui.getSelectedSubCommit()
var task updateTask
if commit == nil {
2021-04-04 15:51:59 +02:00
task = NewRenderStringTask("No commits")
2020-08-22 00:49:02 +02:00
} else {
cmdObj := gui.git.Commit.ShowCmdObj(commit.Sha, gui.State.Modes.Filtering.GetPath())
2020-08-22 00:49:02 +02:00
2021-12-07 12:59:36 +02:00
task = NewRunPtyTask(cmdObj.GetCmd())
2020-08-22 00:49:02 +02:00
}
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
2020-08-22 00:49:02 +02:00
main: &viewUpdateOpts{
title: "Commit",
task: task,
},
})
}
func (gui *Gui) handleCheckoutSubCommit() error {
2020-08-22 00:49:02 +02:00
commit := gui.getSelectedSubCommit()
if commit == nil {
return nil
}
2022-01-29 10:09:20 +02:00
err := gui.c.Ask(types.AskOpts{
Title: gui.c.Tr.LcCheckoutCommit,
Prompt: gui.c.Tr.SureCheckoutThisCommit,
2022-01-28 11:44:36 +02:00
HandleConfirm: func() error {
gui.c.LogAction(gui.c.Tr.Actions.CheckoutCommit)
2022-01-31 13:11:34 +02:00
return gui.helpers.Refs.CheckoutRef(commit.Sha, types.CheckoutRefOptions{})
2020-08-22 00:49:02 +02:00
},
})
if err != nil {
return err
}
gui.State.Contexts.SubCommits.GetPanelState().SetSelectedLineIdx(0)
2020-08-22 00:49:02 +02:00
return nil
}
func (gui *Gui) handleCreateSubCommitResetMenu() error {
commit := gui.getSelectedSubCommit()
2022-01-31 13:11:34 +02:00
return gui.helpers.Refs.CreateGitResetMenu(commit.Sha)
2020-08-22 00:49:02 +02:00
}
func (gui *Gui) handleViewSubCommitFiles() error {
commit := gui.getSelectedSubCommit()
if commit == nil {
return nil
}
return gui.SwitchToCommitFilesContext(controllers.SwitchToCommitFilesContextOpts{
RefName: commit.Sha,
CanRebase: false,
Context: gui.State.Contexts.SubCommits,
WindowName: "branches",
})
2020-08-22 00:49:02 +02:00
}
func (gui *Gui) switchToSubCommitsContext(refName string) error {
// need to populate my sub commits
commits, err := gui.git.Loaders.Commits.GetCommits(
2021-12-30 04:35:10 +02:00
loaders.GetCommitsOptions{
2020-08-22 00:49:02 +02:00
Limit: gui.State.Panels.Commits.LimitCommits,
2021-04-03 02:32:14 +02:00
FilterPath: gui.State.Modes.Filtering.GetPath(),
2020-08-22 00:49:02 +02:00
IncludeRebaseCommits: false,
RefName: refName,
},
)
if err != nil {
return err
}
2022-01-31 13:11:34 +02:00
gui.State.Model.SubCommits = commits
2020-08-22 00:49:02 +02:00
gui.State.Panels.SubCommits.refName = refName
gui.State.Contexts.SubCommits.GetPanelState().SetSelectedLineIdx(0)
2021-04-04 15:51:59 +02:00
gui.State.Contexts.SubCommits.SetParentContext(gui.currentSideListContext())
2020-08-22 00:49:02 +02:00
return gui.c.PushContext(gui.State.Contexts.SubCommits)
2020-08-22 00:49:02 +02:00
}
2022-01-30 11:03:08 +02:00
func (gui *Gui) handleNewBranchOffSubCommit() error {
commit := gui.getSelectedSubCommit()
if commit == nil {
return nil
}
2022-01-31 13:11:34 +02:00
return gui.helpers.Refs.NewBranch(commit.RefName(), commit.Description(), "")
2022-01-30 11:03:08 +02:00
}
func (gui *Gui) handleCopySubCommit() error {
commit := gui.getSelectedSubCommit()
if commit == nil {
return nil
}
2022-01-31 13:11:34 +02:00
return gui.helpers.CherryPick.Copy(commit, gui.State.Model.SubCommits, gui.State.Contexts.SubCommits)
2022-01-30 11:03:08 +02:00
}
func (gui *Gui) handleCopySubCommitRange() error {
// just doing this to ensure something is selected
commit := gui.getSelectedSubCommit()
if commit == nil {
return nil
}
2022-01-31 13:11:34 +02:00
return gui.helpers.CherryPick.CopyRange(gui.State.Contexts.SubCommits.GetPanelState().GetSelectedLineIdx(), gui.State.Model.SubCommits, gui.State.Contexts.SubCommits)
2022-01-30 11:03:08 +02:00
}