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