cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/discovery/files.go
57lines · modecode
| 1 | package discovery |
| 2 | |
| 3 | import "sort" |
| 4 | |
| 5 | func NewFileCommitsFromMap(m map[string][]string) FileCommits { |
| 6 | return FileCommits{pathCommits: m} |
| 7 | } |
| 8 | |
| 9 | type FileCommits struct { |
| 10 | pathCommits map[string][]string |
| 11 | } |
| 12 | |
| 13 | func (fc FileCommits) Results() (results []File) { |
| 14 | for path, commits := range fc.pathCommits { |
| 15 | results = append(results, File{Path: path, Commits: commits}) |
| 16 | } |
| 17 | sort.Slice(results, func(i, j int) bool { |
| 18 | return results[i].Path < results[j].Path |
| 19 | }) |
| 20 | return |
| 21 | } |
| 22 | |
| 23 | func (fc FileCommits) Paths() (paths []string) { |
| 24 | for path := range fc.pathCommits { |
| 25 | paths = append(paths, path) |
| 26 | } |
| 27 | |
| 28 | sort.Strings(paths) |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | func (fc FileCommits) Commits() (commits []string) { |
| 33 | cm := map[string]struct{}{} |
| 34 | for _, cs := range fc.pathCommits { |
| 35 | for _, c := range cs { |
| 36 | cm[c] = struct{}{} |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | for c := range cm { |
| 41 | commits = append(commits, c) |
| 42 | } |
| 43 | |
| 44 | sort.Strings(commits) |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | func (fc FileCommits) HasCommit(commit string) bool { |
| 49 | for _, commits := range fc.pathCommits { |
| 50 | for _, c := range commits { |
| 51 | if c == commit { |
| 52 | return true |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | return false |
| 57 | } |
| 58 | |