zl程序教程

您现在的位置是:首页 >  其他

当前栏目

git 代码行数统计

2023-06-13 09:11:03 时间

命令需要在bash下运行,windows系统可使用git客户端附带的“git bash here”右键菜单进入bash命令行

  1. 统计所有代码行数
git log --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'

命令详解: 使用指定格式输出日志

git log --pretty=tformat: --numstat 

输出形式为

添加行数 删除行数 文件路径

读取每一行日志并分成数个字段进行处理,并在处理完成后执行END指定的命令输出汇总信息

awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
  1. 统计一定时间内产生的代码行数
git log --since=2019-01-01 --until==2019-12-31 --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'
  1. 统计指定开发者一段时间内产生的代码行数
git log --since =2019-01-01 --until==2019-12-31 --author="psr" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }'