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

783 lines
22 KiB
Go
Raw Normal View History

2018-08-14 11:05:26 +02:00
package gui
import (
2019-02-24 04:51:52 +02:00
"strconv"
2020-03-26 14:48:11 +02:00
"sync"
2018-08-14 11:05:26 +02:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
2020-02-25 11:11:07 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/presentation"
"github.com/jesseduffield/lazygit/pkg/utils"
2018-08-14 11:05:26 +02:00
)
// list panel functions
func (gui *Gui) getSelectedCommit() *commands.Commit {
selectedLine := gui.State.Panels.Commits.SelectedLine
if selectedLine == -1 {
return nil
}
return gui.State.Commits[selectedLine]
}
2020-08-15 09:01:43 +02:00
func (gui *Gui) handleCommitSelect() error {
2019-02-25 13:11:35 +02:00
if gui.popupPanelFocused() {
return nil
}
2019-11-05 06:11:35 +02:00
// this probably belongs in an 'onFocus' function than a 'commit selected' function
if err := gui.refreshSecondaryPatchPanel(); err != nil {
return err
}
2020-08-15 09:01:43 +02:00
if _, err := gui.g.SetCurrentView("commits"); err != nil {
2019-02-25 13:11:35 +02:00
return err
}
2019-11-10 07:20:35 +02:00
2020-01-11 09:23:35 +02:00
state := gui.State.Panels.Commits
if state.SelectedLine > 290 && state.LimitCommits {
2020-01-11 09:23:35 +02:00
state.LimitCommits = false
go func() {
if err := gui.refreshCommitsWithLimit(); err != nil {
2020-03-28 02:47:54 +02:00
_ = gui.surfaceError(err)
2020-01-11 09:23:35 +02:00
}
}()
}
2019-11-10 07:20:35 +02:00
gui.getMainView().Title = "Patch"
gui.getSecondaryView().Title = "Custom Patch"
gui.handleEscapeLineByLinePanel()
2019-11-10 07:20:35 +02:00
commit := gui.getSelectedCommit()
if commit == nil {
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
return gui.newStringTask("main", gui.Tr.SLocalize("NoCommitsThisBranch"))
}
if gui.inDiffMode() {
return gui.renderDiff()
}
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
cmd := gui.OSCommand.ExecutableFromString(
2020-03-29 01:11:15 +02:00
gui.GitCommand.ShowCmdStr(commit.Sha, gui.State.FilterPath),
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
)
2020-03-01 03:30:48 +02:00
if err := gui.newPtyTask("main", cmd); err != nil {
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
gui.Log.Error(err)
}
allow fast flicking through any list panel Up till now our approach to rendering things like file diffs, branch logs, and commit patches, has been to run a command on the command line, wait for it to complete, take its output as a string, and then write that string to the main view (or secondary view e.g. when showing both staged and unstaged changes of a file). This has caused various issues. For once, if you are flicking through a list of files and an untracked file is particularly large, not only will this require lazygit to load that whole file into memory (or more accurately it's equally large diff), it also will slow down the UI thread while loading that file, and if the user continued down the list, the original command might eventually resolve and replace whatever the diff is for the newly selected file. Following what we've done in lazydocker, I've added a tasks package for when you need something done but you want it to cancel as soon as something newer comes up. Given this typically involves running a command to display to a view, I've added a viewBufferManagerMap struct to the Gui struct which allows you to define these tasks on a per-view basis. viewBufferManagers can run files and directly write the output to their view, meaning we no longer need to use so much memory. In the tasks package there is a helper method called NewCmdTask which takes a command, an initial amount of lines to read, and then runs that command, reads that number of lines, and allows for a readLines channel to tell it to read more lines. We read more lines when we scroll or resize the window. There is an adapter for the tasks package in a file called tasks_adapter which wraps the functions from the tasks package in gui-specific stuff like clearing the main view before starting the next task that wants to write to the main view. I've removed some small features as part of this work, namely the little headers that were at the top of the main view for some situations. For example, we no longer show the upstream of a selected branch. I want to re-introduce this in the future, but I didn't want to make this tasks system too complicated, and in order to facilitate a header section in the main view we'd need to have a task that gets the upstream for the current branch, writes it to the header, then tells another task to write the branch log to the main view, but without clearing inbetween. So it would get messy. I'm thinking instead of having a separate 'header' view atop the main view to render that kind of thing (which can happen in another PR) I've also simplified the 'git show' to just call 'git show' and not do anything fancy when it comes to merge commits. I considered using this tasks approach whenever we write to a view. The only thing is that the renderString method currently resets the origin of a view and I don't want to lose that. So I've left some in there that I consider harmless, but we should probably be just using tasks now for all rendering, even if it's just strings we can instantly make.
2020-01-11 05:54:59 +02:00
return nil
}
// during startup, the bottleneck is fetching the reflog entries. We need these
// on startup to sort the branches by recency. So we have two phases: INITIAL, and COMPLETE.
// In the initial phase we don't get any reflog commits, but we asynchronously get them
// and refresh the branches after that
func (gui *Gui) refreshReflogCommitsConsideringStartup() {
switch gui.State.StartupStage {
case INITIAL:
go func() {
2020-03-28 02:47:54 +02:00
_ = gui.refreshReflogCommits()
gui.refreshBranches()
gui.State.StartupStage = COMPLETE
}()
case COMPLETE:
2020-03-28 02:47:54 +02:00
_ = gui.refreshReflogCommits()
}
}
// whenever we change commits, we should update branches because the upstream/downstream
// counts can change. Whenever we change branches we should probably also change commits
// e.g. in the case of switching branches.
2020-03-27 12:23:42 +02:00
func (gui *Gui) refreshCommits() error {
2020-03-26 14:48:11 +02:00
wg := sync.WaitGroup{}
wg.Add(2)
2020-03-26 14:48:11 +02:00
go func() {
gui.refreshReflogCommitsConsideringStartup()
2020-03-26 14:48:11 +02:00
gui.refreshBranches()
wg.Done()
}()
go func() {
2020-03-28 02:47:54 +02:00
_ = gui.refreshCommitsWithLimit()
2020-08-16 05:58:29 +02:00
if gui.g.CurrentView() == gui.getCommitFilesView() || (gui.currentContext().GetKey() == gui.Contexts.PatchBuilding.Context.GetKey()) {
2020-03-28 02:47:54 +02:00
_ = gui.refreshCommitFilesView()
}
2020-03-26 14:48:11 +02:00
wg.Done()
}()
2020-03-26 14:48:11 +02:00
wg.Wait()
2020-03-26 14:20:12 +02:00
2018-08-14 11:05:26 +02:00
return nil
2020-01-11 09:23:35 +02:00
}
func (gui *Gui) refreshCommitsWithLimit() error {
2020-03-29 04:56:03 +02:00
builder, err := commands.NewCommitListBuilder(gui.Log, gui.GitCommand, gui.OSCommand, gui.Tr, gui.State.CherryPickedCommits)
2020-01-11 09:23:35 +02:00
if err != nil {
return err
}
2020-03-29 01:11:15 +02:00
commits, err := builder.GetCommits(commands.GetCommitsOptions{Limit: gui.State.Panels.Commits.LimitCommits, FilterPath: gui.State.FilterPath})
2020-01-11 09:23:35 +02:00
if err != nil {
return err
}
gui.State.Commits = commits
if gui.getCommitsView().Context == "branch-commits" {
if err := gui.renderBranchCommitsWithSelection(); err != nil {
return err
}
}
return nil
2018-08-14 11:05:26 +02:00
}
// specific functions
2018-08-14 11:05:26 +02:00
func (gui *Gui) handleCommitSquashDown(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
if len(gui.State.Commits) <= 1 {
2020-03-28 02:47:54 +02:00
return gui.createErrorPanel(gui.Tr.SLocalize("YouNoCommitsToSquash"))
2018-08-14 11:05:26 +02:00
}
applied, err := gui.handleMidRebaseCommand("squash")
if err != nil {
return err
2018-08-14 11:05:26 +02:00
}
if applied {
return nil
2018-08-14 11:05:26 +02:00
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("Squash"),
prompt: gui.Tr.SLocalize("SureSquashThisCommit"),
handleConfirm: func() error {
return gui.WithWaitingStatus(gui.Tr.SLocalize("SquashingStatus"), func() error {
err := gui.GitCommand.InteractiveRebase(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, "squash")
return gui.handleGenericMergeCommandResult(err)
})
},
})
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleCommitFixup(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
if len(gui.State.Commits) <= 1 {
2020-03-28 02:47:54 +02:00
return gui.createErrorPanel(gui.Tr.SLocalize("YouNoCommitsToSquash"))
2018-08-14 11:05:26 +02:00
}
applied, err := gui.handleMidRebaseCommand("fixup")
if err != nil {
return err
2018-08-14 11:05:26 +02:00
}
if applied {
return nil
2018-08-14 11:05:26 +02:00
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("Fixup"),
prompt: gui.Tr.SLocalize("SureFixupThisCommit"),
handleConfirm: func() error {
return gui.WithWaitingStatus(gui.Tr.SLocalize("FixingStatus"), func() error {
err := gui.GitCommand.InteractiveRebase(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, "fixup")
return gui.handleGenericMergeCommandResult(err)
})
},
})
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) handleRenameCommit(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
applied, err := gui.handleMidRebaseCommand("reword")
if err != nil {
return err
}
if applied {
return nil
}
if gui.State.Panels.Commits.SelectedLine != 0 {
2020-03-28 02:47:54 +02:00
return gui.createErrorPanel(gui.Tr.SLocalize("OnlyRenameTopCommit"))
2018-08-14 11:05:26 +02:00
}
2020-08-16 11:31:55 +02:00
commit := gui.getSelectedCommit()
if commit == nil {
return nil
}
message, err := gui.GitCommand.GetCommitMessage(commit.Sha)
if err != nil {
return gui.surfaceError(err)
}
return gui.prompt(v, gui.Tr.SLocalize("renameCommit"), message, func(response string) error {
2020-08-15 08:36:39 +02:00
if err := gui.GitCommand.RenameCommit(response); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
2018-08-14 11:05:26 +02:00
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
2018-08-14 11:05:26 +02:00
})
}
func (gui *Gui) handleRenameCommitEditor(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
applied, err := gui.handleMidRebaseCommand("reword")
if err != nil {
return err
}
if applied {
return nil
}
subProcess, err := gui.GitCommand.RewordCommit(gui.State.Commits, gui.State.Panels.Commits.SelectedLine)
2019-02-18 12:29:43 +02:00
if err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
2019-02-18 12:29:43 +02:00
if subProcess != nil {
gui.SubProcess = subProcess
return gui.Errors.ErrSubProcess
2019-02-18 12:29:43 +02:00
}
return nil
}
// handleMidRebaseCommand sees if the selected commit is in fact a rebasing
// commit meaning you are trying to edit the todo file rather than actually
// begin a rebase. It then updates the todo file with that action
func (gui *Gui) handleMidRebaseCommand(action string) (bool, error) {
selectedCommit := gui.State.Commits[gui.State.Panels.Commits.SelectedLine]
if selectedCommit.Status != "rebasing" {
return false, nil
}
// for now we do not support setting 'reword' because it requires an editor
// and that means we either unconditionally wait around for the subprocess to ask for
// our input or we set a lazygit client as the EDITOR env variable and have it
// request us to edit the commit message when prompted.
if action == "reword" {
2020-03-28 02:47:54 +02:00
return true, gui.createErrorPanel(gui.Tr.SLocalize("rewordNotSupported"))
}
if err := gui.GitCommand.EditRebaseTodo(gui.State.Panels.Commits.SelectedLine, action); err != nil {
2020-03-28 02:47:54 +02:00
return false, gui.surfaceError(err)
}
// TODO: consider doing this in a way that is less expensive. We don't actually
// need to reload all the commits, just the TODO commits.
return true, gui.refreshSidePanels(refreshOptions{scope: []int{COMMITS}})
}
func (gui *Gui) handleCommitDelete(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
applied, err := gui.handleMidRebaseCommand("drop")
if err != nil {
return err
}
if applied {
return nil
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("DeleteCommitTitle"),
prompt: gui.Tr.SLocalize("DeleteCommitPrompt"),
handleConfirm: func() error {
return gui.WithWaitingStatus(gui.Tr.SLocalize("DeletingStatus"), func() error {
err := gui.GitCommand.InteractiveRebase(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, "drop")
return gui.handleGenericMergeCommandResult(err)
})
},
})
}
func (gui *Gui) handleCommitMoveDown(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
index := gui.State.Panels.Commits.SelectedLine
selectedCommit := gui.State.Commits[index]
if selectedCommit.Status == "rebasing" {
if gui.State.Commits[index+1].Status != "rebasing" {
return nil
}
if err := gui.GitCommand.MoveTodoDown(index); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
gui.State.Panels.Commits.SelectedLine++
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI, scope: []int{COMMITS, BRANCHES}})
}
2019-03-03 07:11:20 +02:00
return gui.WithWaitingStatus(gui.Tr.SLocalize("MovingStatus"), func() error {
err := gui.GitCommand.MoveCommitDown(gui.State.Commits, index)
if err == nil {
gui.State.Panels.Commits.SelectedLine++
}
return gui.handleGenericMergeCommandResult(err)
})
}
func (gui *Gui) handleCommitMoveUp(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
index := gui.State.Panels.Commits.SelectedLine
if index == 0 {
return nil
}
selectedCommit := gui.State.Commits[index]
if selectedCommit.Status == "rebasing" {
if err := gui.GitCommand.MoveTodoDown(index - 1); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
gui.State.Panels.Commits.SelectedLine--
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI, scope: []int{COMMITS, BRANCHES}})
}
2019-03-03 07:11:20 +02:00
return gui.WithWaitingStatus(gui.Tr.SLocalize("MovingStatus"), func() error {
err := gui.GitCommand.MoveCommitDown(gui.State.Commits, index-1)
if err == nil {
gui.State.Panels.Commits.SelectedLine--
}
return gui.handleGenericMergeCommandResult(err)
})
}
func (gui *Gui) handleCommitEdit(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
applied, err := gui.handleMidRebaseCommand("edit")
if err != nil {
return err
}
if applied {
return nil
}
2019-03-03 07:11:20 +02:00
return gui.WithWaitingStatus(gui.Tr.SLocalize("RebasingStatus"), func() error {
err = gui.GitCommand.InteractiveRebase(gui.State.Commits, gui.State.Panels.Commits.SelectedLine, "edit")
return gui.handleGenericMergeCommandResult(err)
})
}
func (gui *Gui) handleCommitAmendTo(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("AmendCommitTitle"),
prompt: gui.Tr.SLocalize("AmendCommitPrompt"),
handleConfirm: func() error {
return gui.WithWaitingStatus(gui.Tr.SLocalize("AmendingStatus"), func() error {
err := gui.GitCommand.AmendTo(gui.State.Commits[gui.State.Panels.Commits.SelectedLine].Sha)
return gui.handleGenericMergeCommandResult(err)
})
},
})
}
func (gui *Gui) handleCommitPick(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
applied, err := gui.handleMidRebaseCommand("pick")
if err != nil {
return err
}
if applied {
return nil
}
// at this point we aren't actually rebasing so we will interpret this as an
// attempt to pull. We might revoke this later after enabling configurable keybindings
return gui.handlePullFiles(g, v)
}
func (gui *Gui) handleCommitRevert(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
if err := gui.GitCommand.Revert(gui.State.Commits[gui.State.Panels.Commits.SelectedLine].Sha); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
}
gui.State.Panels.Commits.SelectedLine++
return gui.refreshSidePanels(refreshOptions{mode: BLOCK_UI, scope: []int{COMMITS, BRANCHES}})
}
2019-02-24 04:51:52 +02:00
func (gui *Gui) handleCopyCommit(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
2019-02-24 04:51:52 +02:00
// get currently selected commit, add the sha to state.
commit := gui.State.Commits[gui.State.Panels.Commits.SelectedLine]
2019-02-24 04:51:52 +02:00
// we will un-copy it if it's already copied
for index, cherryPickedCommit := range gui.State.CherryPickedCommits {
if commit.Sha == cherryPickedCommit.Sha {
gui.State.CherryPickedCommits = append(gui.State.CherryPickedCommits[0:index], gui.State.CherryPickedCommits[index+1:]...)
return gui.renderBranchCommitsWithSelection()
2019-02-24 04:51:52 +02:00
}
}
gui.addCommitToCherryPickedCommits(gui.State.Panels.Commits.SelectedLine)
return gui.renderBranchCommitsWithSelection()
2019-02-24 04:51:52 +02:00
}
func (gui *Gui) cherryPickedCommitShaMap() map[string]bool {
commitShaMap := map[string]bool{}
for _, commit := range gui.State.CherryPickedCommits {
commitShaMap[commit.Sha] = true
}
return commitShaMap
}
func (gui *Gui) addCommitToCherryPickedCommits(index int) {
commitShaMap := gui.cherryPickedCommitShaMap()
commitShaMap[gui.State.Commits[index].Sha] = true
2019-02-24 04:51:52 +02:00
newCommits := []*commands.Commit{}
2019-02-24 04:51:52 +02:00
for _, commit := range gui.State.Commits {
if commitShaMap[commit.Sha] {
// duplicating just the things we need to put in the rebase TODO list
newCommits = append(newCommits, &commands.Commit{Name: commit.Name, Sha: commit.Sha})
2019-02-24 04:51:52 +02:00
}
}
gui.State.CherryPickedCommits = newCommits
2019-02-24 04:51:52 +02:00
}
func (gui *Gui) handleCopyCommitRange(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
commitShaMap := gui.cherryPickedCommitShaMap()
2019-02-24 04:51:52 +02:00
// find the last commit that is copied that's above our position
// if there are none, startIndex = 0
startIndex := 0
for index, commit := range gui.State.Commits[0:gui.State.Panels.Commits.SelectedLine] {
if commitShaMap[commit.Sha] {
2019-02-24 04:51:52 +02:00
startIndex = index
}
}
gui.Log.Info("commit copy start index: " + strconv.Itoa(startIndex))
for index := startIndex; index <= gui.State.Panels.Commits.SelectedLine; index++ {
gui.addCommitToCherryPickedCommits(index)
2019-02-24 04:51:52 +02:00
}
return gui.renderBranchCommitsWithSelection()
2019-02-24 04:51:52 +02:00
}
// HandlePasteCommits begins a cherry-pick rebase with the commits the user has copied
func (gui *Gui) HandlePasteCommits(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("CherryPick"),
prompt: gui.Tr.SLocalize("SureCherryPick"),
handleConfirm: func() error {
return gui.WithWaitingStatus(gui.Tr.SLocalize("CherryPickingStatus"), func() error {
err := gui.GitCommand.CherryPickCommits(gui.State.CherryPickedCommits)
return gui.handleGenericMergeCommandResult(err)
})
},
})
2019-02-24 04:51:52 +02:00
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleSwitchToCommitFilesPanel() error {
if err := gui.refreshCommitFilesView(); err != nil {
return err
}
2020-08-16 05:58:29 +02:00
return gui.switchContext(gui.Contexts.BranchCommits.Files.Context)
}
2019-03-28 11:58:34 +02:00
func (gui *Gui) hasCommit(commits []*commands.Commit, target string) (int, bool) {
for idx, commit := range commits {
if commit.Sha == target {
return idx, true
}
}
return -1, false
}
func (gui *Gui) unchooseCommit(commits []*commands.Commit, i int) []*commands.Commit {
return append(commits[:i], commits[i+1:]...)
}
2019-04-07 03:35:34 +02:00
func (gui *Gui) handleCreateFixupCommit(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
commit := gui.getSelectedCommit()
2019-04-07 03:35:34 +02:00
if commit == nil {
return nil
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("CreateFixupCommit"),
prompt: gui.Tr.TemplateLocalize(
"SureCreateFixupCommit",
Teml{
"commit": commit.Sha,
},
),
handleConfirm: func() error {
if err := gui.GitCommand.CreateFixupCommit(commit.Sha); err != nil {
return gui.surfaceError(err)
}
2019-04-07 03:35:34 +02:00
2020-08-15 08:36:39 +02:00
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
},
})
2019-04-07 03:35:34 +02:00
}
func (gui *Gui) handleSquashAllAboveFixupCommits(g *gocui.Gui, v *gocui.View) error {
2020-03-29 01:11:15 +02:00
if ok, err := gui.validateNotInFilterMode(); err != nil || !ok {
return err
}
commit := gui.getSelectedCommit()
2019-04-07 03:35:34 +02:00
if commit == nil {
return nil
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: v,
returnFocusOnClose: true,
title: gui.Tr.SLocalize("SquashAboveCommits"),
prompt: gui.Tr.TemplateLocalize(
"SureSquashAboveCommits",
Teml{
"commit": commit.Sha,
},
),
handleConfirm: func() error {
return gui.WithWaitingStatus(gui.Tr.SLocalize("SquashingStatus"), func() error {
err := gui.GitCommand.SquashAllAboveFixupCommits(commit.Sha)
return gui.handleGenericMergeCommandResult(err)
})
2019-04-07 03:35:34 +02:00
},
2020-08-15 08:36:39 +02:00
})
2019-04-07 03:35:34 +02:00
}
2019-11-18 00:38:36 +02:00
func (gui *Gui) handleTagCommit(g *gocui.Gui, v *gocui.View) error {
// TODO: bring up menu asking if you want to make a lightweight or annotated tag
// if annotated, switch to a subprocess to create the message
commit := gui.getSelectedCommit()
2019-11-18 00:38:36 +02:00
if commit == nil {
return nil
}
return gui.handleCreateLightweightTag(commit.Sha)
}
func (gui *Gui) handleCreateLightweightTag(commitSha string) error {
2020-08-15 08:38:16 +02:00
return gui.prompt(gui.getCommitsView(), gui.Tr.SLocalize("TagNameTitle"), "", func(response string) error {
2020-08-15 08:36:39 +02:00
if err := gui.GitCommand.CreateLightweightTag(response, commitSha); err != nil {
2020-03-28 02:47:54 +02:00
return gui.surfaceError(err)
2019-11-18 00:38:36 +02:00
}
return gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []int{COMMITS, TAGS}})
2019-11-18 00:38:36 +02:00
})
}
2020-01-07 11:24:10 +02:00
func (gui *Gui) handleCheckoutCommit(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit()
2020-01-07 11:24:10 +02:00
if commit == nil {
2020-01-09 13:08:28 +02:00
return nil
2020-01-07 11:24:10 +02:00
}
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-15 08:36:39 +02:00
returnToView: gui.getCommitsView(),
returnFocusOnClose: true,
title: gui.Tr.SLocalize("checkoutCommit"),
prompt: gui.Tr.SLocalize("SureCheckoutThisCommit"),
handleConfirm: func() error {
return gui.handleCheckoutRef(commit.Sha, handleCheckoutRefOptions{})
},
})
2020-01-07 11:24:10 +02:00
}
2020-01-09 12:34:17 +02:00
func (gui *Gui) renderBranchCommitsWithSelection() error {
commitsView := gui.getCommitsView()
gui.refreshSelectedLine(&gui.State.Panels.Commits.SelectedLine, len(gui.State.Commits))
displayStrings := presentation.GetCommitListDisplayStrings(gui.State.Commits, gui.State.ScreenMode != SCREEN_NORMAL, gui.cherryPickedCommitShaMap(), gui.State.Diff.Ref)
2020-02-25 11:11:07 +02:00
gui.renderDisplayStrings(commitsView, displayStrings)
2020-01-09 12:34:17 +02:00
if gui.g.CurrentView() == commitsView && commitsView.Context == "branch-commits" {
2020-08-15 09:01:43 +02:00
if err := gui.handleCommitSelect(); err != nil {
2020-01-09 12:34:17 +02:00
return err
}
}
return nil
}
func (gui *Gui) onCommitsTabClick(tabIndex int) error {
contexts := []string{"branch-commits", "reflog-commits"}
commitsView := gui.getCommitsView()
commitsView.TabIndex = tabIndex
return gui.switchCommitsPanelContext(contexts[tabIndex])
}
func (gui *Gui) switchCommitsPanelContext(context string) error {
commitsView := gui.getCommitsView()
commitsView.Context = context
2020-03-09 02:34:10 +02:00
if err := gui.onSearchEscape(); err != nil {
return err
}
2020-01-09 12:34:17 +02:00
contextTabIndexMap := map[string]int{
"branch-commits": 0,
"reflog-commits": 1,
}
commitsView.TabIndex = contextTabIndexMap[context]
2020-02-25 11:55:36 +02:00
return gui.refreshCommitsViewWithSelection()
}
func (gui *Gui) refreshCommitsViewWithSelection() error {
commitsView := gui.getCommitsView()
switch commitsView.Context {
2020-01-09 12:34:17 +02:00
case "branch-commits":
return gui.renderBranchCommitsWithSelection()
case "reflog-commits":
return gui.renderReflogCommitsWithSelection()
}
return nil
}
func (gui *Gui) handleNextCommitsTab(g *gocui.Gui, v *gocui.View) error {
return gui.onCommitsTabClick(
utils.ModuloWithWrap(v.TabIndex+1, len(v.Tabs)),
)
}
func (gui *Gui) handlePrevCommitsTab(g *gocui.Gui, v *gocui.View) error {
return gui.onCommitsTabClick(
utils.ModuloWithWrap(v.TabIndex-1, len(v.Tabs)),
)
}
2020-02-15 23:59:48 +02:00
func (gui *Gui) handleCreateCommitResetMenu(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit()
2020-02-15 23:59:48 +02:00
if commit == nil {
2020-03-28 02:47:54 +02:00
return gui.createErrorPanel(gui.Tr.SLocalize("NoCommitsThisBranch"))
2020-02-15 23:59:48 +02:00
}
return gui.createResetMenu(commit.Sha)
}
2020-02-23 13:00:42 +02:00
func (gui *Gui) handleOpenSearchForCommitsPanel(g *gocui.Gui, v *gocui.View) error {
// we usually lazyload these commits but now that we're searching we need to load them now
if gui.State.Panels.Commits.LimitCommits {
gui.State.Panels.Commits.LimitCommits = false
if err := gui.refreshSidePanels(refreshOptions{mode: ASYNC, scope: []int{COMMITS}}); err != nil {
2020-02-23 13:00:42 +02:00
return err
}
}
return gui.handleOpenSearch(gui.g, v)
}
func (gui *Gui) handleResetCherryPick(g *gocui.Gui, v *gocui.View) error {
gui.State.CherryPickedCommits = []*commands.Commit{}
return gui.renderBranchCommitsWithSelection()
}
2020-03-28 04:57:16 +02:00
func (gui *Gui) handleGotoBottomForCommitsPanel(g *gocui.Gui, v *gocui.View) error {
// we usually lazyload these commits but now that we're searching we need to load them now
if gui.State.Panels.Commits.LimitCommits {
gui.State.Panels.Commits.LimitCommits = false
if err := gui.refreshSidePanels(refreshOptions{mode: SYNC, scope: []int{COMMITS}}); err != nil {
return err
}
}
for _, view := range gui.getListViews() {
2020-08-16 02:05:45 +02:00
if view.ViewName == "commits" {
2020-03-28 04:57:16 +02:00
return view.handleGotoBottom(g, v)
}
}
return nil
}
2020-04-15 12:30:24 +02:00
func (gui *Gui) handleClipboardCopyCommit(g *gocui.Gui, v *gocui.View) error {
commit := gui.getSelectedCommit()
if commit == nil {
return nil
}
return gui.OSCommand.CopyToClipboard(commit.Sha)
}
func (gui *Gui) handleNewBranchOffCommit() error {
commit := gui.getSelectedCommit()
if commit == nil {
return nil
}
message := gui.Tr.TemplateLocalize(
"NewBranchNameBranchOff",
Teml{
"branchName": commit.NameWithSha(),
},
)
return gui.prompt(gui.getCommitsView(), message, "", func(response string) error {
if err := gui.GitCommand.NewBranch(response, commit.Sha); err != nil {
return err
}
gui.State.Panels.Commits.SelectedLine = 0
gui.State.Panels.Branches.SelectedLine = 0
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
})
}