Skip to content

Instantly share code, notes, and snippets.

@asqd
Last active June 17, 2021 14:56
Show Gist options
  • Save asqd/6efcffe060049c526d44b865bb5d14a2 to your computer and use it in GitHub Desktop.
Save asqd/6efcffe060049c526d44b865bb5d14a2 to your computer and use it in GitHub Desktop.
get stats from git repository
#!/bin/bash
strict_mode=false
PARAMS=()
usage() {
cat <<EOF
Usage: gitstat.sh [since] [before] [arguments]
-h, --help Display this help and exit
-o, --out OUTFILE Write stats to OUTFILE instead of standard output
-a, --author USER_NAME Limit stats by author
-s, --strict Strict mode. Statrs git log with:
--ignore-space-change --ignore-all-space
--ignore-submodules --find-copies-harder
EOF
}
git_set_head_message() {
cat <<EOF
Please set HEAD by yourself and run script again
For example, you can use 'git remote set-head origin --auto'
EOF
}
set_head() {
git remote set-head origin --auto
}
check_head() {
if [[ -z $(git branch -r | grep HEAD) ]]; then
printf "%s\n" 'HEAD is not set. Do you want to set it automatically (y/N)?'
read -r decision
case "$decision" in
y | Y)
decision='Y'
set_head
;;
*)
git_set_head_message
;;
esac
else
decision='Y'
fi
}
while (("$#")); do
case "$1" in
-h | --help)
usage
exit 0
;;
-s | --strict)
strict_mode=true
shift
;;
-o | --out)
output="$2"
shift 2
;;
-a | --author)
author="$2"
shift 2
;;
*)
PARAMS+=("$1")
shift
;;
esac
done
check_head
if [[ $decision != 'Y' ]]; then
exit 0
fi
if [[ "$OSTYPE" == "darwin"* ]]; then
since=${PARAMS[0]:-$(date -v-14d '+%d %m %Y')}
else
since=${PARAMS[0]:-$(date -d '14 days' '+%d %m %Y')}
fi
before=${PARAMS[1]:-$(date '+%d %m %Y')}
stats_period="Stats from $since to $before"
printf "%s\n" "$stats_period"
authors=$(git log origin --all --no-merges --format='%aN' --since="$since" --before="$before" | sort -u)
IFS=$'\n' read -d '' -r -a authors < <(printf '%s\n' "$authors")
IFS=' '
gitlog='git log origin --all --shortstat --no-merges --diff-filter=ACDM'
if $strict_mode; then
gitlog+=' --ignore-space-change --ignore-all-space --ignore-submodules --find-copies-harder -M '
fi
author_stats() {
awk -v author="$author" \
'{files+=$1; inserted+=$4; deleted+=$6; delta+=$4-$6; hoc+=$4+$6; ratio=deleted/inserted} END {printf "\n\nCommit stats by %s:\n- Files changed (total).. %s\n- Lines added (total).... %s\n- Lines deleted (total).. %s\n- Total lines (delta).... %s\n- Hits Of Code........... %s\n- Add./Del. ratio (1:n).. 1 : %s\n\n", author, files, inserted, deleted, delta, hoc, ratio }'
}
stats=''
if [[ -n $author ]]; then
stats+=$($gitlog --author="$author" --since="$since" --before="$before" |
grep -E "fil(e|es) changed" | author_stats "$author")
printf '.'
else
for author in "${authors[@]}"; do
stats+=$($gitlog --author="$author" --since="$since" --before="$before" |
grep -E "fil(e|es) changed" | author_stats "$author")
printf '.'
done
fi
if [[ -n $output ]]; then
printf "%s %s" "$stats_period" "$stats" >"$output"
else
printf "%s\n" "$stats"
fi
@asqd
Copy link
Author

asqd commented Jun 17, 2021

Что нового?

  • Добавлена метрика Hits Of Code
  • Добавлена фильтрация по автору

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment