计算git存储库中每位作者的代码行数

时间:2017-10-08 10:42:11

标签: git git-branch author lines-of-code cloc

所以我和一些其他程序员在一个团队中,需要在我们的git存储库中获得每位作者的代码行数。这不仅仅意味着作者修改的行,因为这将包括空白和注释行。理想情况下,我可以创建一个仅包含特定作者的提交的新分支(我自己为cloc),然后使用git log --author="BtheDestroyer" --format=%H > mycommits git checkout --orphan mycommits tac mycommits| while read sha; do git cherry-pick --no-commit ${sha}; done 分别获取注释行数和代码行数。我尝试过使用

filepath: unmerged (commit-id-1)
filepath: unmerged (commit-id-2)
error: your index file is unmerged.
fatal: cherry-pick failed
然而,在最后一行中,我得到了大量的以下错误:

{{1}}

我也不确定这是否会在此过程中通过其他提交快速转发。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

回答我自己的问题:

我最终使用git blame和Bourne shell脚本遍历源文件夹中的不同文件,使用grepcut将它们转换回代码,将输出组织为临时文件,然后在其上运行cloc

这是我的shell脚本,适用于任何想要做类似事情的人(我在./Blame/中有这样做,所以请适当更改SOURCE!):

#!/bin/bash
#Name of user to check
#  If you have multiple usernames, separate them with a space
#  The full name is not required, just enough to not be ambiguous
USERS="YOUR USERNAMES HERE"
#Directories
SOURCE=../source

for USER in $USERS
do
    #clear blame files
    echo "" > $USER-Blame.h
    echo "" > $USER-Blame.cpp
    echo "" > $USER-Blame.sh
    echo "Finding blame for $USER..."
    #C++ files
    echo "  Finding blame for C++ files..."
    for f in $SOURCE/*.cpp
    do
        git blame "$f" | grep "$USER" | cut -c 70- >> "$USER-Blame.cpp"
    done
    #Header files
    echo "  Finding blame for Header files..."
    for f in $SOURCE/*.h
    do
        git blame "$f" | grep "$USER" | cut -c 70- >> "$USER-Blame.h"
    done
    #Shell script files
    echo "  Finding blame for shell script files..."
    for f in ./GetUSERBlame.sh
    do
        git blame "$f" | grep "$USER" | cut -c 70- >> "$USER-Blame.sh"
    done
done

for USER in $USERS
do
#cloc
echo "Blame for all users found! Cloc-ing $USER..."
cloc $USER-Blame.* --quiet
#this line is for cleaning up the temporary files
#if you want to save them for future reference, comment this out.
rm $USER-Blame.* -f
done