【问题标题】:Counting lines of code per author in a git repository计算 git 存储库中每位作者的代码行数
【发布时间】:2018-03-19 16:51:30
【问题描述】:

所以我和其他一些程序员在一个团队中,需要在我们的 git 存储库中获取每个作者的代码行数。这不仅仅意味着作者修改的行,因为这将包括空白行和注释行。理想情况下,我可以创建一个仅包含特定作者提交的新分支(我自己为--author="BtheDestroyer"),然后使用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

我也不确定这是否最终会通过过程中的其他提交进行快进。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    这是我的单行:

    function gitfilecontributors() { local perfile="false" ; if [[ $1 = "-f" ]]; then perfile="true" ; shift ; fi ; if [[ $# -eq 0 ]]; then echo "no files given!" >&2 ; return 1 ; else local f ; { for f in "$@"; do echo "$f" ; git blame --show-email "$f" | sed -nE 's/^[^ ]* *.<([^>]*)>.*$/: \1/p' | sort | uniq -c | sort -r -nk1 ; done } | if [[ "$perfile" = "true" ]]; then tee /tmp/gitblamestats.txt ; else tee /tmp/gitblamestats.txt >/dev/null ; fi ; echo ; echo "total:" ; awk -v FS=' *: *' '/^ *[0-9]/{sums[$2] += $1} END { for (i in sums) printf("%7s : %s\n", sums[i], i)}' /tmp/gitblamestats.txt | sort -r -nk1 ; fi ; }
    

    或换行:

    gitfilecontributors ()
    {
        local perfile="false";
        if [[ $1 = "-f" ]]; then
            perfile="true";
            shift;
        fi;
        if [[ $# -eq 0 ]]; then
            echo "no files given!" 1>&2;
            return 1;
        else
            local f;
            {
                for f in "$@";
                do
                    echo "$f";
                    git blame --show-email "$f" | sed -nE 's/^[^ ]* *.<([^>]*)>.*$/: \1/p' | sort | uniq -c | sort -r -nk1;
                done
            } | if [[ "$perfile" = "true" ]]; then
                tee /tmp/gitblamestats.txt;
            else
                tee /tmp/gitblamestats.txt > /dev/null;
            fi;
            echo;
            echo "total:";
            awk -v FS=' *: *' '/^ *[0-9]/{sums[$2] += $1} END { for (i in sums) printf("%7s : %s\n", sums[i], i)}' /tmp/gitblamestats.txt | sort -r -nk1;
        fi
    }
    

    使用您选择的四个文件夹。

    选项 -f 显示每个文件,否则只显示总数:

    $ gitfilecontributors    $(fd --type f '.*' source)
    
    total:
        139 : somebody@somewhere.de
         29 : else.user@somewhere.de
          9 : just.another@somewhere.de
    
    gitfilecontributors -f $(fd --type f '.*' source)
    
    source/040_InitialSetup.md
         80 : somebody@somewhere.de
         29 : else.user@somewhere.de
          6 : just.another@somewhere.de
    README.md
         59 : somebody@somewhere.de
          5 : whosthat@somewhere.de
          3 : just.another@somewhere.de
    
    total:
        139 : somebody@somewhere.de
         29 : else.user@somewhere.de
          9 : just.another@somewhere.de
          5 : whosthat@somewhere.de
    

    【讨论】:

      【解决方案2】:

      回答我自己的问题:

      我最终使用 git blame 和 Bourne shell 脚本循环访问源文件夹中的不同文件,使用 grepcut 将它们转换回代码,将输出组织到临时文件中,然后运行 ​​@ 987654324@那个。

      这是我的 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
      

      【讨论】:

        猜你喜欢
        • 2019-05-06
        • 2011-06-16
        • 2010-11-18
        • 2012-08-12
        • 2014-11-09
        • 1970-01-01
        • 1970-01-01
        • 2014-11-20
        • 2017-07-02
        相关资源
        最近更新 更多