ernesto
ernesto

Reputation: 1818

Git: get number of changed lines per day

I'd like to make a diagram of added/removed/changed lines in a git repository per day and/or week. I do not want to count the number of commits.

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?

Thank you!

Upvotes: 40

Views: 15298

Answers (7)

Adam Shand
Adam Shand

Reputation: 222

Updated to work on macOS.

ds() {
  if [ "${OSTYPE:0:6}" = "darwin" ]; then
    date -v-${1}d +%Y-%m-%d
  else
    date --date="$1 days ago" +%Y-%m-%d
  fi
}

echo "GIT changes stat: Date, Total lines modified (new, changed)"
for week in {13..1}; do
  lines_all=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=$1; s+=$2} END {print s/1}')
  lines_chg=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=$1;} END {print s/1}')
  lines_new=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=$2}  END {print s/1}')
  printf "%10s: %10s \t\t(new: %10s, \tchanged: %10s)\n" $(ds $week) $lines_all $lines_new $lines_chg
done

Upvotes: 1

mishaikon
mishaikon

Reputation: 540

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="$1 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+=$1; s+=$2} END {print s/1}')
  lines_chg=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=$1;} END {print s/1}')
  lines_new=$(git --no-pager log --after=$(ds $week) --before=$(ds $(($week - 1))) --format=format: --numstat | awk '{s+=$2}  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

Upvotes: 5

Sven Marnach
Sven Marnach

Reputation: 601499

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

git log --after=2011-01-01 --before=2011-01-31 --format=format: --shortstat master

Upvotes: 40

JuanPablo
JuanPablo

Reputation: 24754

#!/bin/bash                                                                                                                                                                         
a=""                                                                                                                                                                                
b=""                                                                                                                                                                                
for i in $(seq 1 10)                                                                                                                                                                
do                                                                                                                                                                                  
    b=$(git diff --shortstat "@{ $i day ago }")                                                                                                                                     
    if [[ "$b" != "$a" ]]; then                                                                                                                                                     
        echo $i "day ago" $b                                                                                                                                                        
    fi                                                                                                                                                                              
    a=$b                                                                                                                                                                            
done 

output

1 day ago 2 files changed, 144 insertions(+), 21 deletions(-)
3 day ago 2 files changed, 227 insertions(+), 73 deletions(-)

the gist

Upvotes: 27

gnmerritt
gnmerritt

Reputation: 576

Here's a bash script that calculates the total number of changed lines per week:

#!/bin/bash

ds() {
  date --date="$1 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+=$1; s+=$2} END {print s}')
  echo "$(ds $week),$lines"
done

Output is in CSV format and looks like:

Week,Lines Changed
2016-12-21,5989
2016-12-28,17179

Upvotes: 6

yuan3y
yuan3y

Reputation: 305

Adapting from gnmerritt's answer, this script will generate number of lines of add and delete for each calendar day.

#!/bin/bash

ds() {
  date --date="$1 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+=$1; t+=$2} END {printf "+"; printf s;printf ",-"; printf t;}')
  if [[ "$lines" != "+,-" ]]; then
      echo "$(ds $day),$lines"
  else
      echo "$(ds $day),0,0"
  fi
done

(Link: Github Gist)

Upvotes: 4

Markus Duft
Markus Duft

Reputation: 211

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:

( 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 :)

Upvotes: 6

Related Questions