summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/sub_commits_switch_controller.go
blob: cbc9ce1379f974bce6a0bac70d6b4277c5ffe50c (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
package controllers

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

type SubCommitsSwitchControllerFactory struct {
	controllerCommon *controllerCommon
	setSubCommits    func([]*models.Commit)
}

var _ types.IController = &SubCommitsSwitchController{}

type ContextWithRefName interface {
	types.Context
	GetSelectedRefName() string
}

type SubCommitsSwitchController struct {
	baseController
	*controllerCommon
	context ContextWithRefName

	setSubCommits func([]*models.Commit)
}

func NewSubCommitsSwitchControllerFactory(
	common *controllerCommon,
	setSubCommits func([]*models.Commit),
) *SubCommitsSwitchControllerFactory {
	return &SubCommitsSwitchControllerFactory{
		controllerCommon: common,
		setSubCommits:    setSubCommits,
	}
}

func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
	return &SubCommitsSwitchController{
		baseController:   baseController{},
		controllerCommon: self.controllerCommon,
		context:          context,
		setSubCommits:    self.setSubCommits,
	}
}

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

	return bindings
}

func (self *SubCommitsSwitchController) viewCommits() error {
	refName := self.context.GetSelectedRefName()
	if refName == "" {
		return nil
	}

	// need to populate my sub commits
	commits, err := self.git.Loaders.Commits.GetCommits(
		loaders.GetCommitsOptions{
			Limit:                true,
			FilterPath:           self.modes.Filtering.GetPath(),
			IncludeRebaseCommits: false,
			RefName:              refName,
		},
	)
	if err != nil {
		return err
	}

	self.setSubCommits(commits)
	self.contexts.SubCommits.SetSelectedLineIdx(0)
	self.contexts.SubCommits.SetParentContext(self.context)

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

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