Git:如何根据添加/更改的代码行来估计一个人对我的项目的贡献?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4592866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:44:24  来源:igfitidea点击:

Git: How to estimate a contribution of a person to my project in terms of added/changed lines of code?

git

提问by Lu4

I have a GIT repository and I want to calculate how many lines of code were added/changed by one person or a group of persons during some period of time. Is it possible to calculate with git?

我有一个 GIT 存储库,我想计算在一段时间内一个人或一组人添加/更改了多少行代码。可以用git计算吗?

回答by abyx

You can use git logand some shell-fu:

您可以使用git log一些 shell-fu:

git log --shortstat --author "Aviv Ben-Yosef" --since "2 weeks ago" --until "1 week ago" \
    | grep "files\? changed" \
    | awk '{files+=; inserted+=; deleted+=} END \
           {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'

Explanation: git log --shortstatdisplays a short statistic about each commit, which, among other things, shows the number of changed files, inserted and deleted lines. We can then filter it for a specific committer (--author "Your Name") and a time range (--since "2 weeks ago" --until "1 week ago").

说明:git log --shortstat显示关于每次提交的简短统计信息,其中包括显示更改文件、插入和删除行的数量。然后,我们可以针对特定的提交者 ( --author "Your Name") 和时间范围 ( --since "2 weeks ago" --until "1 week ago")对其进行过滤。

Now, in order to actually sum up the stats instead of seeing the entry per commit, we do some shell scripting to do it. First, we use grepto filter only the lines with the diffs. These lines look like this:

现在,为了实际总结统计数据而不是查看每次提交的条目,我们编写了一些 shell 脚本来完成它。首先,我们使用grep仅过滤具有差异的行。这些行看起来像这样:

 8 files changed, 169 insertions(+), 81 deletions(-)

or this:

或这个:

 1 file changed, 4 insertions(+), 4 deletions(-)

We then sum these using awk: for each line we add the files changed (1st word), inserted lines (4th word) and deleted lines (6th word) and then print them after summing it all up.

然后我们使用awk:对每一行添加更改的文件(第一个单词)、插入的行(第 4 个单词)和删除的行(第 6 个单词),然后在汇总后打印它们。

Edit: forward slashes were added in the top snippet so it can be copy and pasted into a command line.

编辑:在顶部代码段中添加了正斜杠,因此可以将其复制并粘贴到命令行中。

回答by Dogbert

You can generate stats using Gitstats. It has an 'Authors' section which includes number of lines add/removed by the top 20 authors (top 20 by commit count).

您可以使用Gitstats生成统计信息。它有一个“作者”部分,其中包括前 20 位作者(按提交计数排名前 20 位)添加/删除的行数。

Edit: There's also Git: Blame Statistics

编辑:还有Git: Blame Statistics

回答by 1P0

For particular dates, you can use --since "2012-08-27" --until "2012-09-01"

对于特定日期,您可以使用 --since "2012-08-27" --until "2012-09-01"

Like

喜欢

git log --shortstat --author "Fabian" --since "2012-08-27" --until "2012-09-01" | grep "files changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "files changed", files, "lines inserted:", inserted, "lines deleted:", deleted}'

git log --shortstat --author "Fabian" --since "2012-08-27" --until "2012-09-01" | grep "文件已更改" | awk '{文件+=$1; 插入+=$4;删除+=$6} END {打印“文件已更改”,文件,“插入的行:”,插入的,“删除的行:”,已删除}'

Check this explanation

检查这个解释

http://gitref.org/inspect/

http://gitref.org/inspect/

回答by GreenRaccoon23

Run this command:

运行此命令:

git log --pretty=format:'' --numstat --author 'Lu4' | awk 'NF' | awk '{insertions+=; deletions+=} END {print NR, "files changed,", insertions, "insertions(+),", deletions, "deletions(+)"}';

This command is very close to the clever one in abyx's answer, but it also handles the edge case found by Wallace Sidhrée. Sometimes, a commit involves deletions only (i.e., no insertions). The command in abyx's answerincorrectly reads those deletions as insertions. The command here reads them correctly because it uses --numstatinstead of --shortstat. Unlike --shortstat, --numstatincludes both the insertions and deletions for those commits.

此命令与abyx 的答案中的聪明命令非常接近,但它也处理了Wallace Sidhrée发现的边缘情况。有时,提交仅涉及删除(即,没有插入)。abyx 的答案中的命令错误地将这些删除读取为插入。此处的命令可以正确读取它们,因为它使用--numstat代替--shortstat. 与 不同--shortstat--numstat包括这些提交的插入和删除。

Note that both commands include binary files in the file count but exclude the number of lines inserted and deleted inside those binaries.

请注意,这两个命令都在文件计数中包含二进制文件,但不包括在这些二进制文件中插入和删除的行数。



Here is another useful trick. Create a file called gitstatswith this content:

这是另一个有用的技巧。创建一个gitstats使用此内容调用的文件:

#!/usr/bin/env bash

git log --pretty=format:'' --numstat "$@" | awk 'NF' | awk '{insertions+=; deletions+=} END {print NR, "files changed,", insertions, "insertions(+),", deletions, "deletions(+)"}';

Then you can run that command with any extra options to git logyou want. Here are some examples:

然后,您可以根据需要使用任何额外选项运行该命令git log。这里有些例子:

./gitstats;
./gitstats --since '1 month ago';
./gitstats --since '1 month ago' --until '1 day ago';
./gitstats --author 'Lu4' --since '1 month ago' --until '1 day ago';

(The file can be named something other than gitstats, of course.)

(当然,该文件可以命名为 以外的其他名称gitstats。)

回答by Walterwhites

you can do that:

1) Run:

你可以这样做:

1)运行:

nano contribution.sh

2) fill :

2)填写:

if [ $# -eq 1 ]
then
        git log --author= --pretty=tformat: --numstat | awk '{ add += ; subs += ; loc +=  -  } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' - > logs.txt
        cat logs.txt
else
        echo "ERROR: you should pass username at argument"
fi

3) Run :

3)运行:

chmod +x contribution.sh

4) Now you can see your contribution with:

4)现在你可以看到你的贡献:

./contribution.sh your_git_username

回答by victor.cheval

I wrote some cli tool for this (https://www.npmjs.com/package/whodid)

我为此编写了一些 cli 工具(https://www.npmjs.com/package/whodid

$ npm install -g whodid
$ cd your-proj-dir

and then

进而

$ whodid --include-merge=false --since=1.week

回答by ingyhere

You can try Atlassian's Fisheye/Cruciblewhich integrates with Git (as well as other code repos). Then everyone's contributions -- including their LOC -- are displayed publicly in an easily readable Web app. For small groups, it's pretty cheap, too.

您可以尝试与 Git(以及其他代码存储库)集成的Atlassian 的 Fisheye/Crucible。然后,每个人的贡献——包括他们的 LOC——都会在一个易于阅读的 Web 应用程序中公开显示。对于小团体来说,它也很便宜。

Open source the information and let it speak for itself.

开源信息,让它不言自明。

回答by grepit

I'm not sure the line of code would be a good metric but if you are looking for just total commits and compare it against other engineers then you could use this: no plugin or addon is needed...just pure shell script, it was tested in zshell

我不确定代码行是否是一个很好的指标,但是如果您只是在寻找总提交并将其与其他工程师进行比较,那么您可以使用它:不需要插件或插件......只是纯粹的 shell 脚本,它在 zshell 中测试

Note: you must run this from the repo folder

注意:您必须从 repo 文件夹运行它

#!/bin/env zsh
team_total=$(git shortlog -s -n |sed 's/\t/,/g'|cut -f1 -d, |bc -l|awk '{sum+=}END{print sum}');
tmp_counter='/tmp/counter.txt';
tmp_user='/tmp/users.txt';
tmp_percentage='/tmp/counters_users'
# if you are running this again it make the file empty or you can rm it 
rm $tmp_percentage $tmp_user $tmp_counter
git shortlog -s -n |sed 's/\t/,/g'|cut -f2 -d, >>$tmp_user;
git shortlog -s -n |sed 's/\t/,/g'|cut -f1 -d, >>$tmp_counter;

cat $tmp_counter | while read LINE; do
    printf '%.2f %%  \n' $(echo \($LINE/$team_total\)\*100 |bc -l ) >>$tmp_percentage
done
echo 'commits %      | contributor     | # of commits';paste  $tmp_percentage $tmp_user $tmp_counter

here is the sample report:

这是示例报告:

enter image description here

在此处输入图片说明