此處介紹的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. 使用重點
-
用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 >
-
列出資料表欄位結構: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 │
╰─────────────────┴──────────╯
-
找出2025-01-01
後的分支
select * from branches where updated >= '2025-01-01 12:00:00' order by updated
```
-
找出`2025-03-01`後的提交資訊
select title, datetime from commits where datetime >= '2025-03-01 00:00:00' order by datetime
-
提交人次數統計 (次數最多的前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
-
找出特定提交人今年的提交資訊
SELECT title,author_name,datetime FROM commits WHERE LOWER(author_name) = "emisjerry" and datetime>='2025-01-01 00:00:00'
-
找出儲存庫特定字串的提交人員與其提交次數
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
##
您可能也會有興趣的類似文章
- fselect: 擺脫複雜的命令選項,用SQL語法搜尋檔案 (0則留言, 2020/11/15)
- 打破Windows與UNIX的藩籬:nyagos-介於UNIX跟DOS之間的混血的CommandLine Shell (0則留言, 2024/04/14)
- Pake:快速打包網站服務,使用Tauri安裝檔只有3MB Win+Linux+macOS (0則留言, 2023/01/15)
- 快速建立Windows Defender Firewall 封鎖IP的Command Line命令 (0則留言, 2023/12/14)
- [Obs#13] 快速開啟筆記的方法:快速切換對話窗與obsidian:// URI 命令行 (0則留言, 2020/10/18)
- 在命令提示字元取得目前資料夾路徑的方法—使用set /p|初學者的命令行#10 (0則留言, 2021/04/22)
- 命令行:快速縮減圖片大小的方法 (0則留言, 2023/12/10)
- 撰寫git info工具以模擬svn info功能 (0則留言, 2014/09/01)
- 將Git分支名稱加到提示字元(Prompt)裡 (0則留言, 2014/09/02)
- [Git#9] Gitea 安裝與設定:輕量級程式碼託管解決方案 (0則留言, 2022/05/22)
- [分享] 濱野純訪談:為什麼 Google 接管開發 Git 2.0 了? (0則留言, 2018/05/25)
- Gitea網頁添加自訂選項以開啟說明文件 (0則留言, 2022/09/01)
- 建立測試環境以git rebase -i變更Commit歷史 (0則留言, 2014/10/08)
- Git https/SSL 連線失敗時的解決方法 (0則留言, 2022/09/06)
- [Windows] 用Gitea架設自用的Git Server (2則留言, 2017/07/21)