您可以使用 DuckDB 来分析 Git 日志,方法是利用 git log
命令的输出。
导出 Git 日志
我们首先选择一个在提交日志的任何部分(作者名称、消息等)中都不会出现的字符。自 v1.2.0 版本以来,DuckDB 的 CSV 读取器支持4 字节分隔符,这使得使用表情符号成为可能!🎉
尽管曾出现在《表情符号大电影》(IMDb 评分:3.4)中,我们可以假设漩涡鱼板表情符号 (🍥) 在大多数 Git 日志中并不常见。因此,让我们克隆 duckdb/duckdb
仓库并按如下方式导出其日志
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 日志加载到 DuckDB
启动 DuckDB 并将日志读取为 CSV 🍥SV
CREATE TABLE commits AS
FROM read_csv(
'git-log.csv',
delim = '🍥',
header = false,
column_names = ['timestamp', 'hash', 'author', 'message']
);
这将生成一个整洁的 DuckDB 表
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 │
└─────────────────────┴────────────┴──────────────────┴───────────────────────────────────────────────────────────────────────────────┘
分析日志
我们可以像分析 DuckDB 中的任何其他表一样分析该表。
常见主题
让我们从一个简单的问题开始:在提交消息中,哪个主题被提及最多: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 │
│ duckdb │ 1710 │
│ use │ 1442 │
│ mytherin │ 1410 │
│ fixes │ 1333 │
│ hawkfish │ 1147 │
│ feature │ 1139 │
│ function │ 1088 │
├──────────┴───────┤
│ 20 rows │
└──────────────────┘
正如所料,有许多 Git 术语(merge
、branch
、pull
等),紧随其后的是与开发相关的术语(fix
、test
/tests
、issue
、format
)。我们还看到了某些开发者的账户名(mytherin
、hawkfish
),这些很可能是因为合并拉取请求的提交消息(例如 “Merge pull request #13776 from Mytherin/expressiondepth”)而出现。最后,我们还看到一些与 DuckDB 相关的术语,如 duckdb
(令人震惊!)和 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 │ ███████▏ │
└───────┴─────────────┴──────────────────────────────────────────────────────────────────────────────────┘
我们看到这些年来稳步增长——特别是考虑到 DuckDB 的许多功能和客户端,这些最初是主仓库的一部分,现在都在单独的仓库中维护(例如 Java、R)。
祝您编程愉快!