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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
""" Generate the AUTHORS.rst file from git commit history.
This module reads git commit logs and produces a formatted list of contributors
grouped by their contribution count, mapping email aliases and GitHub usernames.
"""
from dataclasses import dataclass
import math
import subprocess # noqa: S404
def main() -> None:
""" Generate and print the AUTHORS.rst content. """
contributors = get_git_contributors()
print_contributors(contributors)
# ------------------------------------------------------------------------------
EMAIL_ALIASES: dict[str, str | None] = {
# Maintainers.
"git@mikeboers.com": "github@mikeboers.com",
"mboers@keypics.com": "github@mikeboers.com",
"mikeb@loftysky.com": "github@mikeboers.com",
"mikeb@markmedia.co": "github@mikeboers.com",
"westernx@mikeboers.com": "github@mikeboers.com",
# Junk.
"mark@mark-VirtualBox.(none)": None,
# Aliases.
"a.davoudi@aut.ac.ir": "davoudialireza@gmail.com",
"tcaswell@bnl.gov": "tcaswell@gmail.com",
"xxr3376@gmail.com": "xxr@megvii.com",
"dallan@pha.jhu.edu": "daniel.b.allan@gmail.com",
"61652821+laggykiller@users.noreply.github.com": "chaudominic2@gmail.com",
}
CANONICAL_NAMES: dict[str, str] = {
"caspervdw@gmail.com": "Casper van der Wel",
"daniel.b.allan@gmail.com": "Dan Allan",
"mgoacolou@cls.fr": "Manuel Goacolou",
"mindmark@gmail.com": "Mark Reid",
"moritzkassner@gmail.com": "Moritz Kassner",
"vidartf@gmail.com": "Vidar Tonaas Fauske",
"xxr@megvii.com": "Xinran Xu",
}
GITHUB_USERNAMES: dict[str, str] = {
"billy.shambrook@gmail.com": "billyshambrook",
"daniel.b.allan@gmail.com": "danielballan",
"davoudialireza@gmail.com": "adavoudi",
"github@mikeboers.com": "mikeboers",
"jeremy.laine@m4x.org": "jlaine",
"kalle.litterfeldt@gmail.com": "litterfeldt",
"mindmark@gmail.com": "markreidvfx",
"moritzkassner@gmail.com": "mkassner",
"rush@logic.cz": "radek-senfeld",
"self@brendanlong.com": "brendanlong",
"tcaswell@gmail.com": "tacaswell",
"ulrik.mikaelsson@magine.com": "rawler",
"vidartf@gmail.com": "vidartf",
"willpatera@gmail.com": "willpatera",
"xxr@megvii.com": "xxr3376",
"chaudominic2@gmail.com": "laggykiller",
"wyattblue@auto-editor.com": "WyattBlue",
"Curtis@GreenKey.net": "dotysan",
}
@dataclass
class Contributor:
""" Represents a contributor with their email, names, and GitHub username. """
email: str
names: set[str]
github: str | None = None
commit_count: int = 0
@property
def display_name(self) -> str:
""" Return the formatted display name for the contributor.
Returns:
Comma-separated sorted list of contributor names.
"""
return ", ".join(sorted(self.names))
def format_line(self, bullet: str) -> str:
""" Format the contributor line for RST output.
Args:
bullet: The bullet character to use (- or *).
Returns:
Formatted RST line with contributor info.
"""
if self.github:
return (
f"{bullet} {self.display_name} <{self.email}>; "
f"`@{self.github} <https://github.com/{self.github}>`_"
)
return f"{bullet} {self.display_name} <{self.email}>"
def get_git_contributors() -> dict[str, Contributor]:
""" Parse git log and return contributors grouped by canonical email.
Returns:
Dictionary mapping canonical emails to Contributor objects.
"""
contributors: dict[str, Contributor] = {}
git_log = subprocess.check_output(
["git", "log", "--format=%aN,%aE"], # noqa: S607
text=True,
).splitlines()
for line in git_log:
name, email = line.strip().rsplit(",", 1)
canonical_email = EMAIL_ALIASES.get(email, email)
if not canonical_email:
continue
if canonical_email not in contributors:
contributors[canonical_email] = Contributor(
email=canonical_email,
names=set(),
github=GITHUB_USERNAMES.get(canonical_email),
)
contributor = contributors[canonical_email]
contributor.names.add(name)
contributor.commit_count += 1
for email, canonical_name in CANONICAL_NAMES.items():
if email in contributors:
contributors[email].names = {canonical_name}
return contributors
def print_contributors(contributors: dict[str, Contributor]) -> None:
"""Print contributors grouped by logarithmic order of commits.
Args:
contributors: Dictionary of contributors to print.
"""
print("""\
Contributors
============
All contributors (by number of commits):
""".replace(" ", ""))
sorted_contributors = sorted(
contributors.values(),
key=lambda c: (-c.commit_count, c.email),
)
last_order: int | None = None
block_index = 0
for contributor in sorted_contributors:
# This is the natural log, because of course it should be. ;)
order = int(math.log(contributor.commit_count))
if last_order and last_order != order:
block_index += 1
print()
last_order = order
# The '-' vs '*' is so that Sphinx treats them as different lists, and
# introduces a gap between them.
bullet = "-*"[block_index % 2]
print(contributor.format_line(bullet))
if __name__ == "__main__":
main()
|