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

158 lines
4.1 KiB
Go
Raw Normal View History

package gui
import (
"fmt"
"strings"
2021-06-05 07:08:36 +02:00
2022-01-29 10:15:46 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/context"
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/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
2022-01-29 10:15:46 +02:00
case context.FILES_CONTEXT_KEY, context.SUBMODULES_CONTEXT_KEY:
2020-09-30 00:27:23 +02:00
// TODO: should we just return nil here?
return []string{""}
2022-01-29 10:15:46 +02:00
case context.COMMIT_FILES_CONTEXT_KEY:
return []string{gui.State.Contexts.CommitFiles.GetRefName()}
2022-01-29 10:15:46 +02:00
case context.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:
2022-01-30 11:03:08 +02:00
itemId := gui.getSideContextSelectedItemId()
if itemId == "" {
2020-08-22 03:05:37 +02:00
return nil
2020-08-19 14:57:22 +02:00
}
2022-01-30 11:03:08 +02:00
return []string{itemId}
}
}
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() {
2022-01-29 10:15:46 +02:00
case context.FILES_CONTEXT_KEY, context.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-29 10:09:20 +02:00
menuItems := []*types.MenuItem{}
for _, name := range names {
name := name
2022-01-29 10:09:20 +02:00
menuItems = append(menuItems, []*types.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-29 10:09:20 +02:00
menuItems = append(menuItems, []*types.MenuItem{
{
DisplayString: gui.c.Tr.LcEnterRefToDiff,
2022-01-28 11:44:36 +02:00
OnPress: func() error {
2022-01-29 10:09:20 +02:00
return gui.c.Prompt(types.PromptOpts{
Title: gui.c.Tr.LcEnteRefName,
2022-01-30 01:40:48 +02:00
FindSuggestionsFunc: gui.helpers.suggestions.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-29 10:09:20 +02:00
menuItems = append(menuItems, []*types.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
}...)
}
2022-01-29 10:09:20 +02:00
return gui.c.Menu(types.CreateMenuOptions{Title: gui.c.Tr.DiffingMenuTitle, Items: menuItems})
}