Git 저장소 분석하기
Git 저장소 분석하기
DuckDB를 이용하면 Git 로그를 데이터처럼 다뤄서 분석할 수 있어요. git log 명령의 출력을 CSV처럼 읽어 들인 뒤, 커밋 메시지에서 자주 나오는 주제나 단어, 연도별 커밋 수를 SQL로 뽑아내는 것이 핵심이에요.
출처: 공식문서
Git 로그 내보내기
먼저 커밋 로그(작성자 이름, 메시지 등) 어디에도 나타나지 않는 문자를 골라야 해요. DuckDB v1.2.0부터 CSV 리더가 4바이트 구분자를 지원해서 이모지도 구분자로 쓸 수 있어요! 🎉
에모지 무비에 나왔음에도(IMDb 평점 3.4) 대부분의 Git 로그에서 Fish Cake with Swirl 이모지(🍥)는 흔치 않다고 가정할 수 있어요. 그럼 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 │
└─────────┴─────────────┘
세 주제 중에서 지속적 통합(continuous integration) 관련 커밋이 로그를 압도해요!
좀 더 탐색적으로, 커밋 메시지의 모든 단어를 살펴볼 수도 있어요. 먼저 메시지를 토큰화해요:
CREATE TABLE words AS
SELECT unnest(
message
.lower()
.regexp_replace('\W', ' ')
.trim(' ')
.string_split_regex('\W')
) AS word
FROM commits;
그다음 미리 정의된 목록으로 불용어(stopwords)를 제거해요:
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)도 보이는데, 이는 아마 PR 머지 커밋 메시지(예: "Merge pull request #13776 from Mytherin/expressiondepth") 때문일 거예요. 마지막으로 duckdb(놀랍죠?)와 function 같은 DuckDB 관련 용어도 보여요.
커밋 수 시각화
연도별 커밋 수를 시각화해 볼게요:
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)로 관리된다는 점을 고려하면 더 그렇죠.
해피 해킹!