-
Notifications
You must be signed in to change notification settings - Fork 4
/
repository.go
57 lines (50 loc) · 1.58 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package pulse
import (
"cmp"
"time"
)
// Repository represents a git repository. A coding session
// might open files across any number of repos. The files of
// the coding session are later grouped by repository.
type Repository struct {
Name string `json:"name"`
Files Files `json:"files"`
Duration time.Duration `json:"duration"`
}
// merge takes two repositories, merges them, and returns the result.
func (r Repository) merge(b Repository) Repository {
return Repository{
Name: cmp.Or(r.Name, b.Name),
Files: r.Files.merge(b.Files),
Duration: r.Duration + b.Duration,
}
}
// Repositories represents a list of git repositories.
type Repositories []Repository
// repositoriesByName takes a slice of repositories and returns a map
// where the repository name is the key and the repository the value.
func repositoriesByName(repos Repositories) map[string]Repository {
nameRepoMap := make(map[string]Repository)
for _, repo := range repos {
nameRepoMap[repo.Name] = repo
}
return nameRepoMap
}
// merge takes two lists of repositories, merges them, and returns the result.
func (r Repositories) merge(b Repositories) Repositories {
aNames, bNames := repositoriesByName(r), repositoriesByName(b)
allNames := make(map[string]bool)
for name := range aNames {
allNames[name] = true
}
for name := range bNames {
allNames[name] = true
}
mergedRepositories := make([]Repository, 0)
for name := range allNames {
aRepo := aNames[name]
bRepo := bNames[name]
mergedRepositories = append(mergedRepositories, aRepo.merge(bRepo))
}
return mergedRepositories
}