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

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

// Helper structs are for defining functionality that could be used by multiple contexts.
// For example, here we have a CreateTagMenu which is applicable to both the tags context
// and the commits context.

type TagsHelper struct {
	c   *types.ControllerCommon
	git *commands.GitCommand
}

func NewTagsHelper(c *types.ControllerCommon, git *commands.GitCommand) *TagsHelper {
	return &TagsHelper{
		c:   c,
		git: git,
	}
}

func (self *TagsHelper) CreateTagMenu(commitSha string, onCreate func()) error {
	return self.c.Menu(types.CreateMenuOptions{
		Title: self.c.Tr.TagMenuTitle,
		Items: []*types.MenuItem{
			{
				DisplayString: self.c.Tr.LcLightweightTag,
				OnPress: func() error {
					return self.handleCreateLightweightTag(commitSha, onCreate)
				},
			},
			{
				DisplayString: self.c.Tr.LcAnnotatedTag,
				OnPress: func() error {
					return self.handleCreateAnnotatedTag(commitSha, onCreate)
				},
			},
		},
	})
}

func (self *TagsHelper) afterTagCreate(onCreate func()) error {
	onCreate()
	return self.c.Refresh(types.RefreshOptions{
		Mode: types.ASYNC, Scope: []types.RefreshableView{types.COMMITS, types.TAGS},
	})
}

func (self *TagsHelper) handleCreateAnnotatedTag(commitSha string, onCreate func()) error {
	return self.c.Prompt(types.PromptOpts{
		Title: self.c.Tr.TagNameTitle,
		HandleConfirm: func(tagName string) error {
			return self.c.Prompt(types.PromptOpts{
				Title: self.c.Tr.TagMessageTitle,
				HandleConfirm: func(msg string) error {
					self.c.LogAction(self.c.Tr.Actions.CreateAnnotatedTag)
					if err := self.git.Tag.CreateAnnotated(tagName, commitSha, msg); err != nil {
						return self.c.Error(err)
					}
					return self.afterTagCreate(onCreate)
				},
			})
		},
	})
}

func (self *TagsHelper) handleCreateLightweightTag(commitSha string, onCreate func()) error {
	return self.c.Prompt(types.PromptOpts{
		Title: self.c.Tr.TagNameTitle,
		HandleConfirm: func(tagName string) error {
			self.c.LogAction(self.c.Tr.Actions.CreateLightweightTag)
			if err := self.git.Tag.CreateLightweight(tagName, commitSha); err != nil {
				return self.c.Error(err)
			}
			return self.afterTagCreate(onCreate)
		},
	})
}