oskbor oskbor - 2 months ago 15
Git Question

Search git history and group by author

Let say I want to count how many

if
statements have been commited and then group them by author, how would I do that?

Example of what I want to do:

git count-by-author "if ("

245 Author A
199 Author B
20 Author C


I'm mainly interested in number of
if
s introduced, and not modified
if
statements, if that is possible.

qzb qzb
Answer

There is no pure git solution, but you can use following bash script to get such stats:

for sha in $(git log --format=%H); do
  author="$(git show --format=%an $sha | head -n1)"

  removed_if_statements=$(git show $sha | grep -E '^\-(.* )?if \(' | wc -l)
  added_if_statements=$(git show $sha | grep -E '^\+(.* )?if \(' | wc -l)

  delta=$(( $added_if_statements - $removed_if_statements ))

  if [ $delta -gt 0 ]; then
    for x in {1..$delta}; do
      echo $author
    done
  fi
done | sort | uniq -c | sort -rg
Comments