forked from tangled.org/core
Monorepo for Tangled — https://tangled.org
at master 2.5 kB view raw
1package repo 2 3import ( 4 "maps" 5 "slices" 6 "sort" 7 "strings" 8 9 "tangled.org/core/appview/db" 10 "tangled.org/core/appview/models" 11 "tangled.org/core/orm" 12 "tangled.org/core/types" 13) 14 15func sortFiles(files []types.NiceTree) { 16 sort.Slice(files, func(i, j int) bool { 17 iIsFile := files[i].IsFile() 18 jIsFile := files[j].IsFile() 19 if iIsFile != jIsFile { 20 return !iIsFile 21 } 22 return files[i].Name < files[j].Name 23 }) 24} 25 26func sortBranches(branches []types.Branch) { 27 slices.SortFunc(branches, func(a, b types.Branch) int { 28 if a.IsDefault { 29 return -1 30 } 31 if b.IsDefault { 32 return 1 33 } 34 if a.Commit != nil && b.Commit != nil { 35 if a.Commit.Committer.When.Before(b.Commit.Committer.When) { 36 return 1 37 } else { 38 return -1 39 } 40 } 41 return strings.Compare(a.Name, b.Name) 42 }) 43} 44 45func uniqueEmails(commits []types.Commit) []string { 46 emails := make(map[string]struct{}) 47 for _, commit := range commits { 48 emails[commit.Author.Email] = struct{}{} 49 emails[commit.Committer.Email] = struct{}{} 50 for _, c := range commit.CoAuthors() { 51 emails[c.Email] = struct{}{} 52 } 53 } 54 55 // delete empty emails if any, from the set 56 delete(emails, "") 57 58 return slices.Collect(maps.Keys(emails)) 59} 60 61func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) { 62 if commitCount == 0 && tagCount == 0 && branchCount == 0 { 63 return 64 } 65 66 // typically 1 item on right side = 2 files in height 67 availableSpace := fileCount / 2 68 69 // clamp tagcount 70 if tagCount > 0 { 71 tagsTrunc = 1 72 availableSpace -= 1 // an extra subtracted for headers etc. 73 } 74 75 // clamp branchcount 76 if branchCount > 0 { 77 branchesTrunc = min(max(branchCount, 1), 3) 78 availableSpace -= branchesTrunc // an extra subtracted for headers etc. 79 } 80 81 // show 82 if commitCount > 0 { 83 commitsTrunc = max(availableSpace, 3) 84 } 85 86 return 87} 88 89// grab pipelines from DB and munge that into a hashmap with commit sha as key 90// 91// golang is so blessed that it requires 35 lines of imperative code for this 92func getPipelineStatuses( 93 d *db.DB, 94 repo *models.Repo, 95 shas []string, 96) (map[string]models.Pipeline, error) { 97 m := make(map[string]models.Pipeline) 98 99 if len(shas) == 0 { 100 return m, nil 101 } 102 103 ps, err := db.GetPipelineStatuses( 104 d, 105 len(shas), 106 orm.FilterEq("repo_owner", repo.Did), 107 orm.FilterEq("repo_name", repo.Name), 108 orm.FilterEq("knot", repo.Knot), 109 orm.FilterIn("sha", shas), 110 ) 111 if err != nil { 112 return nil, err 113 } 114 115 for _, p := range ps { 116 m[p.Sha] = p 117 } 118 119 return m, nil 120}