xonecas.io
8356
Wx

www-xonecas-io / files

www-xonecas-io / internal / project / project.go

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