Skip to content
181 linesCodeBlameRaw
1//! Who made a repository: the authors of the commits on its default branch,
2//! each matched to their account by an address they confirmed (or their
3//! noreply address), g1t as itself, and anyone else by the name on their
4//! commits. Several addresses of one account count as one contributor.
5
6use std::collections::{BTreeMap, HashMap};
7
8use g1t_contracts::about::{Contributor, ContributorKind, MAX_CONTRIBUTOR_WEEKS, MAX_CONTRIBUTORS, WeekCommits, next_week, week_of};
9use g1t_contracts::accounts::EmailOwner;
10
11/// The address g1t's own commits carry.
12pub const G1T_EMAIL: &str = "g1t@users.noreply.g1t.sh";
13
14/// The addresses g1t's agents and merge queue committed as before they
15/// committed as g1t (2026-10-06): history keeps them, and they are g1t's.
16/// The site matches the same set (apps/web/app/lib/commit-people.ts).
17pub const LEGACY_G1T_EMAILS: [&str; 3] = ["agent@g1t.sh", "queue@g1t.sh", "mergecheck@g1t.sh"];
18
19/// One commit's author, as history gives it.
20#[derive(Clone, Debug)]
21pub struct Authored {
22 pub name: String,
23 pub email: String,
24 /// RFC 3339.
25 pub at: String,
26}
27
28/// Whether a commit's author is g1t itself.
29pub fn is_g1t(author: &Authored) -> bool {
30 let email = author.email.trim();
31 email.eq_ignore_ascii_case(G1T_EMAIL) || LEGACY_G1T_EMAILS.iter().any(|legacy| email.eq_ignore_ascii_case(legacy))
32}
33
34#[derive(Default)]
35struct Tally {
36 kind: Option<ContributorKind>,
37 name: String,
38 username: Option<String>,
39 avatar: Option<String>,
40 commits: u32,
41 first_at: String,
42 last_at: String,
43 weeks: BTreeMap<String, u32>,
44}
45
46/// The contributors, most commits first (then by name), at most
47/// `MAX_CONTRIBUTORS`, the most active `MAX_CONTRIBUTOR_WEEKS` with their
48/// weeks; how many there are; and every commit by week, oldest first, with
49/// the empty weeks between.
50pub fn tally(commits: &[Authored], owners: &HashMap<String, EmailOwner>) -> (Vec<Contributor>, u32, Vec<WeekCommits>) {
51 let mut by_key: HashMap<String, Tally> = HashMap::new();
52 let mut all_weeks: BTreeMap<String, u32> = BTreeMap::new();
53 for commit in commits {
54 let email = commit.email.trim().to_ascii_lowercase();
55 let (key, kind, name, username, avatar) = if is_g1t(commit) {
56 ("g1t".to_owned(), ContributorKind::G1t, "g1t".to_owned(), None, None)
57 } else if let Some(owner) = owners.get(&email) {
58 (format!("user:{}", owner.id), ContributorKind::User, owner.username.clone(), Some(owner.username.clone()), owner.avatar.clone())
59 } else {
60 let key = if email.is_empty() { format!("name:{}", commit.name.trim().to_lowercase()) } else { format!("email:{email}") };
61 (key, ContributorKind::Author, commit.name.trim().to_owned(), None, None)
62 };
63 let tally = by_key.entry(key).or_default();
64 if tally.kind.is_none() {
65 *tally = Tally { kind: Some(kind), name, username, avatar, first_at: commit.at.clone(), last_at: commit.at.clone(), ..Tally::default() };
66 }
67 tally.commits += 1;
68 if commit.at < tally.first_at {
69 tally.first_at = commit.at.clone();
70 }
71 if commit.at > tally.last_at {
72 tally.last_at = commit.at.clone();
73 }
74 if let Some(week) = week_of(&commit.at) {
75 *tally.weeks.entry(week.clone()).or_default() += 1;
76 *all_weeks.entry(week).or_default() += 1;
77 }
78 }
79 let total = by_key.len() as u32;
80 let mut tallies: Vec<Tally> = by_key.into_values().collect();
81 tallies.sort_by(|a, b| b.commits.cmp(&a.commits).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())));
82 tallies.truncate(MAX_CONTRIBUTORS);
83 let contributors = tallies
84 .into_iter()
85 .enumerate()
86 .map(|(rank, tally)| Contributor {
87 kind: tally.kind.unwrap_or(ContributorKind::Author),
88 name: tally.name,
89 username: tally.username,
90 avatar: tally.avatar,
91 commits: tally.commits,
92 first_at: tally.first_at,
93 last_at: tally.last_at,
94 weeks: if rank < MAX_CONTRIBUTOR_WEEKS {
95 tally.weeks.into_iter().map(|(week, commits)| WeekCommits { week, commits }).collect()
96 } else {
97 Vec::new()
98 },
99 })
100 .collect();
101 (contributors, total, filled(&all_weeks))
102}
103
104/// Every week from the first to the last, with none for those without.
105fn filled(weeks: &BTreeMap<String, u32>) -> Vec<WeekCommits> {
106 let (Some(first), Some(last)) = (weeks.keys().next(), weeks.keys().next_back()) else {
107 return Vec::new();
108 };
109 let mut out = Vec::new();
110 let mut week = first.clone();
111 // At most twenty years of weeks, whatever the dates say.
112 while week <= *last && out.len() < 1_100 {
113 out.push(WeekCommits { commits: weeks.get(&week).copied().unwrap_or(0), week: week.clone() });
114 match next_week(&week) {
115 Some(next) => week = next,
116 None => break,
117 }
118 }
119 out
120}
121
122#[cfg(test)]
123mod tests {
124 use super::*;
125
126 fn authored(name: &str, email: &str, at: &str) -> Authored {
127 Authored { name: name.into(), email: email.into(), at: at.into() }
128 }
129
130 #[test]
131 fn authors_are_matched_merged_and_ranked() {
132 let commits = vec![
133 authored("Ada", "ada@example.com", "2026-10-07T10:00:00Z"),
134 authored("Ada L", "ADA@work.example", "2026-09-30T10:00:00Z"),
135 authored("g1t", G1T_EMAIL, "2026-10-06T10:00:00Z"),
136 authored("g1t", G1T_EMAIL, "2026-10-01T10:00:00Z"),
137 authored("g1t", G1T_EMAIL, "2026-09-20T10:00:00Z"),
138 authored("Sam", "sam@example.com", "2026-09-16T10:00:00Z"),
139 ];
140 let ada = EmailOwner { id: "usr_ada".into(), username: "ada".into(), avatar: Some("abc".into()) };
141 let owners = HashMap::from([("ada@example.com".to_owned(), ada.clone()), ("ada@work.example".to_owned(), ada)]);
142 let (contributors, total, weeks) = tally(&commits, &owners);
143 assert_eq!(total, 3);
144 assert_eq!(contributors[0].kind, ContributorKind::G1t);
145 assert_eq!(contributors[0].commits, 3);
146 assert_eq!(contributors[1].username.as_deref(), Some("ada"));
147 assert_eq!(contributors[1].commits, 2, "both of Ada's addresses");
148 assert_eq!(contributors[1].first_at, "2026-09-30T10:00:00Z");
149 assert_eq!(contributors[1].last_at, "2026-10-07T10:00:00Z");
150 assert_eq!(contributors[1].avatar.as_deref(), Some("abc"));
151 assert_eq!(contributors[2].kind, ContributorKind::Author);
152 assert_eq!(contributors[2].name, "Sam");
153 // Weeks of 2026-09-14 through 2026-10-05, none skipped.
154 let named: Vec<(&str, u32)> = weeks.iter().map(|week| (week.week.as_str(), week.commits)).collect();
155 assert_eq!(named, vec![("2026-09-14", 2), ("2026-09-21", 0), ("2026-09-28", 2), ("2026-10-05", 2)]);
156 assert_eq!(contributors[1].weeks.len(), 2);
157 }
158
159 #[test]
160 fn g1t_older_addresses_are_g1t() {
161 let commits = vec![
162 authored("g1t agent", "agent@g1t.sh", "2026-10-01T10:00:00Z"),
163 authored("g1t merge queue", "Queue@g1t.sh", "2026-10-02T10:00:00Z"),
164 authored("g1t", G1T_EMAIL, "2026-10-07T10:00:00Z"),
165 authored("Not g1t", "someone@g1t.sh", "2026-10-07T10:00:00Z"),
166 ];
167 let (contributors, total, _) = tally(&commits, &HashMap::new());
168 assert_eq!(total, 2);
169 assert_eq!(contributors[0].kind, ContributorKind::G1t);
170 assert_eq!(contributors[0].name, "g1t");
171 assert_eq!(contributors[0].commits, 3);
172 assert_eq!(contributors[1].kind, ContributorKind::Author, "only g1t's own addresses");
173 }
174
175 #[test]
176 fn no_history_is_no_one() {
177 let (contributors, total, weeks) = tally(&[], &HashMap::new());
178 assert!(contributors.is_empty() && weeks.is_empty());
179 assert_eq!(total, 0);
180 }
181}