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