Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/commands/git_commands/tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package git_commands

import (
"strings"

"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
)
Expand Down Expand Up @@ -74,3 +76,13 @@ func (self *TagCommands) ShowAnnotationInfo(tagName string) (string, error) {

return self.cmd.New(cmdArgs).RunWithOutput()
}

func (self *TagCommands) IsTagAnnotated(tagName string) (bool, error) {
cmdArgs := NewGitCmd("cat-file").
Arg("-t").
Arg("refs/tags/" + tagName).
ToArgv()

output, err := self.cmd.New(cmdArgs).RunWithOutput()
return strings.TrimSpace(output) == "tag", err
}
32 changes: 14 additions & 18 deletions pkg/commands/git_commands/tag_loader.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package git_commands

import (
"strings"
"regexp"

"github.com/jesseduffield/lazygit/pkg/commands/models"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
Expand All @@ -26,34 +26,30 @@ func NewTagLoader(
}

func (self *TagLoader) GetTags() ([]*models.Tag, error) {
// get tags, sorted by creation date (descending)
// get remote branches, sorted by creation date (descending)
// see: https://git-scm.com/docs/git-tag#Documentation/git-tag.txt---sortltkeygt
cmdArgs := NewGitCmd("for-each-ref").
Arg("--sort=-creatordate").
Arg("--format=%(refname)%00%(objecttype)%00%(contents:subject)").
Arg("refs/tags").
ToArgv()
cmdArgs := NewGitCmd("tag").Arg("--list", "-n", "--sort=-creatordate").ToArgv()
tagsOutput, err := self.cmd.New(cmdArgs).DontLog().RunWithOutput()
if err != nil {
return nil, err
}

split := utils.SplitLines(tagsOutput)

tags := lo.FilterMap(split, func(line string, _ int) (*models.Tag, bool) {
fields := strings.SplitN(line, "\x00", 3)
if len(fields) != 3 {
return nil, false
lineRegex := regexp.MustCompile(`^([^\s]+)(\s+)?(.*)$`)

tags := lo.Map(split, func(line string, _ int) *models.Tag {
matches := lineRegex.FindStringSubmatch(line)
tagName := matches[1]
message := ""
if len(matches) > 3 {
message = matches[3]
}
tagName := fields[0]
objectType := fields[1]
message := fields[2]

return &models.Tag{
Name: strings.TrimPrefix(tagName, "refs/tags/"),
Message: message,
IsAnnotated: objectType == "tag",
}, true
Name: tagName,
Message: message,
}
})

return tags, nil
Expand Down
17 changes: 9 additions & 8 deletions pkg/commands/git_commands/tag_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/stretchr/testify/assert"
)

const tagsOutput = "refs/tags/tag1\x00tag\x00this is my message\n" +
"refs/tags/tag2\x00commit\x00\n" +
"refs/tags/tag3\x00tag\x00this is my other message\n"
const tagsOutput = `tag1 this is my message
tag2
tag3 this is my other message
`

func TestGetTags(t *testing.T) {
type scenario struct {
Expand All @@ -25,18 +26,18 @@ func TestGetTags(t *testing.T) {
{
testName: "should return no tags if there are none",
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"for-each-ref", "--sort=-creatordate", "--format=%(refname)%00%(objecttype)%00%(contents:subject)", "refs/tags"}, "", nil),
ExpectGitArgs([]string{"tag", "--list", "-n", "--sort=-creatordate"}, "", nil),
expectedTags: []*models.Tag{},
expectedError: nil,
},
{
testName: "should return tags if present",
runner: oscommands.NewFakeRunner(t).
ExpectGitArgs([]string{"for-each-ref", "--sort=-creatordate", "--format=%(refname)%00%(objecttype)%00%(contents:subject)", "refs/tags"}, tagsOutput, nil),
ExpectGitArgs([]string{"tag", "--list", "-n", "--sort=-creatordate"}, tagsOutput, nil),
expectedTags: []*models.Tag{
{Name: "tag1", Message: "this is my message", IsAnnotated: true},
{Name: "tag2", Message: "", IsAnnotated: false},
{Name: "tag3", Message: "this is my other message", IsAnnotated: true},
{Name: "tag1", Message: "this is my message"},
{Name: "tag2", Message: ""},
{Name: "tag3", Message: "this is my other message"},
},
expectedError: nil,
},
Expand Down
4 changes: 0 additions & 4 deletions pkg/commands/models/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@ package models
// Tag : A git tag
type Tag struct {
Name string

// this is either the first line of the message of an annotated tag, or the
// first line of a commit message for a lightweight tag
Message string

// true if this is an annotated tag, false if it's a lightweight tag
IsAnnotated bool
}

func (t *Tag) FullRefName() string {
Expand Down
7 changes: 6 additions & 1 deletion pkg/gui/controllers/tags_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ func (self *TagsController) GetOnRenderToMain() func() {
}

func (self *TagsController) getTagInfo(tag *models.Tag) string {
if tag.IsAnnotated {
tagIsAnnotated, err := self.c.Git().Tag.IsTagAnnotated(tag.Name)
if err != nil {
self.c.Log.Warnf("Error checking if tag is annotated: %v", err)
}

if tagIsAnnotated {
info := fmt.Sprintf("%s: %s", self.c.Tr.AnnotatedTag, style.AttrBold.Sprint(style.FgYellow.Sprint(tag.Name)))
output, err := self.c.Git().Tag.ShowAnnotationInfo(tag.Name)
if err == nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/integration/tests/tag/crud_annotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ var CrudAnnotated = NewIntegrationTest(NewIntegrationTestArgs{
Lines(
MatchesRegexp(`new-tag.*message`).IsSelected(),
).
Tap(func() {
t.Views().Main().ContainsLines(
Equals("Annotated tag: new-tag"),
Equals(""),
Contains("Tagger:"),
Contains("TaggerDate:"),
Equals(""),
Equals("message"),
Equals(""),
Equals("---"),
)
}).
Press(keys.Universal.Push).
Tap(func() {
t.ExpectPopup().Prompt().
Expand Down
7 changes: 7 additions & 0 deletions pkg/integration/tests/tag/crud_lightweight.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ var CrudLightweight = NewIntegrationTest(NewIntegrationTestArgs{
Lines(
MatchesRegexp(`new-tag.*initial commit`).IsSelected(),
).
Tap(func() {
t.Views().Main().ContainsLines(
Equals("Lightweight tag: new-tag"),
Equals(""),
Equals("---"),
)
}).
PressEnter().
Tap(func() {
// view the commits of the tag
Expand Down