Git Query Language:用SQL語法探索Git儲存庫的命令行利器
GQL(Git Query Language)是一种基于SQL的工具,用于查询Git仓库信息。通过安装并运行命令行工具,用户可以执行各种查询操作,如统计提交次数、查找特定时间后的分支等。文章还提供了详细的使用说明和相关资源链接。 2025-5-11 07:0:58 Author: jdev.tw(查看原文) 阅读量:9 收藏

此處介紹的GQL,不是較多人熟知的GraphQL,而是Git Query Language的縮寫。
GQL讓我們可以用SQL語法來查詢Git儲存庫,從而快速獲知Git儲存庫的各項資訊,例如找出儲存庫裡所有作者提交次數的統計等。

1. 安裝與使用

GitHub下載最新版到本地PATH指向資料夾裡,再更名為gql即可直接執行。

2. 初次使用

在命令行將資料夾切換到Git工作目錄,先用-h查看命令行選項;若無選項則進入交互模式:

gql -h
GitQL is a SQL like query language to run on local repositories

Usage: gitql [OPTIONS]

Options:
-r,  --repos <REPOS>        Path for local repositories to run query on
-s,  --script <file>        Script file contains one or more query
-q,  --query <GitQL Query>  GitQL query to run on selected repositories
-p,  --pagination           Enable print result with pagination
-ps, --pagesize             Set pagination page size [default: 10]
-o,  --output               Set output format [render, json, csv]
-a,  --analysis             Print Query analysis
-e,  --editor               Enable GitQL Rich Line Editor
-h,  --help                 Print GitQL help
-v,  --version              Print GitQL Current Version

3. 使用重點

  1. show tables查詢可使用的資料表,有 commits、branches、refs、diff_changes、tags、diffs等6個資料表

    c:/git/obsidian-translations[master] # gql
    gitql > show tables
    ╭───────────────╮
    │ Tables        │
    ╞═══════════════╡
    │ commits       │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    │ branches      │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    │ refs          │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    │ diffs_changes │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    │ tags          │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┤
    │ diffs         │
    ╰───────────────╯
    gitql >
  2. 列出資料表欄位結構:describe

    gitql > describe commits
    ╭─────────────────┬──────────╮
    │ Field           ┆ Type     │
    ╞═════════════════╪══════════╡
    │ commit_id       ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ title           ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ message         ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ author_name     ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ author_email    ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ committer_name  ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ committer_email ┆ Text     │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ datetime        ┆ DateTime │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ parents_count   ┆ Int      │
    ├╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌┤
    │ repo            ┆ Text     │
    ╰─────────────────┴──────────╯
  3. 找出2025-01-01後的分支

    
    select * from branches where updated >= '2025-01-01 12:00:00' order by updated
    ```
  4. 找出`2025-03-01`後的提交資訊

    select title, datetime from commits where datetime >= '2025-03-01 00:00:00' order by datetime
  5. 提交人次數統計 (次數最多的前10人)

    SELECT author_name, COUNT(author_name) AS commit_num FROM commits GROUP BY author_name, author_email ORDER BY commit_num DESC LIMIT 10
  6. 找出特定提交人今年的提交資訊

    SELECT title,author_name,datetime FROM commits WHERE LOWER(author_name) = "emisjerry" and datetime>='2025-01-01 00:00:00'
  7. 找出儲存庫特定字串的提交人員與其提交次數

    select author_name,count(author_name) as Count from commits where message like '%zh_TW%' group by author_name

4. 💡 相關鏈接

💡 解說文章(繁體中文):
💡 Explanation article(English):
💡 解説記事(日本語):

✅GitHub: https://github.com/AmrDeveloper/GQL
✅官方文件: https://amrdeveloper.github.io/GQL/
✅另一個GitQL GitHub: https://github.com/filhodanuvem/gitql

5. 教學影片

https://youtu.be/dnJRixV12Vs

##

您可能也會有興趣的類似文章


文章来源: https://jdev.tw/blog/8805/gql-git-query-language-command-line
如有侵权请联系:admin#unsafe.sh