Git:获取每天更改的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8154516/
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
Git: get number of changed lines per day
提问by ernesto
I'd like to make a diagram of added/removed/changed lines in a git repository per day and/or week. I do notwant to count the number of commits.
我想每天和/或每周在 git 存储库中制作一个添加/删除/更改行的图表。我不是要算提交的数目。
Is there a tool that can produce such charts (gitstats does not)? Or, with which git command I can produce an output which i could parse easily?
有没有工具可以生成这样的图表(gitstats 没有)?或者,我可以使用哪个 git 命令生成可以轻松解析的输出?
Thank you!
谢谢!
回答by Sven Marnach
Maybe something like this:
也许是这样的:
$ git diff --shortstat "@{1 month ago}"
7 files changed, 29 insertions(+), 6 deletions(-)
(As you can see, I tried this on a pretty stale repository.)
(如您所见,我在一个非常陈旧的存储库中尝试了此操作。)
Note that this will compare the current working directory to what the current branch pointed to one month ago on your local machine.
请注意,这会将当前工作目录与本地机器上一个月前当前分支指向的目录进行比较。
Edit: To get stats for all commits on the branch master
in a certain date range, you can use
编辑:要获取master
特定日期范围内分支上所有提交的统计信息,您可以使用
git log --after=2011-01-01 --before=2011-01-31 --format=format: --shortstat master
回答by JuanPablo
回答by Markus Duft
I just needed the accumulated diffstat for a period of time in the repository without relying on the reflog (as the repo was freshly cloned). Thus i came up with this:
我只需要在存储库中积累一段时间的 diffstat 而不依赖 reflog(因为 repo 是新克隆的)。因此我想出了这个:
( eval $(git log --pretty="%H" --since="2 day" | while read line; do if [[ -z ${first} ]]; then first=${line}; echo "export first=${first}"; fi; echo "export last=${line}"; done; ) ; git diff --stat ${first} ${last}; )
( eval $(git log --pretty="%H" --since="2 day" | while read line; do if [[ -z ${first} ]]; then first=${line}; echo "export first=${first}"; fi; echo "export last=${line}"; done; ) ; git diff --stat ${first} ${last}; )
you can easily modify the "2 day" to get something else :)
您可以轻松修改“2 天”以获得其他内容:)
回答by gnmerritt
Here's a bash script that calculates the total number of changed lines per week:
这是一个 bash 脚本,用于计算每周更改的行总数:
#!/bin/bash
ds() {
date --date=" weeks ago" +%Y-%m-%d
}
BRANCH=master # your branch here
echo "Week,Lines Changed"
for week in {80..1}
do
# git log outputs lines like:
# 11 10 path/to/your/file.java
# => add first two columns with awk
lines=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat $BRANCH | awk '{s+=; s+=} END {print s}')
echo "$(ds $week),$lines"
done
Output is in CSV format and looks like:
输出为 CSV 格式,如下所示:
Week,Lines Changed
2016-12-21,5989
2016-12-28,17179
回答by yuan3y
Adapting from gnmerritt's answer, this script will generate number of lines of add and delete for each calendar day.
根据gnmerritt 的回答改编,此脚本将为每个日历日生成添加和删除行数。
#!/bin/bash
ds() {
date --date=" days ago" +%Y-%m-%d
}
BRANCH=master # your branch here
echo "Date,LinesAdded,LinesDeleted"
for day in $(seq 1 10)
do
lines=$(git --no-pager log --after=$(ds $day) --before=$(ds $(($day - 1))) --format=format: --numstat $BRANCH | awk '/([0-9]+).*([0-9]+).*/{s+=; t+=} END {printf "+"; printf s;printf ",-"; printf t;}')
if [[ "$lines" != "+,-" ]]; then
echo "$(ds $day),$lines"
else
echo "$(ds $day),0,0"
fi
done
回答by mishaikon
My version of above scripts, for stats per days, with nice formatted output ...
我的上述脚本版本,每天的统计数据,具有很好的格式化输出......
#!/bin/bash
# Calculate num of changed, new lines on git per days (show your productivity)
ds() {
date --date=" days ago" +%Y-%m-%d
}
echo "GIT changes stat: Date, Total lines modified (New added, Changed)"
for week in {13..0}
do
# git log outputs lines like:
# 11 10 path/to/your/file.java
# => add first two columns with awk
lines_all=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=; s+=} END {print s/1}')
lines_chg=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=;} END {print s/1}')
lines_new=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=} END {print s/1}')
#echo -e "$(ds $week): $lines_all \t\t(new: $lines_new, \tchanged: $lines_chg)"
printf "%10s: %10s \t\t(new: %10s, \tchanged: %10s)\n" $(ds $week) $lines_all $lines_new $lines_chg
done