summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/switch_to_sub_commits_controller.go
blob: 8163181e5dba5fe60a1850eca58708dab9980378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package controllers

import (
	"github.com/jesseduffield/lazygit/pkg/commands/git_commands"
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

var _ types.IController = &SwitchToSubCommitsController{}

type CanSwitchToSubCommits interface {
	types.Context
	GetSelectedRef() types.Ref
}

type SwitchToSubCommitsController struct {
	baseController
	c       *ControllerCommon
	context CanSwitchToSubCommits

	setSubCommits func([]*models.Commit)
}

func NewSwitchToSubCommitsController(
	controllerCommon *ControllerCommon,
	setSubCommits func([]*models.Commit),
	context CanSwitchToSubCommits,
) *SwitchToSubCommitsController {
	return &SwitchToSubCommitsController{
		baseController: baseController{},
		c:              controllerCommon,
		context:        context,
		setSubCommits:  setSubCommits,
	}
}

func (self *SwitchToSubCommitsController) GetKeybindings(opts types.KeybindingsOpts) []*types.Binding {
	bindings := []*types.Binding{
		{
			Handler:     self.viewCommits,
			Key:         opts.GetKey(opts.Config.Universal.GoInto),
			Description: self.c.Tr.ViewCommits,
		},
	}

	return bindings
}

func (self *SwitchToSubCommitsController) GetOnClick() func() error {
	return self.viewCommits
}

func (self *SwitchToSubCommitsController) viewCommits() error {
	ref := self.context.GetSelectedRef()
	if ref == nil {
		return nil
	}

	// need to populate my sub commits
	commits, err := self.c.Git().Loaders.CommitLoader.GetCommits(
		git_commands.GetCommitsOptions{
			Limit:                true,
			FilterPath:           self.c.Modes().Filtering.GetPath(),
			IncludeRebaseCommits: false,
			RefName:              ref.FullRefName(),
		},
	)
	if err != nil {
		return err
	}

	self.setSubCommits(commits)

	subCommitsContext := self.c.Contexts().SubCommits
	subCommitsContext.SetSelectedLineIdx(0)
	subCommitsContext.SetParentContext(self.context)
	subCommitsContext.SetWindowName(self.context.GetWindowName())
	subCommitsContext.SetTitleRef(ref.Description())
	subCommitsContext.SetRef(ref)
	subCommitsContext.SetLimitCommits(true)
	subCommitsContext.ClearSearchString()
	subCommitsContext.GetView().ClearSearch()

	err = self.c.PostRefreshUpdate(self.c.Contexts().SubCommits)
	if err != nil {
		return err
	}

	return self.c.PushContext(self.c.Contexts().SubCommits)
}

func (self *SwitchToSubCommitsController) Context() types.Context {
	return self.context
}