diff options
author | Robert Verst <robert@verst.eu> | 2021-06-01 18:07:46 +0200 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2021-06-05 10:56:46 +1000 |
commit | cb78cf7de4798bf628cf726689d62b18da34a347 (patch) | |
tree | 973a1a499171e4e6be23828c731dbc86426b3b94 /pkg/commands/loading_tags.go | |
parent | 94b52af661dd8b61c60b5801106025e4a81be7b6 (diff) |
Simplify sorting of git tags by using git's functions
Diffstat (limited to 'pkg/commands/loading_tags.go')
-rw-r--r-- | pkg/commands/loading_tags.go | 67 |
1 files changed, 3 insertions, 64 deletions
diff --git a/pkg/commands/loading_tags.go b/pkg/commands/loading_tags.go index 8bd66c936..71704f9c0 100644 --- a/pkg/commands/loading_tags.go +++ b/pkg/commands/loading_tags.go @@ -1,28 +1,16 @@ package commands import ( - "regexp" - "sort" - "strconv" "strings" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/utils" ) -const semverRegex = `v?((\d+\.?)+)([^\d]?.*)` - -func convertToInt(s string) int { - i, err := strconv.Atoi(s) - if err != nil { - return 0 - } - return i -} - func (c *GitCommand) GetTags() ([]*models.Tag, error) { - // get remote branches - remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput(`git tag --list`) + // get remote branches, sorted by creation date (descending) + // see: https://git-scm.com/docs/git-tag#Documentation/git-tag.txt---sortltkeygt + remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput(`git tag --list --sort=-creatordate`) if err != nil { return nil, err } @@ -37,59 +25,10 @@ func (c *GitCommand) GetTags() ([]*models.Tag, error) { // first step is to get our remotes from go-git tags := make([]*models.Tag, len(split)) for i, tagName := range split { - tags[i] = &models.Tag{ Name: tagName, } } - // now lets sort our tags by name numerically - re := regexp.MustCompile(semverRegex) - - sortAsc := c.Config.GetUserConfig().Gui.SortTagsAscending - // the reason this is complicated is because we're both sorting alphabetically - // and when we're dealing with semver strings - sort.Slice(tags, func(i, j int) bool { - var a, b string - if sortAsc { - a = tags[i].Name - b = tags[j].Name - } else { - b = tags[i].Name - a = tags[j].Name - } - - matchA := re.FindStringSubmatch(a) - matchB := re.FindStringSubmatch(b) - - if len(matchA) > 0 && len(matchB) > 0 { - numbersA := strings.Split(matchA[1], ".") - numbersB := strings.Split(matchB[1], ".") - k := 0 - for { - if len(numbersA) == k && len(numbersB) == k { - break - } - if len(numbersA) == k { - return true - } - if len(numbersB) == k { - return false - } - if convertToInt(numbersA[k]) < convertToInt(numbersB[k]) { - return true - } - if convertToInt(numbersA[k]) > convertToInt(numbersB[k]) { - return false - } - k++ - } - - return strings.ToLower(matchA[3]) < strings.ToLower(matchB[3]) - } - - return strings.ToLower(a) < strings.ToLower(b) - }) - return tags, nil } |