summaryrefslogtreecommitdiffstats
path: root/pkg/commands/git_commands/tag_loader.go
blob: f9674a2b088e8e06a3fd27a589b7c9141c56e24d (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
package git_commands

import (
	"fmt"

	"github.com/jesseduffield/generics/slices"
	"github.com/jesseduffield/lazygit/pkg/commands/models"
	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
	"github.com/jesseduffield/lazygit/pkg/common"
	"github.com/jesseduffield/lazygit/pkg/utils"
)

type TagLoader struct {
	*common.Common
	version *GitVersion
	cmd     oscommands.ICmdObjBuilder
}

func NewTagLoader(
	common *common.Common,
	version *GitVersion,
	cmd oscommands.ICmdObjBuilder,
) *TagLoader {
	return &TagLoader{
		Common:  common,
		version: version,
		cmd:     cmd,
	}
}

func (self *TagLoader) GetTags() ([]*models.Tag, error) {
	// get remote branches, sorted  by creation date (descending)
	// see: https://git-scm.com/docs/git-tag#Documentation/git-tag.txt---sortltkeygt
	sortKey := "-creatordate"
	if self.version.IsOlderThan(2, 7, 0) {
		sortKey = "-v:refname"
	}

	tagsOutput, err := self.cmd.New(fmt.Sprintf(`git tag --list --sort=%s`, sortKey)).DontLog().RunWithOutput()
	if err != nil {
		return nil, err
	}

	split := utils.SplitLines(tagsOutput)

	tags := slices.Map(split, func(tagName string) *models.Tag {
		return &models.Tag{
			Name: tagName,
		}
	})

	return tags, nil
}