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