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

343 lines
9.0 KiB
Go
Raw Normal View History

2018-08-14 11:05:26 +02:00
// though this panel is called the merge panel, it's really going to use the main panel. This may change in the future
package gui
import (
"bufio"
"fmt"
2018-08-14 11:05:26 +02:00
"io/ioutil"
"math"
"os"
2020-08-18 14:41:14 +02:00
"github.com/go-errors/errors"
2018-12-08 07:54:54 +02:00
"github.com/golang-collections/collections/stack"
2018-08-14 11:05:26 +02:00
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands"
2021-04-01 15:48:13 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/mergeconflicts"
2018-08-14 11:05:26 +02:00
)
2021-04-01 15:48:13 +02:00
func (gui *Gui) handleSelectTop() error {
return gui.withMergeConflictLock(func() error {
gui.takeOverMergeConflictScrolling()
2021-04-01 15:48:13 +02:00
gui.State.Panels.Merging.ConflictTop = true
return gui.refreshMergePanel()
})
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) handleSelectBottom() error {
return gui.withMergeConflictLock(func() error {
gui.takeOverMergeConflictScrolling()
2021-04-01 15:48:13 +02:00
gui.State.Panels.Merging.ConflictTop = false
return gui.refreshMergePanel()
})
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) handleSelectNextConflict() error {
return gui.withMergeConflictLock(func() error {
gui.takeOverMergeConflictScrolling()
2021-04-01 15:48:13 +02:00
if gui.State.Panels.Merging.ConflictIndex >= len(gui.State.Panels.Merging.Conflicts)-1 {
return nil
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
gui.State.Panels.Merging.ConflictIndex++
return gui.refreshMergePanel()
})
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) handleSelectPrevConflict() error {
return gui.withMergeConflictLock(func() error {
gui.takeOverMergeConflictScrolling()
2021-04-01 15:48:13 +02:00
if gui.State.Panels.Merging.ConflictIndex <= 0 {
return nil
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
gui.State.Panels.Merging.ConflictIndex--
return gui.refreshMergePanel()
})
2018-08-14 11:05:26 +02:00
}
2020-11-16 11:38:26 +02:00
func (gui *Gui) pushFileSnapshot() error {
2020-08-16 09:45:12 +02:00
gitFile := gui.getSelectedFile()
if gitFile == nil {
return nil
2018-08-14 11:05:26 +02:00
}
content, err := gui.GitCommand.CatFile(gitFile.Name)
if err != nil {
return err
}
2018-12-08 07:54:54 +02:00
gui.State.Panels.Merging.EditHistory.Push(content)
2018-08-14 11:05:26 +02:00
return nil
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) handlePopFileSnapshot() error {
2018-12-08 07:54:54 +02:00
if gui.State.Panels.Merging.EditHistory.Len() == 0 {
2018-08-14 11:05:26 +02:00
return nil
}
2018-12-08 07:54:54 +02:00
prevContent := gui.State.Panels.Merging.EditHistory.Pop().(string)
2020-08-16 09:45:12 +02:00
gitFile := gui.getSelectedFile()
if gitFile == nil {
return nil
2018-08-14 11:05:26 +02:00
}
2020-03-09 02:34:10 +02:00
if err := ioutil.WriteFile(gitFile.Name, []byte(prevContent), 0644); err != nil {
return err
}
return gui.refreshMergePanel()
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) handlePickHunk() error {
return gui.withMergeConflictLock(func() error {
conflict := gui.getCurrentConflict()
if conflict == nil {
return nil
}
gui.takeOverMergeConflictScrolling()
2021-04-01 15:48:13 +02:00
if err := gui.pushFileSnapshot(); err != nil {
return err
}
2020-03-09 02:34:10 +02:00
2021-04-01 15:48:13 +02:00
selection := mergeconflicts.BOTTOM
if gui.State.Panels.Merging.ConflictTop {
selection = mergeconflicts.TOP
}
err := gui.resolveConflict(*conflict, selection)
if err != nil {
panic(err)
}
2018-12-08 07:54:54 +02:00
2021-04-01 15:48:13 +02:00
// if that was the last conflict, finish the merge for this file
if len(gui.State.Panels.Merging.Conflicts) == 1 {
if err := gui.handleCompleteMerge(); err != nil {
return err
}
}
return gui.refreshMergePanel()
})
}
func (gui *Gui) handlePickBothHunks() error {
return gui.withMergeConflictLock(func() error {
conflict := gui.getCurrentConflict()
if conflict == nil {
return nil
}
gui.takeOverMergeConflictScrolling()
2021-04-01 15:48:13 +02:00
if err := gui.pushFileSnapshot(); err != nil {
2018-12-08 07:54:54 +02:00
return err
}
2021-04-01 15:48:13 +02:00
err := gui.resolveConflict(*conflict, mergeconflicts.BOTH)
if err != nil {
panic(err)
}
return gui.refreshMergePanel()
})
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) getCurrentConflict() *commands.Conflict {
if len(gui.State.Panels.Merging.Conflicts) == 0 {
return nil
}
2021-04-01 15:48:13 +02:00
return &gui.State.Panels.Merging.Conflicts[gui.State.Panels.Merging.ConflictIndex]
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) resolveConflict(conflict commands.Conflict, selection mergeconflicts.Selection) error {
gitFile := gui.getSelectedFile()
if gitFile == nil {
return nil
2020-03-09 02:34:10 +02:00
}
2021-04-01 15:48:13 +02:00
file, err := os.Open(gitFile.Name)
2018-08-14 11:05:26 +02:00
if err != nil {
2021-04-01 15:48:13 +02:00
return err
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
defer file.Close()
reader := bufio.NewReader(file)
output := ""
for i := 0; true; i++ {
line, err := reader.ReadString('\n')
if err != nil {
break
}
if !mergeconflicts.IsIndexToDelete(i, conflict, selection) {
output += line
}
}
return ioutil.WriteFile(gitFile.Name, []byte(output), 0644)
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) refreshMergePanelWithLock() error {
2021-04-01 15:48:13 +02:00
return gui.withMergeConflictLock(gui.refreshMergePanel)
}
func (gui *Gui) refreshMergePanel() error {
2018-12-08 07:54:54 +02:00
panelState := gui.State.Panels.Merging
2020-11-16 11:38:26 +02:00
cat, err := gui.catSelectedFile()
2018-08-14 11:05:26 +02:00
if err != nil {
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
2020-08-18 14:23:36 +02:00
main: &viewUpdateOpts{
title: "",
2021-04-04 15:51:59 +02:00
task: NewRenderStringTask(err.Error()),
2020-08-18 14:23:36 +02:00
},
})
2018-08-14 11:05:26 +02:00
}
2021-04-01 15:48:13 +02:00
panelState.Conflicts = mergeconflicts.FindConflicts(cat)
2020-08-18 14:23:36 +02:00
2018-12-08 07:54:54 +02:00
// handle potential fixes that the user made in their editor since we last refreshed
if len(panelState.Conflicts) == 0 {
return gui.handleCompleteMerge()
} else if panelState.ConflictIndex > len(panelState.Conflicts)-1 {
panelState.ConflictIndex = len(panelState.Conflicts) - 1
2018-08-14 11:05:26 +02:00
}
2018-12-08 07:54:54 +02:00
2019-02-25 13:11:35 +02:00
hasFocus := gui.currentViewName() == "main"
2021-04-01 15:48:13 +02:00
content := mergeconflicts.ColoredConflictFile(cat, panelState.Conflicts, panelState.ConflictIndex, panelState.ConflictTop, hasFocus)
2020-11-16 11:38:26 +02:00
if err := gui.scrollToConflict(); err != nil {
2020-03-29 02:49:37 +02:00
return err
}
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
2020-08-18 14:02:35 +02:00
main: &viewUpdateOpts{
2020-10-04 02:00:48 +02:00
title: gui.Tr.MergeConflictsTitle,
2021-04-04 15:51:59 +02:00
task: NewRenderStringWithoutScrollTask(content),
2020-08-18 14:02:35 +02:00
noWrap: true,
},
})
2018-08-14 11:05:26 +02:00
}
2020-11-16 11:38:26 +02:00
func (gui *Gui) catSelectedFile() (string, error) {
2020-08-16 09:45:12 +02:00
item := gui.getSelectedFile()
if item == nil {
2020-10-04 02:00:48 +02:00
return "", errors.New(gui.Tr.NoFilesDisplay)
}
2020-08-16 09:45:12 +02:00
if item.Type != "file" {
2020-10-04 02:00:48 +02:00
return "", errors.New(gui.Tr.NotAFile)
}
2020-08-16 09:45:12 +02:00
cat, err := gui.GitCommand.CatFile(item.Name)
if err != nil {
gui.Log.Error(err)
2020-08-18 14:41:14 +02:00
return "", err
}
return cat, nil
}
2020-11-16 11:38:26 +02:00
func (gui *Gui) scrollToConflict() error {
if gui.State.Panels.Merging.UserScrolling {
return nil
}
2018-12-08 07:54:54 +02:00
panelState := gui.State.Panels.Merging
if len(panelState.Conflicts) == 0 {
2018-08-14 11:05:26 +02:00
return nil
}
2021-04-04 15:51:59 +02:00
mergingView := gui.Views.Main
2018-12-08 07:54:54 +02:00
conflict := panelState.Conflicts[panelState.ConflictIndex]
ox, _ := mergingView.Origin()
_, height := mergingView.Size()
2018-08-14 11:05:26 +02:00
conflictMiddle := (conflict.End + conflict.Start) / 2
newOriginY := int(math.Max(0, float64(conflictMiddle-(height/2))))
2018-12-08 07:54:54 +02:00
gui.g.Update(func(g *gocui.Gui) error {
return mergingView.SetOrigin(ox, newOriginY)
})
return nil
2018-08-14 11:05:26 +02:00
}
func (gui *Gui) getMergingOptions() map[string]string {
2020-10-03 06:54:55 +02:00
keybindingConfig := gui.Config.GetUserConfig().Keybinding
return map[string]string{
2020-10-04 02:00:48 +02:00
fmt.Sprintf("%s %s", gui.getKeyDisplay(keybindingConfig.Universal.PrevItem), gui.getKeyDisplay(keybindingConfig.Universal.NextItem)): gui.Tr.LcSelectHunk,
fmt.Sprintf("%s %s", gui.getKeyDisplay(keybindingConfig.Universal.PrevBlock), gui.getKeyDisplay(keybindingConfig.Universal.NextBlock)): gui.Tr.LcNavigateConflicts,
gui.getKeyDisplay(keybindingConfig.Universal.Select): gui.Tr.LcPickHunk,
gui.getKeyDisplay(keybindingConfig.Main.PickBothHunks): gui.Tr.LcPickBothHunks,
gui.getKeyDisplay(keybindingConfig.Universal.Undo): gui.Tr.LcUndo,
}
2018-08-14 11:05:26 +02:00
}
2020-08-15 09:23:16 +02:00
func (gui *Gui) handleEscapeMerge() error {
gui.takeOverMergeConflictScrolling()
2018-12-08 07:54:54 +02:00
gui.State.Panels.Merging.EditHistory = stack.New()
if err := gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}}); err != nil {
2019-02-16 12:30:29 +02:00
return err
}
2018-12-08 07:54:54 +02:00
// it's possible this method won't be called from the merging view so we need to
// ensure we only 'return' focus if we already have it
2021-04-04 15:51:59 +02:00
if gui.g.CurrentView() == gui.Views.Main {
2021-04-03 06:56:11 +02:00
return gui.pushContext(gui.State.Contexts.Files)
2018-08-14 11:05:26 +02:00
}
2018-12-08 07:54:54 +02:00
return nil
2018-08-14 11:05:26 +02:00
}
2018-12-08 07:54:54 +02:00
func (gui *Gui) handleCompleteMerge() error {
2020-11-16 11:38:26 +02:00
if err := gui.stageSelectedFile(); err != nil {
2019-02-16 12:30:29 +02:00
return err
}
if err := gui.refreshSidePanels(refreshOptions{scope: []RefreshableView{FILES}}); err != nil {
2019-02-16 12:30:29 +02:00
return err
}
2019-02-16 12:01:17 +02:00
// if we got conflicts after unstashing, we don't want to call any git
// commands to continue rebasing/merging here
2020-11-16 11:38:26 +02:00
if gui.GitCommand.WorkingTreeState() == commands.REBASE_MODE_NORMAL {
2020-08-15 09:23:16 +02:00
return gui.handleEscapeMerge()
2019-02-16 12:01:17 +02:00
}
2018-12-08 07:54:54 +02:00
// if there are no more files with merge conflicts, we should ask whether the user wants to continue
if !gui.anyFilesWithMergeConflicts() {
2021-04-01 15:48:13 +02:00
return gui.promptToContinueRebase()
2018-12-02 18:34:15 +02:00
}
2020-08-15 09:23:16 +02:00
return gui.handleEscapeMerge()
2018-08-14 11:05:26 +02:00
}
2019-03-03 07:11:20 +02:00
2021-04-01 15:48:13 +02:00
// promptToContinueRebase asks the user if they want to continue the rebase/merge that's in progress
func (gui *Gui) promptToContinueRebase() error {
gui.takeOverMergeConflictScrolling()
2020-08-15 08:38:16 +02:00
return gui.ask(askOpts{
2020-08-23 11:28:59 +02:00
title: "continue",
2020-10-04 02:00:48 +02:00
prompt: gui.Tr.ConflictsResolved,
2020-08-23 11:28:59 +02:00
handlersManageFocus: true,
2020-08-15 08:36:39 +02:00
handleConfirm: func() error {
2021-04-03 06:56:11 +02:00
if err := gui.pushContext(gui.State.Contexts.Files); err != nil {
2020-08-23 11:28:59 +02:00
return err
}
2020-08-15 08:36:39 +02:00
return gui.genericMergeCommand("continue")
},
2020-08-23 11:28:59 +02:00
handleClose: func() error {
2021-04-03 06:56:11 +02:00
return gui.pushContext(gui.State.Contexts.Files)
2020-08-23 11:28:59 +02:00
},
2020-08-15 08:36:39 +02:00
})
2019-03-03 07:11:20 +02:00
}
func (gui *Gui) canScrollMergePanel() bool {
currentViewName := gui.currentViewName()
if currentViewName != "main" && currentViewName != "files" {
return false
}
2020-08-16 09:45:12 +02:00
file := gui.getSelectedFile()
if file == nil {
return false
}
return file.HasInlineMergeConflicts
}
2021-04-01 15:48:13 +02:00
func (gui *Gui) withMergeConflictLock(f func() error) error {
gui.State.Panels.Merging.ConflictsMutex.Lock()
defer gui.State.Panels.Merging.ConflictsMutex.Unlock()
return f()
}
func (gui *Gui) takeOverMergeConflictScrolling() {
2021-04-01 15:48:13 +02:00
gui.State.Panels.Merging.UserScrolling = false
}