| 1 | package git |
| 2 | |
| 3 | import ( |
| 4 | "unicode" |
| 5 | ) |
| 6 | |
| 7 | // File represents the status of a file in an index or work tree |
| 8 | type File struct { |
| 9 | Name string |
| 10 | AbsPath string |
| 11 | X FileStatus |
| 12 | Y FileStatus |
| 13 | } |
| 14 | |
| 15 | // FileStatus is the short representation of state of a file |
| 16 | type FileStatus byte |
| 17 | |
| 18 | const ( |
| 19 | // StatusNotupdated says file not updated |
| 20 | StatusNotupdated FileStatus = ' ' |
| 21 | // StatusModified says file is modifed |
| 22 | StatusModified FileStatus = 'M' |
| 23 | // StatusModifiedUntracked says file is modifed and un-tracked |
| 24 | StatusModifiedUntracked FileStatus = 'm' |
| 25 | // StatusAdded says file is added to index |
| 26 | StatusAdded FileStatus = 'A' |
| 27 | // StatusDeleted says file is deleted |
| 28 | StatusDeleted FileStatus = 'D' |
| 29 | // StatusRenamed says file is renamed |
| 30 | StatusRenamed FileStatus = 'R' |
| 31 | // StatusCopied says file is copied |
| 32 | StatusCopied FileStatus = 'C' |
| 33 | // StatusUpdated says file is updated |
| 34 | StatusUpdated FileStatus = 'U' |
| 35 | // StatusUntracked says file is untraced |
| 36 | StatusUntracked FileStatus = '?' |
| 37 | // StatusIgnored says file is ignored |
| 38 | StatusIgnored FileStatus = '!' |
| 39 | ) |
| 40 | |
| 41 | // FilesAlphabetical slice is the re-ordered *File slice that sorted according |
| 42 | // to alphabetical order (A-Z) |
| 43 | type FilesAlphabetical []*File |
| 44 | |
| 45 | // Len is the interface implementation for Alphabetical sorting function |
| 46 | func (s FilesAlphabetical) Len() int { return len(s) } |
| 47 | |
| 48 | // Swap is the interface implementation for Alphabetical sorting function |
| 49 | func (s FilesAlphabetical) Swap(i, j int) { s[i], s[j] = s[j], s[i] } |
| 50 | |
| 51 | // Less is the interface implementation for Alphabetical sorting function |
| 52 | func (s FilesAlphabetical) Less(i, j int) bool { |
| 53 | iRunes := []rune(s[i].Name) |
| 54 | jRunes := []rune(s[j].Name) |
| 55 | |
| 56 | max := len(iRunes) |
| 57 | if max > len(jRunes) { |
| 58 | max = len(jRunes) |
| 59 | } |
| 60 | |
| 61 | for idx := 0; idx < max; idx++ { |
| 62 | ir := iRunes[idx] |
| 63 | jr := jRunes[idx] |
| 64 | |
| 65 | lir := unicode.ToLower(ir) |
| 66 | ljr := unicode.ToLower(jr) |
| 67 | |
| 68 | if lir != ljr { |
| 69 | return lir < ljr |
| 70 | } |
| 71 | |
| 72 | // the lowercase runes are the same, so compare the original |
| 73 | if ir != jr { |
| 74 | return ir < jr |
| 75 | } |
| 76 | } |
| 77 | return false |
| 78 | } |