[Git]Git统计代码行数

2023-05-24,,

1 前言

今天,有这么一个需求:小组老大要求咱们【每个人】把【上个月】的【代码行数统计一下并上报。

成,统计就统计,但那么多项目,总不能让我用手去数吧?何况,时间久了,自己也不清楚自己改了哪些地方了。

So?当然是看看有木有直接用Git统计的方法了。(根据作者+时间区间)

百度大法一下,嘿,还真有!用完之后还真爽!避免未来还需要干这事儿,我觉得有必要小结一下,也方便以后复用。

2 流程分析

step0 switch target directory of project and open git-bash
step1 git chekout [targetBranchName]
step2 git pull
step3 [git command about statistic(as follows:↓)]

3 Git代码统计命令

git command about statistic

根据:当前分支+作者+时间段

git log --author=zengtai --since=2020-05-11 --until=2020-06-03 --format='%aN' | sort -u | while read name; do echo -en "$name\t"; git log --author="$name" --pretty=tformat: --numstat | grep "\(.html\|.java\|.xml\|.properties\)$" | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -; done

结果:当前分支下,新增代码行数、移除代码行数、总计代码行数(新增-移除)

(可见,不同分支下其统计结果不同)
根据:当前分支+作者

git log --author="zengtai" --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 | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }';

(可见,不同分支下其统计结果不同)

4 参考文献

Git代码行数统计命令 - 博客园

[Git]Git统计代码行数的相关教程结束。

《[Git]Git统计代码行数.doc》

下载本文的Word格式文档,以方便收藏与打印。