| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "sort" |
| 6 | |
| 7 | "github.com/git-lfs/git-lfs/v3/tr" |
| 8 | ) |
| 9 | |
| 10 | // An Extension describes how to manipulate files during smudge and clean. |
| 11 | // Extensions are parsed from the Git config. |
| 12 | type Extension struct { |
| 13 | Name string |
| 14 | Clean string |
| 15 | Smudge string |
| 16 | Priority int |
| 17 | } |
| 18 | |
| 19 | // SortExtensions sorts a map of extensions in ascending order by Priority |
| 20 | func SortExtensions(m map[string]Extension) ([]Extension, error) { |
| 21 | pMap := make(map[int]Extension) |
| 22 | priorities := make([]int, 0, len(m)) |
| 23 | for n, ext := range m { |
| 24 | p := ext.Priority |
| 25 | if _, exist := pMap[p]; exist { |
| 26 | err := errors.New(tr.Tr.Get("duplicate priority %d on %s", p, n)) |
| 27 | return nil, err |
| 28 | } |
| 29 | pMap[p] = ext |
| 30 | priorities = append(priorities, p) |
| 31 | } |
| 32 | |
| 33 | sort.Ints(priorities) |
| 34 | |
| 35 | result := make([]Extension, len(priorities)) |
| 36 | for i, p := range priorities { |
| 37 | result[i] = pMap[p] |
| 38 | } |
| 39 | |
| 40 | return result, nil |
| 41 | } |