跳到主要内容

分析 Git 仓库

你可以使用 Goose 基于 git log 命令输出分析 Git 日志。

导出 Git 日志

我们先选择一个不会出现在 commit 日志任何部分(作者名、消息等)中的分隔字符。 从 v1.2.0 起,Goose 的 CSV reader 支持 4 字节分隔符,因此可以用 emoji!🎉

虽然 Emoji Movie(IMDb 评分 3.4)里出现过它, 但我们可以合理假设 Fish Cake with Swirl emoji(🍥)在大多数 Git 日志里并不常见。 于是,我们克隆 goose 仓库并按如下方式导出日志:

git log --date=iso-strict --pretty=format:%ad🍥%h🍥%an🍥%s > git-log.csv

生成文件如下所示:

2025-02-25T18:12:54+01:00🍥d608a31e13🍥Mark🍥MAIN_BRANCH_VERSIONING: Adopt also for Python build and amalgamation (#16400)
2025-02-25T15:05:56+01:00🍥920b39ad96🍥Mark🍥Read support for Parquet Float16 (#16395)
2025-02-25T13:43:52+01:00🍥61f55734b9🍥Carlo Piovesan🍥MAIN_BRANCH_VERSIONING: Adopt also for Python build and amalgamation
2025-02-25T12:35:28+01:00🍥87eff7ebd3🍥Mark🍥Fix issue #16377 (#16391)
2025-02-25T10:33:49+01:00🍥35af26476e🍥Hannes Mühleisen🍥Read support for Parquet Float16

将 Git 日志加载到 Goose

启动 Goose,并将日志按 CSV 🍥SV 读取:

CREATE TABLE commits AS 
FROM read_csv(
'git-log.csv',
delim = '🍥',
header = false,
column_names = ['timestamp', 'hash', 'author', 'message']
);

这会得到一张不错的 Goose 表:

FROM commits
LIMIT 5;
┌─────────────────────┬────────────┬──────────────────┬───────────────────────────────────────────────────────────────────────────────┐
│ timestamp │ hash │ author │ message │
│ timestamp │ varchar │ varchar │ varchar │
├─────────────────────┼────────────┼──────────────────┼───────────────────────────────────────────────────────────────────────────────┤
│ 2025-02-25 17:12:54 │ d608a31e13 │ Mark │ MAIN_BRANCH_VERSIONING: Adopt also for Python build and amalgamation (#16400) │
│ 2025-02-25 14:05:56 │ 920b39ad96 │ Mark │ Read support for Parquet Float16 (#16395) │
│ 2025-02-25 12:43:52 │ 61f55734b9 │ Carlo Piovesan │ MAIN_BRANCH_VERSIONING: Adopt also for Python build and amalgamation │
│ 2025-02-25 11:35:28 │ 87eff7ebd3 │ Mark │ Fix issue #16377 (#16391) │
│ 2025-02-25 09:33:49 │ 35af26476e │ Hannes Mühleisen │ Read support for Parquet Float16 │
└─────────────────────┴────────────┴──────────────────┴───────────────────────────────────────────────────────────────────────────────┘

分析日志

你可以像分析 Goose 中其他表一样分析这张表。

常见主题

先从一个简单问题开始:在提交消息里,CI、CLI、Python 哪个主题被提及最多?

SELECT
message.lower().regexp_extract('\b(ci|cli|python)\b') AS topic,
count(*) AS num_commits
FROM commits
WHERE topic <> ''
GROUP BY ALL
ORDER BY num_commits DESC;
┌─────────┬─────────────┐
│ topic │ num_commits │
│ varchar │ int64 │
├─────────┼─────────────┤
│ ci │ 828 │
│ python │ 666 │
│ cli │ 49 │
└─────────┴─────────────┘

在这三个主题中,与持续集成相关的提交明显最多!

我们还可以做更探索性的分析,查看提交消息中的所有词。 首先对消息进行分词:

CREATE TABLE words AS
SELECT unnest(
message
.lower()
.regexp_replace('\W', ' ')
.trim(' ')
.string_split_regex('\W')
) AS word
FROM commits;

然后,使用预定义列表去除停用词:

CREATE TABLE stopwords AS
SELECT unnest(['a', 'about', 'above', 'after', 'again', 'against', 'all', 'am', 'an', 'and', 'any', 'are', 'as', 'at', 'be', 'because', 'been', 'before', 'being', 'below', 'between', 'both', 'but', 'by', 'can', 'did', 'do', 'does', 'doing', 'don', 'down', 'during', 'each', 'few', 'for', 'from', 'further', 'had', 'has', 'have', 'having', 'he', 'her', 'here', 'hers', 'herself', 'him', 'himself', 'his', 'how', 'i', 'if', 'in', 'into', 'is', 'it', 'its', 'itself', 'just', 'me', 'more', 'most', 'my', 'myself', 'no', 'nor', 'not', 'now', 'of', 'off', 'on', 'once', 'only', 'or', 'other', 'our', 'ours', 'ourselves', 'out', 'over', 'own', 's', 'same', 'she', 'should', 'so', 'some', 'such', 't', 'than', 'that', 'the', 'their', 'theirs', 'them', 'themselves', 'then', 'there', 'these', 'they', 'this', 'those', 'through', 'to', 'too', 'under', 'until', 'up', 'very', 'was', 'we', 'were', 'what', 'when', 'where', 'which', 'while', 'who', 'whom', 'why', 'will', 'with', 'you', 'your', 'yours', 'yourself', 'yourselves']) AS word;

CREATE OR REPLACE TABLE words AS
FROM words
NATURAL ANTI JOIN stopwords
WHERE word != '';

这里使用 NATURAL ANTI JOIN 子句,可优雅地过滤掉出现在 stopwords 表中的值。

最后,选出最常见的前 20 个词。

SELECT word, count(*) AS count FROM words
GROUP BY ALL
ORDER BY count DESC
LIMIT 20;
┌──────────┬───────┐
│ w │ count │
│ varchar │ int64 │
├──────────┼───────┤
│ merge │ 12550 │
│ fix │ 6402 │
│ branch │ 6005 │
│ pull │ 5950 │
│ request │ 5945 │
│ add │ 5687 │
│ test │ 3801 │
│ master │ 3289 │
│ tests │ 2339 │
│ issue │ 1971 │
│ main │ 1935 │
│ remove │ 1884 │
│ format │ 1819 │
│ goose │ 1710 │
│ use │ 1442 │
│ mytherin │ 1410 │
│ fixes │ 1333 │
│ hawkfish │ 1147 │
│ feature │ 1139 │
│ function │ 1088 │
├──────────┴───────┤
│ 20 rows │
└──────────────────┘

不出所料,列表中有很多 Git 术语(mergebranchpull 等),其次是开发相关术语(fixtest/testsissueformat)。 我们还看到了部分开发者账号名(mytherinhawkfish),这通常来自合并 PR 的提交消息(例如 ”Merge pull request #13776 from Mytherin/expressiondepth”)。 最后也能看到一些 Goose 相关词汇,比如 goose(很震惊!)和 function

可视化提交数量

我们来可视化每年的提交数量:

SELECT
year(timestamp) AS year,
count(*) AS num_commits,
num_commits.bar(0, 20_000) AS num_commits_viz
FROM commits
GROUP BY ALL
ORDER BY ALL;
┌───────┬─────────────┬──────────────────────────────────────────────────────────────────────────────────┐
│ year │ num_commits │ num_commits_viz │
│ int64 │ int64 │ varchar │
├───────┼─────────────┼──────────────────────────────────────────────────────────────────────────────────┤
│ 2018 │ 870 │ ███▍ │
│ 2019 │ 1621 │ ██████▍ │
│ 2020 │ 3484 │ █████████████▉ │
│ 2021 │ 6488 │ █████████████████████████▉ │
│ 2022 │ 9817 │ ███████████████████████████████████████▎ │
│ 2023 │ 14585 │ ██████████████████████████████████████████████████████████▎ │
│ 2024 │ 15949 │ ███████████████████████████████████████████████████████████████▊ │
│ 2025 │ 1788 │ ███████▏ │
└───────┴─────────────┴──────────────────────────────────────────────────────────────────────────────────┘

可以看到这些年呈稳步增长趋势—— 尤其考虑到 Goose 的许多功能与客户端原本在主仓库中,如今已拆分到独立仓库维护 (例如 JavaR)。

祝你玩得开心!