summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/sub_commits_switch_controller.go
blob: dbd6ab135b3dd3c7e4aaf69055a5b02eea9655b1 (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
95
96
97
98
99
100
101
102
103
104
105
package controllers

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

type SubCommitsSwitchControllerFactory struct {
	c                 *types.ControllerCommon
	subCommitsContext *context.SubCommitsContext
	git               *commands.GitCommand
	modes             *types.Modes
	setSubCommits     func([]*models.Commit)
}

var _ types.IController = &SubCommitsSwitchController{}

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

type SubCommitsSwitchController struct {
	baseController

	c                 *types.ControllerCommon
	context           ContextWithRefName
	subCommitsContext *context.SubCommitsContext
	git               *commands.GitCommand
	modes             *types.Modes
	setSubCommits     func([]*models.Commit)
}

func NewSubCommitsSwitchControllerFactory(
	c *types.ControllerCommon,
	subCommitsContext *context.SubCommitsContext,
	git *commands.GitCommand,
	modes *types.Modes,
	setSubCommits func([]*models.Commit),
) *SubCommitsSwitchControllerFactory {
	return &SubCommitsSwitchControllerFactory{
		c:                 c,
		subCommitsContext: subCommitsContext,
		git:               git,
		modes:             modes,
		setSubCommits:     setSubCommits,
	}
}

func (self *SubCommitsSwitchControllerFactory) Create(context ContextWithRefName) *SubCommitsSwitchController {
	return &SubCommitsSwitchController{
		baseController:    baseController{},
		c:                 self.c,
		context:           context,
		subCommitsContext: self.subCommitsContext,
		git:               self.git,
		modes:             self.modes,
		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.subCommitsContext.SetSelectedLineIdx(0)
	self.subCommitsContext.SetParentContext(self.context)

	return self.c.PushContext(self.subCommitsContext)
}

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