this repo has no description
1package repo 2 3import ( 4 "slices" 5 "sort" 6 "strings" 7 8 "tangled.org/core/appview/db" 9 "tangled.org/core/appview/models" 10 "tangled.org/core/types" 11) 12 13func sortFiles(files []types.NiceTree) { 14 sort.Slice(files, func(i, j int) bool { 15 iIsFile := files[i].IsFile() 16 jIsFile := files[j].IsFile() 17 if iIsFile != jIsFile { 18 return !iIsFile 19 } 20 return files[i].Name < files[j].Name 21 }) 22} 23 24func sortBranches(branches []types.Branch) { 25 slices.SortFunc(branches, func(a, b types.Branch) int { 26 if a.IsDefault { 27 return -1 28 } 29 if b.IsDefault { 30 return 1 31 } 32 if a.Commit != nil && b.Commit != nil { 33 if a.Commit.Committer.When.Before(b.Commit.Committer.When) { 34 return 1 35 } else { 36 return -1 37 } 38 } 39 return strings.Compare(a.Name, b.Name) 40 }) 41} 42 43func uniqueEmails(commits []types.Commit) []string { 44 emails := make(map[string]struct{}) 45 for _, commit := range commits { 46 if commit.Author.Email != "" { 47 emails[commit.Author.Email] = struct{}{} 48 } 49 if commit.Committer.Email != "" { 50 emails[commit.Committer.Email] = struct{}{} 51 } 52 } 53 var uniqueEmails []string 54 for email := range emails { 55 uniqueEmails = append(uniqueEmails, email) 56 } 57 return uniqueEmails 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}