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 "github.com/go-git/go-git/v5/plumbing/object"
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 []*object.Commit) []string {
46 emails := make(map[string]struct{})
47 for _, commit := range commits {
48 if commit.Author.Email != "" {
49 emails[commit.Author.Email] = struct{}{}
50 }
51 if commit.Committer.Email != "" {
52 emails[commit.Committer.Email] = struct{}{}
53 }
54 }
55 var uniqueEmails []string
56 for email := range emails {
57 uniqueEmails = append(uniqueEmails, email)
58 }
59 return uniqueEmails
60}
61
62func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) {
63 if commitCount == 0 && tagCount == 0 && branchCount == 0 {
64 return
65 }
66
67 // typically 1 item on right side = 2 files in height
68 availableSpace := fileCount / 2
69
70 // clamp tagcount
71 if tagCount > 0 {
72 tagsTrunc = 1
73 availableSpace -= 1 // an extra subtracted for headers etc.
74 }
75
76 // clamp branchcount
77 if branchCount > 0 {
78 branchesTrunc = min(max(branchCount, 1), 3)
79 availableSpace -= branchesTrunc // an extra subtracted for headers etc.
80 }
81
82 // show
83 if commitCount > 0 {
84 commitsTrunc = max(availableSpace, 3)
85 }
86
87 return
88}
89
90// grab pipelines from DB and munge that into a hashmap with commit sha as key
91//
92// golang is so blessed that it requires 35 lines of imperative code for this
93func getPipelineStatuses(
94 d *db.DB,
95 repo *models.Repo,
96 shas []string,
97) (map[string]models.Pipeline, error) {
98 m := make(map[string]models.Pipeline)
99
100 if len(shas) == 0 {
101 return m, nil
102 }
103
104 ps, err := db.GetPipelineStatuses(
105 d,
106 len(shas),
107 db.FilterEq("repo_owner", repo.Did),
108 db.FilterEq("repo_name", repo.Name),
109 db.FilterEq("knot", repo.Knot),
110 db.FilterIn("sha", shas),
111 )
112 if err != nil {
113 return nil, err
114 }
115
116 for _, p := range ps {
117 m[p.Sha] = p
118 }
119
120 return m, nil
121}