gitannals 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- chronicle/__init__.py +6 -0
- chronicle/analyze.py +325 -0
- chronicle/clean.py +331 -0
- chronicle/cli.py +306 -0
- chronicle/config.py +132 -0
- chronicle/features/__init__.py +23 -0
- chronicle/features/_util.py +90 -0
- chronicle/features/churn.py +150 -0
- chronicle/features/contributors.py +100 -0
- chronicle/features/fun_facts.py +213 -0
- chronicle/features/languages.py +135 -0
- chronicle/features/milestones.py +435 -0
- chronicle/features/overview.py +81 -0
- chronicle/features/profile.py +382 -0
- chronicle/features/temporal.py +132 -0
- chronicle/gitio/__init__.py +5 -0
- chronicle/gitio/parse.py +135 -0
- chronicle/gitio/runner.py +120 -0
- chronicle/i18n.py +530 -0
- chronicle/identity.py +58 -0
- chronicle/models.py +80 -0
- chronicle/narrate/__init__.py +7 -0
- chronicle/narrate/client.py +124 -0
- chronicle/narrate/enhance.py +137 -0
- chronicle/narrate/prompt.py +232 -0
- chronicle/narrate/schema.py +133 -0
- chronicle/redact.py +301 -0
- chronicle/render/__init__.py +140 -0
- chronicle/render/charts.py +518 -0
- chronicle/render/templates/_style.css.j2 +80 -0
- chronicle/render/templates/author.html.j2 +318 -0
- chronicle/render/templates/report.html.j2 +407 -0
- gitannals-0.1.0.dist-info/METADATA +251 -0
- gitannals-0.1.0.dist-info/RECORD +37 -0
- gitannals-0.1.0.dist-info/WHEEL +4 -0
- gitannals-0.1.0.dist-info/entry_points.txt +2 -0
- gitannals-0.1.0.dist-info/licenses/LICENSE +21 -0
chronicle/__init__.py
ADDED
chronicle/analyze.py
ADDED
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"""分析编排。
|
|
2
|
+
|
|
3
|
+
两个视角共用同一次 Git 遍历:
|
|
4
|
+
project —— 这个仓库经历了什么
|
|
5
|
+
author —— 这个人在这个仓库里是什么样
|
|
6
|
+
|
|
7
|
+
渲染层拿到的是同一套 JSON 契约;`--no-ai` 与 LLM 模式的区别只落在
|
|
8
|
+
chapters / fun_facts / quote 三个叙事字段上。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import platform
|
|
14
|
+
from collections import Counter
|
|
15
|
+
from collections.abc import Callable
|
|
16
|
+
from dataclasses import dataclass, field
|
|
17
|
+
from datetime import datetime, timezone
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from . import TOOL_NAME, __version__
|
|
22
|
+
from .clean import DEFAULT_IGNORE, filter_noise
|
|
23
|
+
from .config import LlmSettings, Settings
|
|
24
|
+
from .features import (
|
|
25
|
+
churn,
|
|
26
|
+
contributors,
|
|
27
|
+
fun_facts,
|
|
28
|
+
languages,
|
|
29
|
+
milestones,
|
|
30
|
+
overview,
|
|
31
|
+
profile,
|
|
32
|
+
temporal,
|
|
33
|
+
)
|
|
34
|
+
from .gitio import parse, runner
|
|
35
|
+
from .i18n import t
|
|
36
|
+
from .narrate import enhance
|
|
37
|
+
from .redact import redact_report
|
|
38
|
+
|
|
39
|
+
__all__ = ["Options", "analyze", "SCHEMA_VERSION", "AuthorLookupError"]
|
|
40
|
+
|
|
41
|
+
SCHEMA_VERSION = 1
|
|
42
|
+
|
|
43
|
+
ProgressHook = Callable[[str, str], None]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class AuthorLookupError(LookupError):
|
|
47
|
+
"""转发画像模块的查找错误,避免 CLI 直接依赖 features。"""
|
|
48
|
+
|
|
49
|
+
def __init__(self, message: str, candidates: list[str] | None = None) -> None:
|
|
50
|
+
super().__init__(message)
|
|
51
|
+
self.candidates = candidates or []
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@dataclass(slots=True)
|
|
55
|
+
class Options:
|
|
56
|
+
repo: str | Path = "."
|
|
57
|
+
since: str | None = None
|
|
58
|
+
until: str | None = None
|
|
59
|
+
all_branches: bool = False
|
|
60
|
+
follow_renames: bool = False
|
|
61
|
+
anon: bool = False
|
|
62
|
+
lang: str = "zh"
|
|
63
|
+
tone: str = "tech"
|
|
64
|
+
ignore: list[str] = field(default_factory=list)
|
|
65
|
+
top_files: int = 15
|
|
66
|
+
view: str = "project"
|
|
67
|
+
author: str | None = None
|
|
68
|
+
author_aliases: dict[str, list[str]] = field(default_factory=dict)
|
|
69
|
+
llm: LlmSettings | None = None
|
|
70
|
+
|
|
71
|
+
def as_settings(self) -> Settings:
|
|
72
|
+
return Settings(
|
|
73
|
+
lang=self.lang,
|
|
74
|
+
tone=self.tone,
|
|
75
|
+
ignore=list(self.ignore),
|
|
76
|
+
llm=self.llm or LlmSettings(enabled=False),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _noop(_stage: str, _detail: str) -> None:
|
|
81
|
+
return None
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def analyze(options: Options, progress: ProgressHook | None = None) -> dict[str, Any]:
|
|
85
|
+
emit = progress or _noop
|
|
86
|
+
|
|
87
|
+
emit("repo", "定位仓库根目录")
|
|
88
|
+
root = runner.resolve_repo(options.repo)
|
|
89
|
+
repo_info = runner.repo_meta(root)
|
|
90
|
+
|
|
91
|
+
emit("read", "遍历提交历史")
|
|
92
|
+
raw = list(
|
|
93
|
+
parse.iter_raw_commits(
|
|
94
|
+
root,
|
|
95
|
+
since=options.since,
|
|
96
|
+
until=options.until,
|
|
97
|
+
all_branches=options.all_branches,
|
|
98
|
+
follow_renames=options.follow_renames,
|
|
99
|
+
)
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
emit("clean", f"清洗噪音文件({len(raw)} 条提交)")
|
|
103
|
+
commits = filter_noise(raw, list(DEFAULT_IGNORE) + list(options.ignore))
|
|
104
|
+
|
|
105
|
+
if options.view == "author":
|
|
106
|
+
report = _author_report(commits, root, repo_info, options, emit)
|
|
107
|
+
else:
|
|
108
|
+
report = _project_report(commits, root, repo_info, options, emit)
|
|
109
|
+
|
|
110
|
+
# 脱敏必须在 LLM 与渲染之前:它同时守住「发出去的报告」和「发出去的 prompt」
|
|
111
|
+
if options.anon:
|
|
112
|
+
primary = (report.get("meta", {}).get("author") or {}).get("email")
|
|
113
|
+
report = redact_report(report, commits, primary_email=primary)
|
|
114
|
+
|
|
115
|
+
if options.llm is not None and options.llm.enabled and report.get("chapters"):
|
|
116
|
+
report = enhance(
|
|
117
|
+
report, options.as_settings(), progress=lambda msg: emit("narrate", msg)
|
|
118
|
+
)
|
|
119
|
+
elif options.llm is not None:
|
|
120
|
+
report["meta"]["ai"] = {"used": False, "reason": "disabled"}
|
|
121
|
+
else:
|
|
122
|
+
report["meta"]["ai"] = {"used": False, "reason": "not_requested"}
|
|
123
|
+
|
|
124
|
+
return report
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
# ------------------------------------------------------------------ 项目视角
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def _project_report(
|
|
131
|
+
commits: list, root: Path, repo_info: dict, options: Options, emit: ProgressHook
|
|
132
|
+
) -> dict[str, Any]:
|
|
133
|
+
report: dict[str, Any] = {
|
|
134
|
+
"schema_version": SCHEMA_VERSION,
|
|
135
|
+
"view": "project",
|
|
136
|
+
"meta": _build_meta(root, repo_info, options, commits),
|
|
137
|
+
"warnings": [],
|
|
138
|
+
}
|
|
139
|
+
if not commits:
|
|
140
|
+
report.update(_empty_payload(options))
|
|
141
|
+
return report
|
|
142
|
+
|
|
143
|
+
emit("overview", "统计全貌指标")
|
|
144
|
+
report["overview"] = overview.build(commits)
|
|
145
|
+
|
|
146
|
+
emit("timeline", "聚合时间序列")
|
|
147
|
+
timeline = temporal.build_timeline(commits)
|
|
148
|
+
report["timeline"] = timeline
|
|
149
|
+
report["rhythm"] = temporal.build_rhythm(commits, lang=options.lang)
|
|
150
|
+
|
|
151
|
+
emit("churn", "计算文件修罗场评分")
|
|
152
|
+
report["churn"] = churn.build(commits, top_files=options.top_files)
|
|
153
|
+
report["languages"] = languages.build(commits)
|
|
154
|
+
report["contributors"] = contributors.build(commits)
|
|
155
|
+
|
|
156
|
+
emit("chapters", "检测突变节点并切分章节")
|
|
157
|
+
bundle = milestones.build_chapters(
|
|
158
|
+
commits, timeline["points"], lang=options.lang, granularity=timeline["granularity"]
|
|
159
|
+
)
|
|
160
|
+
report["chapters"] = bundle["chapters"]
|
|
161
|
+
report["breakpoints"] = bundle["breakpoints"]
|
|
162
|
+
report["milestones"] = milestones.build_events(
|
|
163
|
+
commits, bundle["chapters"], lang=options.lang, limit=12
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
emit("facts", "挖考古彩蛋")
|
|
167
|
+
report["fun_facts"] = fun_facts.build_facts(commits, lang=options.lang)
|
|
168
|
+
report["quote"] = fun_facts.build_quote(commits, lang=options.lang, tone=options.tone)
|
|
169
|
+
|
|
170
|
+
_append_data_warnings(report, options)
|
|
171
|
+
return report
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
# ------------------------------------------------------------------ 个人视角
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _author_report(
|
|
178
|
+
commits: list, root: Path, repo_info: dict, options: Options, emit: ProgressHook
|
|
179
|
+
) -> dict[str, Any]:
|
|
180
|
+
report: dict[str, Any] = {
|
|
181
|
+
"schema_version": SCHEMA_VERSION,
|
|
182
|
+
"view": "author",
|
|
183
|
+
"meta": _build_meta(root, repo_info, options, commits),
|
|
184
|
+
"warnings": [],
|
|
185
|
+
}
|
|
186
|
+
if not commits:
|
|
187
|
+
report.update(_empty_payload(options))
|
|
188
|
+
return report
|
|
189
|
+
|
|
190
|
+
emit("author", "定位贡献者")
|
|
191
|
+
query = options.author
|
|
192
|
+
if not query:
|
|
193
|
+
counts = Counter((c.email or c.author).lower() for c in commits)
|
|
194
|
+
query = counts.most_common(1)[0][0]
|
|
195
|
+
report["warnings"].append(t("ui.warn.default_author", options.lang, who=query))
|
|
196
|
+
query, aliases = profile.resolve_aliases(query, options.author_aliases)
|
|
197
|
+
try:
|
|
198
|
+
key, name, email, subset = profile.resolve_author(
|
|
199
|
+
commits, query, aliases=aliases, lang=options.lang
|
|
200
|
+
)
|
|
201
|
+
except profile.AuthorLookupError as exc:
|
|
202
|
+
raise AuthorLookupError(str(exc), exc.candidates) from exc
|
|
203
|
+
|
|
204
|
+
authors_total = len({(c.email or c.author).lower() for c in commits})
|
|
205
|
+
report["meta"]["author"] = {"key": key, "name": name, "email": email}
|
|
206
|
+
|
|
207
|
+
emit("persona", f"统计 {name} 的画像")
|
|
208
|
+
report["persona"] = profile.build_persona(
|
|
209
|
+
subset,
|
|
210
|
+
commits,
|
|
211
|
+
key=key,
|
|
212
|
+
name=name,
|
|
213
|
+
email=email,
|
|
214
|
+
lang=options.lang,
|
|
215
|
+
repo_authors=authors_total,
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
emit("timeline", "聚合个人时间序列")
|
|
219
|
+
report["timeline"] = temporal.build_timeline(subset)
|
|
220
|
+
report["rhythm"] = temporal.build_rhythm(subset, lang=options.lang)
|
|
221
|
+
|
|
222
|
+
emit("chapters", "对齐项目阶段")
|
|
223
|
+
project_timeline = temporal.build_timeline(commits)
|
|
224
|
+
bundle = milestones.build_chapters(
|
|
225
|
+
commits,
|
|
226
|
+
project_timeline["points"],
|
|
227
|
+
lang=options.lang,
|
|
228
|
+
granularity=project_timeline["granularity"],
|
|
229
|
+
)
|
|
230
|
+
report["chapters"] = profile.chapter_participation(
|
|
231
|
+
bundle["chapters"], subset, lang=options.lang
|
|
232
|
+
)
|
|
233
|
+
report["milestones"] = milestones.build_events(
|
|
234
|
+
subset, bundle["chapters"], lang=options.lang, limit=8
|
|
235
|
+
)
|
|
236
|
+
|
|
237
|
+
emit("peers", "计算协作关系")
|
|
238
|
+
report["peers"] = profile.build_peers(commits, key, aliases=aliases)
|
|
239
|
+
|
|
240
|
+
emit("facts", "挖个人彩蛋")
|
|
241
|
+
report["fun_facts"] = fun_facts.build_facts(
|
|
242
|
+
subset, lang=options.lang, author_label=name
|
|
243
|
+
)
|
|
244
|
+
report["quote"] = profile.author_quote(
|
|
245
|
+
report["persona"], lang=options.lang, tone=options.tone
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
if report["persona"]["span_days"] < 7:
|
|
249
|
+
report["warnings"].append(t("ui.warn.short_span", options.lang))
|
|
250
|
+
if len(subset) < 10:
|
|
251
|
+
report["warnings"].append(t("ui.warn.few_personal", options.lang))
|
|
252
|
+
if report["persona"]["share"] < 30 and authors_total > 1:
|
|
253
|
+
report["warnings"].append(
|
|
254
|
+
t("ui.warn.low_share", options.lang, share=report["persona"]["share"])
|
|
255
|
+
)
|
|
256
|
+
|
|
257
|
+
_append_data_warnings(report, options)
|
|
258
|
+
return report
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
# ------------------------------------------------------------------ 公共
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
def _append_data_warnings(report: dict, options: Options) -> None:
|
|
265
|
+
lang = options.lang
|
|
266
|
+
block = report.get("overview")
|
|
267
|
+
if block:
|
|
268
|
+
if block["merges"] > block["commits"] * 0.3:
|
|
269
|
+
report["warnings"].append(t("ui.warn.merges", lang))
|
|
270
|
+
if block["bulk_commits"]:
|
|
271
|
+
lines = block["bulk_insertions"] + block["bulk_deletions"]
|
|
272
|
+
report["warnings"].append(
|
|
273
|
+
t("ui.warn.bulk", lang, n=block["bulk_commits"], lines=f"{lines:,}")
|
|
274
|
+
)
|
|
275
|
+
if block["commits"] < 20:
|
|
276
|
+
report["warnings"].append(t("ui.warn.few_commits", lang, n=20))
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def _build_meta(
|
|
280
|
+
root: Path, repo_info: dict, options: Options, commits: list
|
|
281
|
+
) -> dict[str, Any]:
|
|
282
|
+
ordered = sorted(commits, key=lambda c: c.authored_at) if commits else []
|
|
283
|
+
return {
|
|
284
|
+
"tool": TOOL_NAME,
|
|
285
|
+
"version": __version__,
|
|
286
|
+
"schema_version": SCHEMA_VERSION,
|
|
287
|
+
"generated_at": datetime.now(timezone.utc).astimezone().isoformat(timespec="seconds"),
|
|
288
|
+
"python": platform.python_version(),
|
|
289
|
+
"lang": options.lang,
|
|
290
|
+
"tone": options.tone,
|
|
291
|
+
"view": options.view,
|
|
292
|
+
"repo": repo_info,
|
|
293
|
+
"range": {
|
|
294
|
+
"since": options.since,
|
|
295
|
+
"until": options.until,
|
|
296
|
+
"first_commit": ordered[0].authored_at.isoformat() if ordered else None,
|
|
297
|
+
"last_commit": ordered[-1].authored_at.isoformat() if ordered else None,
|
|
298
|
+
},
|
|
299
|
+
"options": {
|
|
300
|
+
"all_branches": options.all_branches,
|
|
301
|
+
"follow_renames": options.follow_renames,
|
|
302
|
+
"anon": options.anon,
|
|
303
|
+
"no_ai": not (options.llm and options.llm.enabled),
|
|
304
|
+
"ignore": list(options.ignore),
|
|
305
|
+
},
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
def _empty_payload(options: Options) -> dict[str, Any]:
|
|
310
|
+
return {
|
|
311
|
+
"overview": overview.build([]),
|
|
312
|
+
"timeline": {"granularity": "week", "points": [], "totals": {"commits": 0}},
|
|
313
|
+
"rhythm": temporal.build_rhythm([], lang=options.lang),
|
|
314
|
+
"churn": {"files": [], "dirs": [], "empty": True, "counted_files": 0, "top_score": 0},
|
|
315
|
+
"languages": {"rows": [], "trend": {"keys": [], "series": []}, "empty": True},
|
|
316
|
+
"contributors": {"rows": [], "total_commits": 0, "empty": True},
|
|
317
|
+
"chapters": [],
|
|
318
|
+
"breakpoints": [],
|
|
319
|
+
"milestones": [],
|
|
320
|
+
"fun_facts": [],
|
|
321
|
+
"quote": "",
|
|
322
|
+
"persona": {},
|
|
323
|
+
"peers": [],
|
|
324
|
+
"warnings": [t("empty.no_commits", options.lang)],
|
|
325
|
+
}
|
chronicle/clean.py
ADDED
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
"""噪音清洗与提交分类。
|
|
2
|
+
|
|
3
|
+
两件事,都被后续统计依赖:
|
|
4
|
+
1. 剔掉构建产物、依赖目录、锁文件、二进制资源 —— 否则 node_modules 里一次
|
|
5
|
+
npm install 就能把「代码量波动」画成一座假山。
|
|
6
|
+
2. 给提交打类型标签(feat / fix / refactor …),churn 风险评分要用。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import fnmatch
|
|
12
|
+
import re
|
|
13
|
+
from collections.abc import Iterable, Sequence
|
|
14
|
+
|
|
15
|
+
from .models import Commit, FileChange
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"DEFAULT_IGNORE",
|
|
19
|
+
"BULK_LINE_THRESHOLD",
|
|
20
|
+
"filter_noise",
|
|
21
|
+
"is_noise",
|
|
22
|
+
"file_category",
|
|
23
|
+
"classify",
|
|
24
|
+
"KINDS",
|
|
25
|
+
"normalize_author_key",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
# 单文件单次变更超过这个行数,一律视为数据/生成物导入,不计入代码量。
|
|
29
|
+
# 20k 是经验值:手写代码极少在单个文件里一次落两万行(大模块总是拆文件的),
|
|
30
|
+
# 而语料、数据集、序列化产物动辄几十万行。
|
|
31
|
+
BULK_LINE_THRESHOLD = 20_000
|
|
32
|
+
|
|
33
|
+
KINDS = (
|
|
34
|
+
"merge",
|
|
35
|
+
"revert",
|
|
36
|
+
"feat",
|
|
37
|
+
"fix",
|
|
38
|
+
"refactor",
|
|
39
|
+
"perf",
|
|
40
|
+
"test",
|
|
41
|
+
"docs",
|
|
42
|
+
"build",
|
|
43
|
+
"style",
|
|
44
|
+
"chore",
|
|
45
|
+
"other",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
# 目录类噪音:任何一层路径命中即排除
|
|
49
|
+
_NOISE_DIRS = (
|
|
50
|
+
"node_modules",
|
|
51
|
+
"bower_components",
|
|
52
|
+
"vendor",
|
|
53
|
+
"third_party",
|
|
54
|
+
"datasets",
|
|
55
|
+
"dist",
|
|
56
|
+
"build",
|
|
57
|
+
"target",
|
|
58
|
+
"out",
|
|
59
|
+
"output",
|
|
60
|
+
"coverage",
|
|
61
|
+
"htmlcov",
|
|
62
|
+
"venv",
|
|
63
|
+
".venv",
|
|
64
|
+
"env",
|
|
65
|
+
"__pycache__",
|
|
66
|
+
".mypy_cache",
|
|
67
|
+
".pytest_cache",
|
|
68
|
+
".ruff_cache",
|
|
69
|
+
".next",
|
|
70
|
+
".nuxt",
|
|
71
|
+
".cache",
|
|
72
|
+
".idea",
|
|
73
|
+
".vscode",
|
|
74
|
+
".gradle",
|
|
75
|
+
".tox",
|
|
76
|
+
"site-packages",
|
|
77
|
+
".terraform",
|
|
78
|
+
"Pods",
|
|
79
|
+
"DerivedData",
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
# 文件名类噪音:命中 basename 即排除
|
|
83
|
+
_NOISE_FILES = (
|
|
84
|
+
"package-lock.json",
|
|
85
|
+
"pnpm-lock.yaml",
|
|
86
|
+
"yarn.lock",
|
|
87
|
+
"npm-shrinkwrap.json",
|
|
88
|
+
"poetry.lock",
|
|
89
|
+
"Pipfile.lock",
|
|
90
|
+
"Cargo.lock",
|
|
91
|
+
"go.sum",
|
|
92
|
+
"composer.lock",
|
|
93
|
+
"Gemfile.lock",
|
|
94
|
+
"bun.lockb",
|
|
95
|
+
"uv.lock",
|
|
96
|
+
"gradle.lockfile",
|
|
97
|
+
".DS_Store",
|
|
98
|
+
"Thumbs.db",
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
# 通配类噪音
|
|
102
|
+
_NOISE_GLOBS = (
|
|
103
|
+
"*.min.js",
|
|
104
|
+
"*.min.css",
|
|
105
|
+
"*.map",
|
|
106
|
+
"*.lock",
|
|
107
|
+
"*.log",
|
|
108
|
+
"*.snap",
|
|
109
|
+
"*.bak",
|
|
110
|
+
"*.orig",
|
|
111
|
+
"*.rej",
|
|
112
|
+
"*.swp",
|
|
113
|
+
"*.tmp",
|
|
114
|
+
"*~",
|
|
115
|
+
"*.pyc",
|
|
116
|
+
"*.pyo",
|
|
117
|
+
"*.class",
|
|
118
|
+
"*.jar",
|
|
119
|
+
"*.war",
|
|
120
|
+
"*.so",
|
|
121
|
+
"*.dylib",
|
|
122
|
+
"*.dll",
|
|
123
|
+
"*.exe",
|
|
124
|
+
"*.o",
|
|
125
|
+
"*.a",
|
|
126
|
+
"*.whl",
|
|
127
|
+
"*.egg-info/*",
|
|
128
|
+
"*.png",
|
|
129
|
+
"*.jpg",
|
|
130
|
+
"*.jpeg",
|
|
131
|
+
"*.gif",
|
|
132
|
+
"*.webp",
|
|
133
|
+
"*.ico",
|
|
134
|
+
"*.bmp",
|
|
135
|
+
"*.pdf",
|
|
136
|
+
"*.psd",
|
|
137
|
+
"*.zip",
|
|
138
|
+
"*.tar",
|
|
139
|
+
"*.gz",
|
|
140
|
+
"*.7z",
|
|
141
|
+
"*.mp4",
|
|
142
|
+
"*.mov",
|
|
143
|
+
"*.woff",
|
|
144
|
+
"*.woff2",
|
|
145
|
+
"*.ttf",
|
|
146
|
+
"*.eot",
|
|
147
|
+
# 数据集 / 模型权重 / 序列化文件:这些一提交就是几十万行,会把代码量统计冲垮
|
|
148
|
+
"*.jsonl",
|
|
149
|
+
"*.ndjson",
|
|
150
|
+
"*.parquet",
|
|
151
|
+
"*.feather",
|
|
152
|
+
"*.arrow",
|
|
153
|
+
"*.pkl",
|
|
154
|
+
"*.pickle",
|
|
155
|
+
"*.npy",
|
|
156
|
+
"*.npz",
|
|
157
|
+
"*.h5",
|
|
158
|
+
"*.hdf5",
|
|
159
|
+
"*.onnx",
|
|
160
|
+
"*.pt",
|
|
161
|
+
"*.pth",
|
|
162
|
+
"*.ckpt",
|
|
163
|
+
"*.safetensors",
|
|
164
|
+
"*.sqlite",
|
|
165
|
+
"*.sqlite3",
|
|
166
|
+
"*.db",
|
|
167
|
+
"*.mdb",
|
|
168
|
+
"*.avro",
|
|
169
|
+
"*.orc",
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
DEFAULT_IGNORE: tuple[str, ...] = _NOISE_DIRS + _NOISE_FILES + _NOISE_GLOBS
|
|
173
|
+
|
|
174
|
+
_DIR_SET = frozenset(_NOISE_DIRS)
|
|
175
|
+
_FILE_SET = frozenset(_NOISE_FILES)
|
|
176
|
+
_GLOB_SET = _NOISE_GLOBS
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def is_noise(path: str, extra: Sequence[str] | None = None) -> bool:
|
|
180
|
+
"""判断一个仓库内相对路径是否属于噪音。
|
|
181
|
+
|
|
182
|
+
extra 支持三种写法:`dir/`(目录段匹配)、`a/b.py`(完整路径 glob)、`*.xxx`(文件名 glob)。
|
|
183
|
+
"""
|
|
184
|
+
segments = path.split("/")
|
|
185
|
+
basename = segments[-1]
|
|
186
|
+
|
|
187
|
+
if any(seg in _DIR_SET for seg in segments[:-1]):
|
|
188
|
+
return True
|
|
189
|
+
if basename in _FILE_SET:
|
|
190
|
+
return True
|
|
191
|
+
if any(fnmatch.fnmatchcase(basename, pattern) for pattern in _GLOB_SET):
|
|
192
|
+
return True
|
|
193
|
+
|
|
194
|
+
for pattern in extra or ():
|
|
195
|
+
if pattern.endswith("/"):
|
|
196
|
+
if pattern.rstrip("/") in segments[:-1]:
|
|
197
|
+
return True
|
|
198
|
+
elif "/" in pattern:
|
|
199
|
+
if fnmatch.fnmatchcase(path, pattern):
|
|
200
|
+
return True
|
|
201
|
+
elif fnmatch.fnmatchcase(basename, pattern):
|
|
202
|
+
return True
|
|
203
|
+
return False
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def filter_noise(commits: Iterable[Commit], extra: Sequence[str] | None = None) -> list[Commit]:
|
|
207
|
+
"""剔除噪音文件、隔离批量导入、打上类型标签,返回新列表。
|
|
208
|
+
|
|
209
|
+
三道闸门,从确定到启发式:
|
|
210
|
+
1. 路径规则命中 -> 直接丢(node_modules、锁文件、二进制)。
|
|
211
|
+
2. 单文件单次变更超过 BULK_LINE_THRESHOLD -> 放进 dropped,不计入代码量。
|
|
212
|
+
这一条挡的是「把 1.1M 行的数据集提交进仓库」这类事故,路径规则挡不住。
|
|
213
|
+
3. 剩下的是真正的代码改动。
|
|
214
|
+
"""
|
|
215
|
+
kept: list[Commit] = []
|
|
216
|
+
for commit in commits:
|
|
217
|
+
normal: list[FileChange] = []
|
|
218
|
+
dropped: list[FileChange] = []
|
|
219
|
+
for change in commit.changes:
|
|
220
|
+
if is_noise(change.path, extra):
|
|
221
|
+
continue
|
|
222
|
+
if change.churn >= BULK_LINE_THRESHOLD:
|
|
223
|
+
dropped.append(change)
|
|
224
|
+
else:
|
|
225
|
+
normal.append(change)
|
|
226
|
+
commit.changes = normal
|
|
227
|
+
commit.dropped = dropped
|
|
228
|
+
commit.kind = classify(commit.subject, is_merge=commit.is_merge)
|
|
229
|
+
kept.append(commit)
|
|
230
|
+
return kept
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
_CODE_EXTS = frozenset(
|
|
234
|
+
{
|
|
235
|
+
".py", ".pyi", ".java", ".kt", ".scala", ".go", ".rs", ".c", ".h", ".cc",
|
|
236
|
+
".cpp", ".hpp", ".cs", ".rb", ".php", ".swift", ".m", ".js", ".jsx",
|
|
237
|
+
".mjs", ".ts", ".tsx", ".vue", ".svelte", ".sh", ".bash", ".ps1", ".sql",
|
|
238
|
+
".lua", ".pl", ".r", ".jl", ".dart", ".ex", ".exs", ".clj", ".hs", ".ml",
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
_DOC_EXTS = frozenset({".md", ".rst", ".txt", ".tex", ".adoc"})
|
|
242
|
+
_CONFIG_EXTS = frozenset({".json", ".yaml", ".yml", ".toml", ".ini", ".cfg", ".conf", ".env"})
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
def file_category(path: str) -> str:
|
|
246
|
+
"""code / doc / config / other。churn 评分要用它调权。"""
|
|
247
|
+
name = path.rsplit("/", 1)[-1].lower()
|
|
248
|
+
if name.startswith(".") or name in ("dockerfile", "makefile", "jenkinsfile", "procfile"):
|
|
249
|
+
return "config"
|
|
250
|
+
dot = name.rfind(".")
|
|
251
|
+
ext = name[dot:] if dot > 0 else ""
|
|
252
|
+
if ext in _CODE_EXTS:
|
|
253
|
+
return "code"
|
|
254
|
+
if ext in _DOC_EXTS:
|
|
255
|
+
return "doc"
|
|
256
|
+
if ext in _CONFIG_EXTS:
|
|
257
|
+
return "config"
|
|
258
|
+
return "other"
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
_CONVENTIONAL = re.compile(r"^(\w+)(?:\([^)]*\))?!?:\s", re.UNICODE)
|
|
262
|
+
_REVERT = re.compile(r"^(revert|rollback)\b|\brevert\b|回滚|撤销", re.IGNORECASE)
|
|
263
|
+
|
|
264
|
+
# 关键词按「优先级从高到低」排,先命中先返回
|
|
265
|
+
_KEYWORDS: tuple[tuple[str, tuple[str, ...]], ...] = (
|
|
266
|
+
("revert", ("revert", "rollback", "回滚", "撤销")),
|
|
267
|
+
("perf", ("perf", "performance", "optimize", "optimi", "speed up", "性能", "优化")),
|
|
268
|
+
("test", ("test", "spec", "coverage", "测试", "单测", "用例")),
|
|
269
|
+
("docs", ("doc", "docs", "readme", "comment", "typo", "文档", "注释", "说明")),
|
|
270
|
+
("build", ("build", "ci", "cd", "deploy", "release", "bump", "deps", "dependency",
|
|
271
|
+
"dependencies", "docker", "构建", "打包", "发布", "部署", "依赖")),
|
|
272
|
+
("style", ("style", "format", "lint", "prettier", "eslint", "black", "ruff",
|
|
273
|
+
"格式", "排版", "格式化")),
|
|
274
|
+
("refactor", ("refactor", "rename", "restructure", "extract", "move", "cleanup",
|
|
275
|
+
"clean up", "simplify", "重构", "重命名", "整理", "拆分", "抽取", "简化")),
|
|
276
|
+
("fix", ("fix", "bug", "hotfix", "patch", "issue", "error", "crash", "broken",
|
|
277
|
+
"修复", "修", "错误", "异常", "崩溃", "补丁")),
|
|
278
|
+
("feat", ("feat", "feature", "add", "implement", "support", "introduce", "create",
|
|
279
|
+
"new", "新增", "添加", "实现", "支持", "增加")),
|
|
280
|
+
("chore", ("chore", "misc", "tweak", "update", "update", "调整", "杂项")),
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def classify(subject: str, *, is_merge: bool = False) -> str:
|
|
285
|
+
"""把提交信息归类到 KINDS 之一。
|
|
286
|
+
|
|
287
|
+
先看 Conventional Commits 前缀,再退化为关键词匹配 —— 因为一大半真实仓库
|
|
288
|
+
(尤其国内团队)根本不写 `feat:` 前缀。
|
|
289
|
+
"""
|
|
290
|
+
if is_merge or subject.startswith("Merge "):
|
|
291
|
+
return "merge"
|
|
292
|
+
|
|
293
|
+
text = subject.strip()
|
|
294
|
+
if not text:
|
|
295
|
+
return "other"
|
|
296
|
+
|
|
297
|
+
match = _CONVENTIONAL.match(text)
|
|
298
|
+
if match:
|
|
299
|
+
head = match.group(1).lower()
|
|
300
|
+
if head in KINDS:
|
|
301
|
+
return head
|
|
302
|
+
if head in ("feature",):
|
|
303
|
+
return "feat"
|
|
304
|
+
if head in ("bugfix", "hotfix"):
|
|
305
|
+
return "fix"
|
|
306
|
+
if head in ("docs", "doc"):
|
|
307
|
+
return "docs"
|
|
308
|
+
if head in ("tests", "testing"):
|
|
309
|
+
return "test"
|
|
310
|
+
|
|
311
|
+
if _REVERT.search(text):
|
|
312
|
+
return "revert"
|
|
313
|
+
|
|
314
|
+
lowered = text.lower()
|
|
315
|
+
for kind, words in _KEYWORDS:
|
|
316
|
+
for word in words:
|
|
317
|
+
if word in lowered:
|
|
318
|
+
return kind
|
|
319
|
+
return "other"
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
def normalize_author_key(name: str, email: str) -> str:
|
|
323
|
+
"""作者身份归一。
|
|
324
|
+
|
|
325
|
+
以邮箱为准:同一个人换过机器名、写过中文名和英文名,靠 name 合并会炸。
|
|
326
|
+
邮箱缺失时退回 name。
|
|
327
|
+
"""
|
|
328
|
+
key = (email or "").strip().lower()
|
|
329
|
+
if key:
|
|
330
|
+
return key
|
|
331
|
+
return (name or "").strip().lower()
|