Skip to content

Instantly share code, notes, and snippets.

@marjamis
Created November 23, 2021 02:36
Show Gist options
  • Save marjamis/60edc1bcc20f59279050bf00eadba423 to your computer and use it in GitHub Desktop.
Save marjamis/60edc1bcc20f59279050bf00eadba423 to your computer and use it in GitHub Desktop.
Basic way to build up generating of git stats with bash with JSON output. Just an example...
#!/bin/bash
# echo "## Each files last update, sorted from oldest to newest"
# cd .. && git ls-tree -r --name-only HEAD | while read filename; do echo "$(git log -1 --format="%aI" -- $filename) $filename"; done | sort -rk1
function lastCommit {
git log -1 --format=%cI
}
function addNumber {
echo $content | jq '. += {"'"$1"'": '"$2"'}'
}
function addString {
echo $content | jq '. += {"'"$1"'": "'"$2"'"}'
}
function addStringList {
data=$($2)
IFS=$'\n' read -d "" -r -a items <<<"$data"
content=$(echo $content | jq '. += {"'"$1"'": []}')
for i in "${items[@]}"; do
content=$(echo $content | jq '.'"$1"' += ["'"$i"'"]')
done
echo $content
}
function topCommitters {
git shortlog -sn --no-merges --all
}
function linesChanged {
# 2 months, 6 months and all time for all stats
# have a flag of content that's missing or not updated in X time
previousTimeframe="2 months"
d=$(date -d "$previousTimeframe ago" "+%b %d %Y" | tr [:lower:] [:upper:])
IFS=$'\n'
for i in $(git shortlog -sn --no-merges --since "$d"); do
name=$(echo $i | awk '{printf("%s %s\n",$2,$3)}')
printf "$name = "
git log --numstat --pretty="%H" --author=$name --since "$d" | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d, -%d\n", plus, minus)}'
done
}
content='{}'
content=$(addString "last_commit" $(lastCommit))
content=$(addNumber "number_of_x" 12)
content=$(addStringList "top_committers" "topCommitters")
content=$(addStringList "lines_changed_in_last_2_months" "linesChanged")
echo $content | jq .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment