gitbase 集成sqler 进行git 代码分析
gitbase 是一个方便的git sql 查询引擎,sqler 是一个很不错的sql 转rest api工具,以下是一个简单的集成测试
项目使用docker-compose 运行
环境准备
- docker-compose 文件
version: "3"
services:
sqler:
image: dalongrong/sqler:2.0
volumes:
- "./config/config-example.hcl:/app/config.example.hcl"
environment:
- "DSN=root@tcp(gitbase:3306)/gitbase"
ports:
- "3678:3678"
- "8025:8025"
gitbase:
container_name: gitbase
hostname: gitbase
image: srcd/gitbase:v0.20.0-rc1
volumes:
- "./git-demos:/opt/repos"
ports:
- "3306:3306"
- sqler hcl 配置
这个是一个简单的统计git 代码行数的sql
filecounts {
exec = <<SQL
SELECT
LANGUAGE(file_path, blob_content) as lang,
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.code')) as code,
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.comment')) as comments,
SUM(JSON_EXTRACT(LOC(file_path, blob_content), '$.blank')) as blanks,
COUNT(1) as files
FROM commit_files
NATURAL JOIN refs
NATURAL JOIN blobs
WHERE ref_name='HEAD'
GROUP BY lang;
SQL
}
运行&&测试
- 准备git 代码
可以通过clone 方式 - 运行
docker-compose up -d
- 访问rest api
curl -i http://localhost:8025/filecounts
效果
HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=UTF-8
Vary: Origin
Vary: Accept-Encoding
Date: Sun, 12 May 2019 07:34:39 GMT
Content-Length: 958
{"data":[{"blanks":0,"code":0,"comments":0,"files":7,"lang":"Ignore List"},{"blanks":78,"code":138,"comments":0,"files":5,"lang":"Markdown"},{"blanks":0,"code":0,"comments":0,"files":6,"lang":"Maven POM"},{"blanks":0,"code":0,"comments":0,"files":5,"lang":"Dockerfile"},{"blanks":152,"code":409,"comments":12,"files":20,"lang":"YAML"},{"blanks":4113,"code":15009,"comments":3815,"files":259,"lang":"Java"},{"blanks":0,"code":0,"comments":0,"files":2,"lang":"Shell"},{"blanks":0,"code":26,"comments":0,"files":1,"lang":"JSON"},{"blanks":0,"code":0,"comments":0,"files":1,"lang":"Nginx"},{"blanks":0,"code":0,"comments":0,"files":1,"lang":""},{"blanks":1,"code":91,"comments":1,"files":3,"lang":"XML"},{"blanks":211,"code":2067,"comments":0,"files":31,"lang":"HTML"},{"blanks":0,"code":0,"comments":0,"files":6,"lang":""},{"blanks":0,"code":0,"comments":0,"files":1,"lang":"Text"},{"blanks":53,"code":31,"comments":260,"files":1,"lang":"TOML"}],"success":true}
说明
gitbase + sqler 我们可以方便的git 代码的分析,同时也很灵活,同时gitbase 官方也为我们提供了好多demo,可以方便使用,实际上官方的
web ui 也是一个不错的选择。
参考资料
https://github.com/rongfengliang/sqler-gitbase-docker-compose
https://github.com/alash3al/sqler
https://github.com/rongfengliang/sqler-docker-compose
https://github.com/src-d/gitbase
https://github.com/src-d/gitbase-web
https://docs.sourced.tech/gitbase/