@@ -0,0 +1,134 @@
1+// Package project groups mirrored remotes into projects: remotes that share a root commit are one project.
2+package project
3+
4+import (
5+ "fmt"
6+ "path/filepath"
7+ "sort"
8+ "strconv"
9+ "strings"
10+
11+ "xonecas.io/site/internal/repo"
12+)
13+
14+// Source is one remote of a project, read from its mirror.
15+type Source struct {
16+ URL string `json:"url"`
17+ Dir string `json:"dir"`
18+ Branch string `json:"branch"`
19+ Commits int `json:"commits"`
20+ First string `json:"first"` // date of the oldest commit, YYYY-MM-DD
21+ Roots []string `json:"-"`
22+}
23+
24+// Project is one history, with every remote that holds it. Sources[0] is canonical: the most commits.
25+type Project struct {
26+ Name string `json:"name"`
27+ Sources []Source `json:"sources"`
28+}
29+
30+// ReadSource reads what grouping needs from the mirror of url in dir.
31+func ReadSource(url, dir string) (Source, error) {
32+ s := Source{URL: url, Dir: dir}
33+ branch, err := repo.Git(dir, "symbolic-ref", "--short", "HEAD")
34+ if err != nil {
35+ return s, err
36+ }
37+ s.Branch = branch
38+ count, err := repo.Git(dir, "rev-list", "--count", "--all")
39+ if err != nil {
40+ return s, err
41+ }
42+ if s.Commits, _ = strconv.Atoi(count); s.Commits == 0 {
43+ return s, nil
44+ }
45+ roots, err := repo.Git(dir, "rev-list", "--max-parents=0", "--all")
46+ if err != nil {
47+ return s, err
48+ }
49+ s.Roots = strings.Fields(roots)
50+ dates, err := repo.Git(dir, append([]string{"log", "--no-walk", "--format=%as"}, s.Roots...)...)
51+ if err == nil {
52+ d := strings.Fields(dates)
53+ sort.Strings(d)
54+ s.First = d[0]
55+ }
56+ return s, err
57+}
58+
59+// Group joins sources that share a root commit, applies the overrides, and names each project.
60+func Group(sources []Source, o Overrides) ([]Project, error) {
61+ parent := make([]int, len(sources))
62+ for i := range parent {
63+ parent[i] = i
64+ }
65+ var find func(int) int
66+ find = func(i int) int {
67+ if parent[i] != i {
68+ parent[i] = find(parent[i])
69+ }
70+ return parent[i]
71+ }
72+ byKey := map[string]int{}
73+ for i, s := range sources {
74+ byKey[s.URL] = i
75+ for _, root := range s.Roots {
76+ if j, ok := byKey[root]; ok {
77+ parent[find(i)] = find(j)
78+ } else {
79+ byKey[root] = i
80+ }
81+ }
82+ }
83+ for _, m := range o.Merge {
84+ i, ok1 := byKey[m[0]]
85+ j, ok2 := byKey[m[1]]
86+ if !ok1 || !ok2 {
87+ return nil, fmt.Errorf("merge %s %s: both must be remote URLs in sources", m[0], m[1])
88+ }
89+ parent[find(i)] = find(j)
90+ }
91+
92+ groups := map[int][]Source{}
93+ for i, s := range sources {
94+ groups[find(i)] = append(groups[find(i)], s)
95+ }
96+ projects := make([]Project, 0, len(groups))
97+ for _, g := range groups {
98+ sort.SliceStable(g, func(a, b int) bool { return g[a].Commits > g[b].Commits })
99+ projects = append(projects, Project{Name: name(g[0].URL), Sources: g})
100+ }
101+ sort.Slice(projects, func(a, b int) bool {
102+ pa, pb := projects[a], projects[b]
103+ if pa.Sources[0].First != pb.Sources[0].First {
104+ return pa.Sources[0].First < pb.Sources[0].First
105+ }
106+ return pa.Name < pb.Name
107+ })
108+ eraNames(projects)
109+ for i := range projects {
110+ if to, ok := o.Rename[projects[i].Name]; ok {
111+ projects[i].Name = to
112+ }
113+ }
114+ return projects, nil
115+}
116+
117+// eraNames gives projects that share a name the year they started, except the newest one.
118+func eraNames(projects []Project) {
119+ seen := map[string][]int{}
120+ for i, p := range projects {
121+ seen[p.Name] = append(seen[p.Name], i)
122+ }
123+ for _, idx := range seen {
124+ for _, i := range idx[:len(idx)-1] { // projects are oldest first
125+ if first := projects[i].Sources[0].First; len(first) >= 4 {
126+ projects[i].Name += "-" + first[:4]
127+ }
128+ }
129+ }
130+}
131+
132+func name(url string) string {
133+ return strings.TrimSuffix(filepath.Base(url), ".git")
134+}