git log统计
- 统计提交的作者
git log --pretty='%aN' | sort | uniq -c |awk '{print $2}'
- 统计代码量
git log --author="uer_name" --since ==2017-05-11 --until=2018-03-07 --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "added lines: %s removed lines : %s total lines: %s\n",add,subs,loc }' -
- 使用图形化工具
- 安装gitstatus
git clone git://github.com/hoxu/gitstats.git cd gitstats ./gitstats 你的项目的位置 生成统计的文件夹位置
生成的 html文件可web上打开index.html
- 统计每次的提交的commit ID,author email,日期
git log --pretty=format:"%H:%cd:%ce" --date=short
- 代码实现计算每个author在指定的时间的提交代码统计或每个commit 的代码修改量统计
#!/usr/bin/env bash
START_TIME=2012-01-01
END_TIME=2012-01-11
#count code by author
function count_author()
{
authors=$(git log --pretty='%aN' | sort | uniq -c |awk '{print $2}')
for user in $authors
do
echo "<$user> `git log --author=$user --since ==$START_TIME --until=$END_TIME --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "added lines: %s ;removed lines: %s ;total lines: %s\n",add,subs,loc }' -`"
done
echo "<TOTAL> `git log --since ==$START_TIME --until=$END_TIME --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "added lines: %s ;removed lines: %s ;total lines: %s\n",add,subs,loc }' -`"
}
#count code by per commit
function count_per_commit()
{
all_commits=$(git log --since ==$START_TIME --until=$END_TIME --pretty=format:"%H:%cd:%ce:" --date=short)
for commit in $all_commits
do
commit_id=$(echo "$commit"|awk -F ":" '{print $1}')
commit_date=$(echo "$commit"|awk -F ":" '{print $2}')
commit_email=$(echo "$commit"|awk -F ":" '{print $3}')
echo "<$commit_email>--<$commit_date>-- `git show $commit_id --pretty=tformat: --numstat | gawk '{ add += $1 ; subs += $2 ; loc += $1 - $2 } END { printf "added lines: %s ;removed lines: %s ;total lines: %s\n",add,subs,loc }' -`"
done
}
count_author
count_per_commit
计算项目中总代码量
- 使用cloc工具
这是操作系统自带的工具,可以通过镜像源安装- 安装cloc工具
yum install -y cloc
- 计算代码量
cloc --exclude_dir="env,docs," .
- 安装cloc工具
- 使用scc工具
这是一个go语言写的开源工具,个人觉得很好用,速度很快。- 下载源代码
代码库地址: https://github.com/boyter/scc
go install github.com/boyter/scc@master
- 计算代码量
- 下载源代码
$ scc redis
Language Files Lines Blanks Comments Code Complexity
C 296 180267 20367 31679 128221 32548
C Header 215 32362 3624 6968 21770 1636
TCL 143 28959 3130 1784 24045 2340
Shell 44 1658 222 326 1110 187
Autoconf 22 10871 1038 1326 8507 953
Lua 20 525 68 70 387 65
Markdown 16 2595 683 0 1912 0
Makefile 11 1363 262 125 976 59
Ruby 10 795 78 78 639 116
gitignore 10 162 16 0 146 0
YAML 6 711 46 8 657 0
HTML 5 9658 2928 12 6718 0
C++ 4 286 48 14 224 31
License 4 100 20 0 80 0
Plain Text 3 185 26 0 159 0
CMake 2 214 43 3 168 4
CSS 2 107 16 0 91 0
Python 2 219 12 6 201 34
Systemd 2 80 6 0 74 0
BASH 1 118 14 5 99 31
Batch 1 28 2 0 26 3
C++ Header 1 9 1 3 5 0
Extensible Styleshe… 1 10 0 0 10 0
Smarty Template 1 44 1 0 43 5
m4 1 562 116 53 393 0
Total 823 271888 32767 42460 196661 38012
Estimated Cost to Develop (organic) $6,918,301
Estimated Schedule Effort (organic) 28.682292 months
Estimated People Required (organic) 21.428982
Processed 9425137 bytes, 9.425 megabytes (SI)
计算代码变更量
使用git log获取的代码变更量,因为可能存在重复提交情况,所以计算实际的项目代码开发前后的变更量是不准确的,这里可以使用diff命令比对项目代码修改前后的代码改动来获取。
下面是一个使用scc和diff功能实现的计算项目代码修改前后的代码变更量及修改率
执行方法:sh code_count.sh old_code_dir new_code_dir
#!/usr/bin/env bash
CWD=$(cd `dirname $0`; pwd -P)
old_dir=${1:?}
if [ ! -e "$old_dir" ];then
echo "File or directory $old_dir does not exist!"
exit 1
fi
new_dir=${1:?}
if [ ! -e "$new_dir" ];then
echo "File or directory $new_dir does not exist!"
exit 1
fi
# 使用diff命令计算代码变更量并存入到result.diff中
output_file=${CWD}/result.diff
diff -ru "$old_dir" "$new_dir" > "$output_file"
if [ $? -ne 0 ];then
echo "execute diff failed, exit!"
exit 1
fi
diff_num=$(cat "$output_file" |wc -l)
if [ $diff_num -eq 0 ];then
echo "No different line found"
exit 1
fi
# 计算修改的代码变更
# 计算注释的行数
comment_num=$(cat "$output_file" |grep -E "^\+\s*/|^\+\s*[*] *" |wc -l)
# 计算空格的行数
space_num=$(cat "$output_file" |grep -E "^\+\s*$" |wc -l)
# 计算实际代码的行数
code_num=$(cat "$output_file" |grep -E "^\+\s*" | grep -v -E "^\+\s*/|^\+\s*[*]" |grep -v -E "^\+\s*$" |wc -l)
# 计算总行数
total_num=$(cat "$output_file" |grep -E "^\+\s*" |wc -l)
# 上面计算的知识修改的代码变更,对于新增的文件需要单独再计算
grep "Only in $new_dir" $output_file -rn > ./tmp_new_file
while IFS= read -r line
do
path=$(echo $line|awk -F':' '{print $2}' |awk '{print $NF}')
file=$(echo $line |awk -F':' '{print $NF}')
file_path=$(echo $path/$file |sed 's/ //g')
if [ ! -e "$file_path" ];then
echo "$file_path not exists"
continue
fi
new_comment=$(scc "$file_path" |grep "Total" |awk '{print $6}')
new_space=$(scc "$file_path" |grep "Total" |awk '{print $4}')
new_line=$(scc "$file_path" |grep "Total" |awk '{print $5}')
new_total=$(scc "$file_path" |grep "Total" |awk '{print $3}')
comment_num=$((comment_num + new_comment))
space_num=$((space_num + new_space))
code_num=$((code_num + new_line))
total_num=$((total_num + new_total))
done < ./tmp_new_file
all_comments=$(scc $new_dir|grep Total |awk '{print $5}')
all_blanks=$(scc $new_dir|grep Total |awk '{print $4}')
all_code=$(scc $new_dir|grep Total |awk '{print $6}')
all_line=$(scc $new_dir|grep Total |awk '{print $3}')
comment_percent=$(echo "scale=2; ($comment_num/$all_comments) * 100 " |bc)
blank_percent=$(echo "scale=2; ($space_num/$all_blanks) * 100 " |bc)
code_percent=$(echo "scale=2; ($code_num/$all_code) * 100 " |bc)
line_percent=$(echo "scale=2; ($total_num/$all_line) * 100 " |bc)
echo "****************************************代码变更统计********************************"
echo "*** 注释行数: $comment_num/$all_comments ($comment_percent%)"
echo "*** 空行行数: $space_num/$all_blanks ($blank_percent%)"
echo "*** 实际代码行数: $code_num/$all_code ($code_percent%)"
echo "*** 总代码变更: $total_num/$all_line ($line_percent%)"
echo “*****************************************************************************************”