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"
|
|
|
|
"bytes"
|
2020-01-07 19:50:25 +02:00
|
|
|
"fmt"
|
2018-08-14 11:05:26 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fatih/color"
|
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"
|
2019-10-18 09:48:37 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/theme"
|
2018-08-14 11:05:26 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (gui *Gui) findConflicts(content string) ([]commands.Conflict, error) {
|
|
|
|
conflicts := make([]commands.Conflict, 0)
|
|
|
|
var newConflict commands.Conflict
|
|
|
|
for i, line := range utils.SplitLines(content) {
|
2018-12-05 13:30:10 +02:00
|
|
|
trimmedLine := strings.TrimPrefix(line, "++")
|
2018-12-08 07:54:54 +02:00
|
|
|
gui.Log.Info(trimmedLine)
|
2019-11-08 00:31:27 +02:00
|
|
|
if trimmedLine == "<<<<<<< HEAD" || trimmedLine == "<<<<<<< MERGE_HEAD" || trimmedLine == "<<<<<<< Updated upstream" || trimmedLine == "<<<<<<< ours" {
|
2018-08-14 11:05:26 +02:00
|
|
|
newConflict = commands.Conflict{Start: i}
|
2018-12-05 13:30:10 +02:00
|
|
|
} else if trimmedLine == "=======" {
|
2018-08-14 11:05:26 +02:00
|
|
|
newConflict.Middle = i
|
2018-12-05 13:30:10 +02:00
|
|
|
} else if strings.HasPrefix(trimmedLine, ">>>>>>> ") {
|
2018-08-14 11:05:26 +02:00
|
|
|
newConflict.End = i
|
|
|
|
conflicts = append(conflicts, newConflict)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return conflicts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) shiftConflict(conflicts []commands.Conflict) (commands.Conflict, []commands.Conflict) {
|
|
|
|
return conflicts[0], conflicts[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) shouldHighlightLine(index int, conflict commands.Conflict, top bool) bool {
|
|
|
|
return (index >= conflict.Start && index <= conflict.Middle && top) || (index >= conflict.Middle && index <= conflict.End && !top)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) coloredConflictFile(content string, conflicts []commands.Conflict, conflictIndex int, conflictTop, hasFocus bool) (string, error) {
|
|
|
|
if len(conflicts) == 0 {
|
|
|
|
return content, nil
|
|
|
|
}
|
|
|
|
conflict, remainingConflicts := gui.shiftConflict(conflicts)
|
|
|
|
var outputBuffer bytes.Buffer
|
|
|
|
for i, line := range utils.SplitLines(content) {
|
2019-10-18 09:48:37 +02:00
|
|
|
colourAttr := theme.DefaultTextColor
|
2018-08-14 11:05:26 +02:00
|
|
|
if i == conflict.Start || i == conflict.Middle || i == conflict.End {
|
|
|
|
colourAttr = color.FgRed
|
|
|
|
}
|
|
|
|
colour := color.New(colourAttr)
|
|
|
|
if hasFocus && conflictIndex < len(conflicts) && conflicts[conflictIndex] == conflict && gui.shouldHighlightLine(i, conflict, conflictTop) {
|
|
|
|
colour.Add(color.Bold)
|
|
|
|
}
|
|
|
|
if i == conflict.End && len(remainingConflicts) > 0 {
|
|
|
|
conflict, remainingConflicts = gui.shiftConflict(remainingConflicts)
|
|
|
|
}
|
|
|
|
outputBuffer.WriteString(utils.ColoredStringDirect(line, colour) + "\n")
|
|
|
|
}
|
|
|
|
return outputBuffer.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleSelectTop(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
gui.State.Panels.Merging.ConflictTop = true
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleSelectBottom(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
gui.State.Panels.Merging.ConflictTop = false
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleSelectNextConflict(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
if gui.State.Panels.Merging.ConflictIndex >= len(gui.State.Panels.Merging.Conflicts)-1 {
|
2018-08-14 11:05:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
gui.State.Panels.Merging.ConflictIndex++
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleSelectPrevConflict(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
if gui.State.Panels.Merging.ConflictIndex <= 0 {
|
2018-08-14 11:05:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
gui.State.Panels.Merging.ConflictIndex--
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) isIndexToDelete(i int, conflict commands.Conflict, pick string) bool {
|
|
|
|
return i == conflict.Middle ||
|
|
|
|
i == conflict.Start ||
|
|
|
|
i == conflict.End ||
|
|
|
|
pick != "both" &&
|
|
|
|
(pick == "bottom" && i > conflict.Start && i < conflict.Middle) ||
|
|
|
|
(pick == "top" && i > conflict.Middle && i < conflict.End)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) resolveConflict(g *gocui.Gui, conflict commands.Conflict, pick string) error {
|
|
|
|
gitFile, err := gui.getSelectedFile(g)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
file, err := os.Open(gitFile.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
reader := bufio.NewReader(file)
|
|
|
|
output := ""
|
|
|
|
for i := 0; true; i++ {
|
|
|
|
line, err := reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if !gui.isIndexToDelete(i, conflict, pick) {
|
|
|
|
output += line
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gui.Log.Info(output)
|
|
|
|
return ioutil.WriteFile(gitFile.Name, []byte(output), 0644)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) pushFileSnapshot(g *gocui.Gui) error {
|
|
|
|
gitFile, err := gui.getSelectedFile(g)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handlePopFileSnapshot(g *gocui.Gui, v *gocui.View) 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)
|
2018-08-14 11:05:26 +02:00
|
|
|
gitFile, err := gui.getSelectedFile(g)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ioutil.WriteFile(gitFile.Name, []byte(prevContent), 0644)
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handlePickHunk(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
conflict := gui.State.Panels.Merging.Conflicts[gui.State.Panels.Merging.ConflictIndex]
|
2018-08-14 11:05:26 +02:00
|
|
|
gui.pushFileSnapshot(g)
|
|
|
|
pick := "bottom"
|
2018-12-08 07:54:54 +02:00
|
|
|
if gui.State.Panels.Merging.ConflictTop {
|
2018-08-14 11:05:26 +02:00
|
|
|
pick = "top"
|
|
|
|
}
|
|
|
|
err := gui.resolveConflict(g, conflict, pick)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-12-08 07:54:54 +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
|
|
|
|
}
|
|
|
|
}
|
2019-02-16 12:30:29 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handlePickBothHunks(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
conflict := gui.State.Panels.Merging.Conflicts[gui.State.Panels.Merging.ConflictIndex]
|
2018-08-14 11:05:26 +02:00
|
|
|
gui.pushFileSnapshot(g)
|
|
|
|
err := gui.resolveConflict(g, conflict, "both")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.refreshMergePanel()
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
2019-02-16 03:07:27 +02:00
|
|
|
func (gui *Gui) refreshMergePanel() error {
|
2018-12-08 07:54:54 +02:00
|
|
|
panelState := gui.State.Panels.Merging
|
2019-02-16 03:07:27 +02:00
|
|
|
cat, err := gui.catSelectedFile(gui.g)
|
2018-08-14 11:05:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-28 11:12:35 +02:00
|
|
|
if cat == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2018-12-08 07:54:54 +02:00
|
|
|
panelState.Conflicts, err = gui.findConflicts(cat)
|
2018-08-14 11:05:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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"
|
2018-12-08 07:54:54 +02:00
|
|
|
content, err := gui.coloredConflictFile(cat, panelState.Conflicts, panelState.ConflictIndex, panelState.ConflictTop, hasFocus)
|
2018-08-14 11:05:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return 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
|
|
|
if err := gui.newStringTask("main", content); err != nil {
|
2018-12-08 07:54:54 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-02-16 03:07:27 +02:00
|
|
|
if err := gui.scrollToConflict(gui.g); err != nil {
|
2018-08-14 11:05:26 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-02-16 03:07:27 +02:00
|
|
|
|
|
|
|
mainView := gui.getMainView()
|
|
|
|
mainView.Wrap = false
|
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
return nil
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) scrollToConflict(g *gocui.Gui) error {
|
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
|
|
|
|
}
|
2019-02-16 03:07:27 +02:00
|
|
|
mergingView := gui.getMainView()
|
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
|
|
|
}
|
|
|
|
|
2018-12-07 09:52:31 +02:00
|
|
|
func (gui *Gui) renderMergeOptions() error {
|
2020-01-07 19:50:25 +02:00
|
|
|
return gui.renderOptionsMap(map[string]string{
|
|
|
|
fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevItem"), gui.getKeyDisplay("universal.nextItem")): gui.Tr.SLocalize("selectHunk"),
|
|
|
|
fmt.Sprintf("%s %s", gui.getKeyDisplay("universal.prevBlock"), gui.getKeyDisplay("universal.nextBlock")): gui.Tr.SLocalize("navigateConflicts"),
|
|
|
|
gui.getKeyDisplay("universal.select"): gui.Tr.SLocalize("pickHunk"),
|
|
|
|
gui.getKeyDisplay("main.pickBothHunks"): gui.Tr.SLocalize("pickBothHunks"),
|
|
|
|
gui.getKeyDisplay("main.undo"): gui.Tr.SLocalize("undo"),
|
|
|
|
})
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gui *Gui) handleEscapeMerge(g *gocui.Gui, v *gocui.View) error {
|
2018-12-08 07:54:54 +02:00
|
|
|
gui.State.Panels.Merging.EditHistory = stack.New()
|
2019-02-16 12:30:29 +02:00
|
|
|
if err := gui.refreshFiles(); err != nil {
|
|
|
|
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
|
2019-02-16 03:07:27 +02:00
|
|
|
if gui.g.CurrentView() == gui.getMainView() {
|
2018-12-08 07:54:54 +02:00
|
|
|
return gui.switchFocus(g, v, gui.getFilesView())
|
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 {
|
2019-02-16 12:30:29 +02:00
|
|
|
if err := gui.stageSelectedFile(gui.g); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gui.refreshFiles(); err != nil {
|
|
|
|
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
|
|
|
|
if gui.State.WorkingTreeState == "normal" {
|
|
|
|
return gui.handleEscapeMerge(gui.g, gui.getMainView())
|
|
|
|
}
|
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() {
|
2019-03-03 07:11:20 +02:00
|
|
|
return gui.promptToContinue()
|
2018-12-02 18:34:15 +02:00
|
|
|
}
|
2019-02-16 03:07:27 +02:00
|
|
|
return gui.handleEscapeMerge(gui.g, gui.getMainView())
|
2018-08-14 11:05:26 +02:00
|
|
|
}
|
2019-03-03 07:11:20 +02:00
|
|
|
|
|
|
|
// promptToContinue asks the user if they want to continue the rebase/merge that's in progress
|
|
|
|
func (gui *Gui) promptToContinue() error {
|
2019-11-05 06:19:43 +02:00
|
|
|
return gui.createConfirmationPanel(gui.g, gui.getFilesView(), true, "continue", gui.Tr.SLocalize("ConflictsResolved"), func(g *gocui.Gui, v *gocui.View) error {
|
2019-03-03 07:11:20 +02:00
|
|
|
return gui.genericMergeCommand("continue")
|
|
|
|
}, nil)
|
|
|
|
}
|