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

160 lines
4.0 KiB
Go
Raw Normal View History

package gui
import (
"fmt"
"strings"
2021-06-05 07:08:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/modes/diffing"
)
func (gui *Gui) exitDiffMode() error {
2021-06-05 07:08:36 +02:00
gui.State.Modes.Diffing = diffing.New()
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
}
func (gui *Gui) renderDiff() error {
2020-08-18 14:02:35 +02:00
cmd := gui.OSCommand.ExecutableFromString(
2020-09-28 00:21:53 +02:00
fmt.Sprintf("git diff --submodule --no-ext-diff --color %s", gui.diffStr()),
2020-08-18 14:02:35 +02:00
)
2021-04-04 15:51:59 +02:00
task := NewRunPtyTask(cmd)
2020-08-18 14:02:35 +02:00
2020-08-23 01:46:28 +02:00
return gui.refreshMainViews(refreshMainOpts{
main: &viewUpdateOpts{
title: "Diff",
2020-08-18 14:02:35 +02:00
task: task,
},
})
}
// currentDiffTerminals returns the current diff terminals of the currently selected item.
// in the case of a branch it returns both the branch and it's upstream name,
// which becomes an option when you bring up the diff menu, but when you're just
// flicking through branches it will be using the local branch name.
func (gui *Gui) currentDiffTerminals() []string {
switch gui.currentContext().GetKey() {
2020-08-22 03:05:37 +02:00
case "":
return nil
2020-09-30 00:27:23 +02:00
case FILES_CONTEXT_KEY, SUBMODULES_CONTEXT_KEY:
// TODO: should we just return nil here?
return []string{""}
case COMMIT_FILES_CONTEXT_KEY:
return []string{gui.State.Panels.CommitFiles.refName}
2020-08-20 11:42:58 +02:00
case LOCAL_BRANCHES_CONTEXT_KEY:
2020-08-22 03:05:37 +02:00
// for our local branches we want to include both the branch and its upstream
2020-08-19 14:57:22 +02:00
branch := gui.getSelectedBranch()
if branch != nil {
2020-08-22 03:05:37 +02:00
names := []string{branch.ID()}
2020-08-19 14:57:22 +02:00
if branch.UpstreamName != "" {
names = append(names, branch.UpstreamName)
}
2020-08-19 14:57:22 +02:00
return names
}
return nil
2020-08-22 03:05:37 +02:00
default:
2021-04-04 15:51:59 +02:00
context := gui.currentSideListContext()
2020-08-22 03:05:37 +02:00
if context == nil {
return nil
2020-08-19 14:57:22 +02:00
}
item, ok := context.GetSelectedItem()
if !ok {
2020-08-22 03:05:37 +02:00
return nil
}
2020-08-22 03:05:37 +02:00
return []string{item.ID()}
}
}
func (gui *Gui) currentDiffTerminal() string {
names := gui.currentDiffTerminals()
if len(names) == 0 {
2020-03-29 08:54:58 +02:00
return ""
}
return names[0]
}
func (gui *Gui) currentlySelectedFilename() string {
switch gui.currentContext().GetKey() {
case FILES_CONTEXT_KEY, COMMIT_FILES_CONTEXT_KEY:
return gui.getSideContextSelectedItemId()
default:
return ""
}
}
func (gui *Gui) diffStr() string {
2020-08-22 03:05:37 +02:00
output := gui.State.Modes.Diffing.Ref
2020-03-29 08:54:58 +02:00
right := gui.currentDiffTerminal()
2020-03-29 08:54:58 +02:00
if right != "" {
output += " " + right
}
2020-08-22 03:05:37 +02:00
if gui.State.Modes.Diffing.Reverse {
2020-03-29 08:54:58 +02:00
output += " -R"
}
file := gui.currentlySelectedFilename()
if file != "" {
output += " -- " + file
} else if gui.State.Modes.Filtering.Active() {
2021-04-03 02:32:14 +02:00
output += " -- " + gui.State.Modes.Filtering.GetPath()
}
2020-03-29 08:54:58 +02:00
return output
}
func (gui *Gui) handleCreateDiffingMenuPanel() error {
names := gui.currentDiffTerminals()
menuItems := []*menuItem{}
for _, name := range names {
name := name
menuItems = append(menuItems, []*menuItem{
{
2020-10-04 02:00:48 +02:00
displayString: fmt.Sprintf("%s %s", gui.Tr.LcDiff, name),
onPress: func() error {
2020-08-22 03:05:37 +02:00
gui.State.Modes.Diffing.Ref = name
// can scope this down based on current view but too lazy right now
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
},
},
}...)
}
menuItems = append(menuItems, []*menuItem{
{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcEnterRefToDiff,
onPress: func() error {
2020-11-28 04:35:58 +02:00
return gui.prompt(promptOpts{
2021-10-23 02:25:37 +02:00
title: gui.Tr.LcEnteRefName,
findSuggestionsFunc: gui.getRefsSuggestionsFunc(),
2020-11-28 04:35:58 +02:00
handleConfirm: func(response string) error {
gui.State.Modes.Diffing.Ref = strings.TrimSpace(response)
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
},
})
},
},
}...)
if gui.State.Modes.Diffing.Active() {
2020-03-29 08:54:58 +02:00
menuItems = append(menuItems, []*menuItem{
{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcSwapDiff,
2020-03-29 08:54:58 +02:00
onPress: func() error {
2020-08-22 03:05:37 +02:00
gui.State.Modes.Diffing.Reverse = !gui.State.Modes.Diffing.Reverse
2020-03-29 08:54:58 +02:00
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
},
},
{
2020-10-04 02:00:48 +02:00
displayString: gui.Tr.LcExitDiffMode,
2020-03-29 08:54:58 +02:00
onPress: func() error {
2021-06-05 07:08:36 +02:00
gui.State.Modes.Diffing = diffing.New()
2020-03-29 08:54:58 +02:00
return gui.refreshSidePanels(refreshOptions{mode: ASYNC})
},
},
2020-03-29 08:54:58 +02:00
}...)
}
2020-10-04 02:00:48 +02:00
return gui.createMenu(gui.Tr.DiffingMenuTitle, menuItems, createMenuOptions{showCancel: true})
}