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

162 lines
4.2 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"
2022-01-28 11:44:36 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/popup"
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
func (gui *Gui) exitDiffMode() error {
2021-06-05 07:08:36 +02:00
gui.State.Modes.Diffing = diffing.New()
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
}
func (gui *Gui) renderDiff() error {
2021-12-29 05:33:38 +02:00
cmdObj := gui.OSCommand.Cmd.New(
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-12-07 12:59:36 +02:00
task := NewRunPtyTask(cmdObj.GetCmd())
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()}
if branch.IsTrackingRemote() {
names = append(names, branch.ID()+"@{u}")
}
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()
2022-01-28 11:44:36 +02:00
menuItems := []*popup.MenuItem{}
for _, name := range names {
name := name
2022-01-28 11:44:36 +02:00
menuItems = append(menuItems, []*popup.MenuItem{
{
DisplayString: fmt.Sprintf("%s %s", gui.c.Tr.LcDiff, name),
2022-01-28 11:44:36 +02:00
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.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
},
},
}...)
}
2022-01-28 11:44:36 +02:00
menuItems = append(menuItems, []*popup.MenuItem{
{
DisplayString: gui.c.Tr.LcEnterRefToDiff,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
return gui.c.Prompt(popup.PromptOpts{
Title: gui.c.Tr.LcEnteRefName,
FindSuggestionsFunc: gui.suggestionsHelper.GetRefsSuggestionsFunc(),
2022-01-28 11:44:36 +02:00
HandleConfirm: func(response string) error {
2020-11-28 04:35:58 +02:00
gui.State.Modes.Diffing.Ref = strings.TrimSpace(response)
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
2020-11-28 04:35:58 +02:00
},
})
},
},
}...)
if gui.State.Modes.Diffing.Active() {
2022-01-28 11:44:36 +02:00
menuItems = append(menuItems, []*popup.MenuItem{
2020-03-29 08:54:58 +02:00
{
DisplayString: gui.c.Tr.LcSwapDiff,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
2020-08-22 03:05:37 +02:00
gui.State.Modes.Diffing.Reverse = !gui.State.Modes.Diffing.Reverse
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
2020-03-29 08:54:58 +02:00
},
},
{
DisplayString: gui.c.Tr.LcExitDiffMode,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
2021-06-05 07:08:36 +02:00
gui.State.Modes.Diffing = diffing.New()
return gui.c.Refresh(types.RefreshOptions{Mode: types.ASYNC})
2020-03-29 08:54:58 +02:00
},
},
2020-03-29 08:54:58 +02:00
}...)
}
return gui.c.Menu(popup.CreateMenuOptions{Title: gui.c.Tr.DiffingMenuTitle, Items: menuItems})
}