1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-01-30 05:49:15 +02:00

Simplify sorting of git tags by using git's functions

This commit is contained in:
Robert Verst 2021-06-01 18:07:46 +02:00 committed by Jesse Duffield
parent 94b52af661
commit cb78cf7de4
2 changed files with 3 additions and 66 deletions

View File

@ -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
}

View File

@ -39,7 +39,6 @@ type GuiConfig struct {
ShowRandomTip bool `yaml:"showRandomTip"`
ShowCommandLog bool `yaml:"showCommandLog"`
CommandLogSize int `yaml:"commandLogSize"`
SortTagsAscending bool `yaml:"sortTagsAscending"`
}
type ThemeConfig struct {
@ -308,7 +307,6 @@ func GetDefaultConfig() *UserConfig {
ShowFileTree: false,
ShowRandomTip: true,
CommandLogSize: 8,
SortTagsAscending: false,
},
Git: GitConfig{
Paging: PagingConfig{