summaryrefslogtreecommitdiffstats
path: root/pkg/gui/controllers/helpers/patch_building_helper.go
blob: 59de50b9908a85b88a9f048e8da935cb8e9561a8 (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
package helpers

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

type IPatchBuildingHelper interface {
	ValidateNormalWorkingTreeState() (bool, error)
}

type PatchBuildingHelper struct {
	c        *types.HelperCommon
	git      *commands.GitCommand
	contexts *context.ContextTree
}

func NewPatchBuildingHelper(
	c *types.HelperCommon,
	git *commands.GitCommand,
	contexts *context.ContextTree,
) *PatchBuildingHelper {
	return &PatchBuildingHelper{
		c:        c,
		git:      git,
		contexts: contexts,
	}
}

func (self *PatchBuildingHelper) ValidateNormalWorkingTreeState() (bool, error) {
	if self.git.Status.WorkingTreeState() != enums.REBASE_MODE_NONE {
		return false, self.c.ErrorMsg(self.c.Tr.CantPatchWhileRebasingError)
	}
	return true, nil
}

// takes us from the patch building panel back to the commit files panel
func (self *PatchBuildingHelper) Escape() error {
	return self.c.PopContext()
}

// kills the custom patch and returns us back to the commit files panel if needed
func (self *PatchBuildingHelper) Reset() error {
	self.git.Patch.PatchBuilder.Reset()

	if self.c.CurrentStaticContext().GetKind() != types.SIDE_CONTEXT {
		if err := self.Escape(); err != nil {
			return err
		}
	}

	if err := self.c.Refresh(types.RefreshOptions{
		Scope: []types.RefreshableView{types.COMMIT_FILES},
	}); err != nil {
		return err
	}

	// refreshing the current context so that the secondary panel is hidden if necessary.
	return self.c.PostRefreshUpdate(self.c.CurrentContext())
}