1package repo
2
3import (
4 "context"
5 "crypto/rand"
6 "fmt"
7 "math/big"
8
9 "github.com/go-git/go-git/v5/plumbing/object"
10)
11
12func uniqueEmails(commits []*object.Commit) []string {
13 emails := make(map[string]struct{})
14 for _, commit := range commits {
15 if commit.Author.Email != "" {
16 emails[commit.Author.Email] = struct{}{}
17 }
18 if commit.Committer.Email != "" {
19 emails[commit.Committer.Email] = struct{}{}
20 }
21 }
22 var uniqueEmails []string
23 for email := range emails {
24 uniqueEmails = append(uniqueEmails, email)
25 }
26 return uniqueEmails
27}
28
29func balanceIndexItems(commitCount, branchCount, tagCount, fileCount int) (commitsTrunc int, branchesTrunc int, tagsTrunc int) {
30 if commitCount == 0 && tagCount == 0 && branchCount == 0 {
31 return
32 }
33
34 // typically 1 item on right side = 2 files in height
35 availableSpace := fileCount / 2
36
37 // clamp tagcount
38 if tagCount > 0 {
39 tagsTrunc = 1
40 availableSpace -= 1 // an extra subtracted for headers etc.
41 }
42
43 // clamp branchcount
44 if branchCount > 0 {
45 branchesTrunc = min(max(branchCount, 1), 3)
46 availableSpace -= branchesTrunc // an extra subtracted for headers etc.
47 }
48
49 // show
50 if commitCount > 0 {
51 commitsTrunc = max(availableSpace, 3)
52 }
53
54 return
55}
56
57// emailToDidOrHandle takes an emailToDidMap from db.GetEmailToDid
58// and resolves all dids to handles and returns a new map[string]string
59func emailToDidOrHandle(r *Repo, emailToDidMap map[string]string) map[string]string {
60 if emailToDidMap == nil {
61 return nil
62 }
63
64 var dids []string
65 for _, v := range emailToDidMap {
66 dids = append(dids, v)
67 }
68 resolvedIdents := r.idResolver.ResolveIdents(context.Background(), dids)
69
70 didHandleMap := make(map[string]string)
71 for _, identity := range resolvedIdents {
72 if !identity.Handle.IsInvalidHandle() {
73 didHandleMap[identity.DID.String()] = fmt.Sprintf("@%s", identity.Handle.String())
74 } else {
75 didHandleMap[identity.DID.String()] = identity.DID.String()
76 }
77 }
78
79 // Create map of email to didOrHandle for commit display
80 emailToDidOrHandle := make(map[string]string)
81 for email, did := range emailToDidMap {
82 if didOrHandle, ok := didHandleMap[did]; ok {
83 emailToDidOrHandle[email] = didOrHandle
84 }
85 }
86
87 return emailToDidOrHandle
88}
89
90func randomString(n int) string {
91 const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
92 result := make([]byte, n)
93
94 for i := 0; i < n; i++ {
95 n, _ := rand.Int(rand.Reader, big.NewInt(int64(len(letters))))
96 result[i] = letters[n.Int64()]
97 }
98
99 return string(result)
100}