mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-05-13 22:17:05 +02:00
Simplify sorting of git tags by using git's functions
This commit is contained in:
parent
94b52af661
commit
cb78cf7de4
pkg
@ -1,28 +1,16 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
"github.com/jesseduffield/lazygit/pkg/commands/models"
|
||||||
"github.com/jesseduffield/lazygit/pkg/utils"
|
"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) {
|
func (c *GitCommand) GetTags() ([]*models.Tag, error) {
|
||||||
// get remote branches
|
// get remote branches, sorted by creation date (descending)
|
||||||
remoteBranchesStr, err := c.OSCommand.RunCommandWithOutput(`git tag --list`)
|
// 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -37,59 +25,10 @@ func (c *GitCommand) GetTags() ([]*models.Tag, error) {
|
|||||||
// first step is to get our remotes from go-git
|
// first step is to get our remotes from go-git
|
||||||
tags := make([]*models.Tag, len(split))
|
tags := make([]*models.Tag, len(split))
|
||||||
for i, tagName := range split {
|
for i, tagName := range split {
|
||||||
|
|
||||||
tags[i] = &models.Tag{
|
tags[i] = &models.Tag{
|
||||||
Name: tagName,
|
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
|
return tags, nil
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,6 @@ type GuiConfig struct {
|
|||||||
ShowRandomTip bool `yaml:"showRandomTip"`
|
ShowRandomTip bool `yaml:"showRandomTip"`
|
||||||
ShowCommandLog bool `yaml:"showCommandLog"`
|
ShowCommandLog bool `yaml:"showCommandLog"`
|
||||||
CommandLogSize int `yaml:"commandLogSize"`
|
CommandLogSize int `yaml:"commandLogSize"`
|
||||||
SortTagsAscending bool `yaml:"sortTagsAscending"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ThemeConfig struct {
|
type ThemeConfig struct {
|
||||||
@ -308,7 +307,6 @@ func GetDefaultConfig() *UserConfig {
|
|||||||
ShowFileTree: false,
|
ShowFileTree: false,
|
||||||
ShowRandomTip: true,
|
ShowRandomTip: true,
|
||||||
CommandLogSize: 8,
|
CommandLogSize: 8,
|
||||||
SortTagsAscending: false,
|
|
||||||
},
|
},
|
||||||
Git: GitConfig{
|
Git: GitConfig{
|
||||||
Paging: PagingConfig{
|
Paging: PagingConfig{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user