重要
由于领导想通过代码量衡量外包的工作量,需要每周提供当前周的代码增量。
规程中遇到以下问题:
- 使用什么工具,cloc
- 如何统计最近一周代码增量
- 解决中文文件名导致cloc报错
环境说明
Ubuntu 18.04
安装
版本较老,如果想使用最新版,参考此处安装
使用
- git 获取距今一周的 Commit ID
--reverse
: 将git log 倒序-1
: 只取一行日志--pretty=format:"%h"
: 日志只输出commit-id
1
2
3
4
5
6
7
8
9
| # 获取最近一周日志
date_str=$(date --date="1 weeks ago" +"%Y-%m-%d")
logs=$(git log --after="${date_str}" --pretty=format:"%h")
# --reverse 与 --pretty 一起使用未反转日志。以下命令会取最新的一次commit-id
# commit_id=$(git log --after="${date_str}" --reverse -1 --pretty=format:"%h")
# 获取最后一条commit-id 与 HEAD的差异
cloc --git --diff "${logs##*$'\n'}" HEAD
|
- 如果git仓库中,存在中文,则有可能遇到以下报错
1
2
3
| $ cloc --git --diff c9df9mf09df HEAD
fatal: pathspec '"road/Road/\346\265\213\350\257\225\345\234\271\2.sql"' did not match any files
Failed to create tarfile of files from git. at /usr/bin/cloc line 3811.
|
将中文名显示乱码问题解决,则能正常统计
1
| git config --global core.quotepath false
|
Reference
bash-date
cloc
git中文显示乱码问题