| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "unicode" |
| 5 | |
| 6 | "github.com/go-git/go-git/v5/plumbing/object" |
| 7 | ) |
| 8 | |
| 9 | // Alphabetical slice is the re-ordered *Repository slice that sorted according |
| 10 | // to alphabetical order (A-Z) |
| 11 | type Alphabetical []*Repository |
| 12 | |
| 13 | // Len is the interface implementation for Alphabetical sorting function |
| 14 | func (s Alphabetical) Len() int { return len(s) } |
| 15 | |
| 16 | // Swap is the interface implementation for Alphabetical sorting function |
| 17 | func (s Alphabetical) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 18 | |
| 19 | // Less is the interface implementation for Alphabetical sorting function |
| 20 | func (s Alphabetical) Less(i, j int) bool { |
| 21 | iRunes := []rune(s[i].Name) |
| 22 | jRunes := []rune(s[j].Name) |
| 23 | |
| 24 | max := len(iRunes) |
| 25 | if max > len(jRunes) { |
| 26 | max = len(jRunes) |
| 27 | } |
| 28 | |
| 29 | for idx := 0; idx < max; idx++ { |
| 30 | ir := iRunes[idx] |
| 31 | jr := jRunes[idx] |
| 32 | |
| 33 | lir := unicode.ToLower(ir) |
| 34 | ljr := unicode.ToLower(jr) |
| 35 | |
| 36 | if lir != ljr { |
| 37 | return lir < ljr |
| 38 | } |
| 39 | |
| 40 | // the lowercase runes are the same, so compare the original |
| 41 | if ir != jr { |
| 42 | return ir < jr |
| 43 | } |
| 44 | } |
| 45 | return false |
| 46 | } |
| 47 | |
| 48 | // LastModified slice is the re-ordered *Repository slice that sorted according |
| 49 | // to last modified date of the repository directory |
| 50 | type LastModified []*Repository |
| 51 | |
| 52 | // Len is the interface implementation for LastModified sorting function |
| 53 | func (s LastModified) Len() int { return len(s) } |
| 54 | |
| 55 | // Swap is the interface implementation for LastModified sorting function |
| 56 | func (s LastModified) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 57 | |
| 58 | // Less is the interface implementation for LastModified sorting function |
| 59 | func (s LastModified) Less(i, j int) bool { |
| 60 | return s[i].ModTime.Unix() > s[j].ModTime.Unix() |
| 61 | } |
| 62 | |
| 63 | // Less returns a comparison between to repositories by name |
| 64 | func Less(ri, rj *Repository) bool { |
| 65 | iRunes := []rune(ri.Name) |
| 66 | jRunes := []rune(rj.Name) |
| 67 | |
| 68 | max := len(iRunes) |
| 69 | if max > len(jRunes) { |
| 70 | max = len(jRunes) |
| 71 | } |
| 72 | |
| 73 | for idx := 0; idx < max; idx++ { |
| 74 | ir := iRunes[idx] |
| 75 | jr := jRunes[idx] |
| 76 | |
| 77 | lir := unicode.ToLower(ir) |
| 78 | ljr := unicode.ToLower(jr) |
| 79 | |
| 80 | if lir != ljr { |
| 81 | return lir < ljr |
| 82 | } |
| 83 | |
| 84 | // the lowercase runes are the same, so compare the original |
| 85 | if ir != jr { |
| 86 | return ir < jr |
| 87 | } |
| 88 | } |
| 89 | return false |
| 90 | } |
| 91 | |
| 92 | // CommitTime slice is the re-ordered *object.Commit slice that sorted according |
| 93 | // commit date |
| 94 | type CommitTime []*object.Commit |
| 95 | |
| 96 | // Len is the interface implementation for LastModified sorting function |
| 97 | func (s CommitTime) Len() int { return len(s) } |
| 98 | |
| 99 | // Swap is the interface implementation for LastModified sorting function |
| 100 | func (s CommitTime) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 101 | |
| 102 | // Less is the interface implementation for LastModified sorting function |
| 103 | func (s CommitTime) Less(i, j int) bool { |
| 104 | return s[i].Author.When.Unix() > s[j].Author.When.Unix() |
| 105 | } |