Refactor myDailyStats function for improved readability and performance

This commit is contained in:
arzzen 2026-04-13 12:15:27 +02:00
parent 0f998185e8
commit 0dc1fcf0c4

View file

@ -721,33 +721,20 @@ function changelogs() {
function myDailyStats() { function myDailyStats() {
optionPicked "My daily status:" optionPicked "My daily status:"
local my_name="$(git config user.name)" local -r my_name="$(git config user.name)"
local my_email="$(git config user.email)" local -r my_email="$(git config user.email)"
local today_start local -r today_start="$(date "+%Y-%m-%dT00:00:00")"
local today_end local -r today_end="$(date "+%Y-%m-%dT23:59:59")"
local yesterday_start local -r yesterday_start="$(date -d "yesterday 00:00:00" "+%Y-%m-%dT%H:%M:%S")"
local yesterday_end local -r yesterday_end="$(date -d "yesterday 23:59:59" "+%Y-%m-%dT%H:%M:%S")"
local today_date local -r today_date="$(date "+%Y-%m-%d")"
local yesterday_date local -r yesterday_date="$(date -d "yesterday" "+%Y-%m-%d")"
today_start="$(date "+%Y-%m-%dT00:00:00")"
today_end="$(date "+%Y-%m-%dT23:59:59")"
yesterday_start="$(date -d "yesterday 00:00:00" "+%Y-%m-%dT%H:%M:%S")"
yesterday_end="$(date -d "yesterday 23:59:59" "+%Y-%m-%dT%H:%M:%S")"
today_date="$(date "+%Y-%m-%d")"
yesterday_date="$(date -d "yesterday" "+%Y-%m-%d")"
# Current user's totals for today # Current user's totals for today
local my_commits_today local -r my_commits_today="$(git rev-list --count --author="$my_name" $_merges \
local my_today_stats --since="$today_start" --until="$today_end" HEAD)"
local my_files_today
local my_insertions_today
local my_deletions_today
my_commits_today=$(git rev-list --count --author="$my_name" $_merges \ local -r my_today_stats="$(git -c log.showSignature=false log --use-mailmap $_merges \
--since="$today_start" --until="$today_end" HEAD)
my_today_stats=$(git -c log.showSignature=false log --use-mailmap $_merges \
--author="$my_name" --numstat --format='' \ --author="$my_name" --numstat --format='' \
--since="$today_start" --until="$today_end" $_log_options $_pathspec \ --since="$today_start" --until="$today_end" $_log_options $_pathspec \
| LC_ALL=C awk -F'\t' ' | LC_ALL=C awk -F'\t' '
@ -762,11 +749,11 @@ function myDailyStats() {
} }
printf "%d|%d|%d", file_count + 0, ins + 0, del + 0; printf "%d|%d|%d", file_count + 0, ins + 0, del + 0;
} }
') ')"
my_files_today=$(echo "$my_today_stats" | awk -F'|' '{print $1}') local -r my_files_today="$(echo "$my_today_stats" | awk -F'|' '{print $1}')"
my_insertions_today=$(echo "$my_today_stats" | awk -F'|' '{print $2}') local -r my_insertions_today="$(echo "$my_today_stats" | awk -F'|' '{print $2}')"
my_deletions_today=$(echo "$my_today_stats" | awk -F'|' '{print $3}') local -r my_deletions_today="$(echo "$my_today_stats" | awk -F'|' '{print $3}')"
echo -e "\tfor ${my_name} (${today_date}):" echo -e "\tfor ${my_name} (${today_date}):"
echo -e "\t commits: ${my_commits_today}" echo -e "\t commits: ${my_commits_today}"
@ -775,18 +762,7 @@ function myDailyStats() {
echo -e "\t deletions: ${my_deletions_today}" echo -e "\t deletions: ${my_deletions_today}"
# Compare repository totals between yesterday and today # Compare repository totals between yesterday and today
local today_stats local -r today_stats="$(git -c log.showSignature=false log --use-mailmap $_merges \
local yday_stats
local today_commits
local today_files
local today_insertions
local today_deletions
local yday_commits
local yday_files
local yday_insertions
local yday_deletions
today_stats=$(git -c log.showSignature=false log --use-mailmap $_merges \
--numstat --format='%H' --since="$today_start" --until="$today_end" \ --numstat --format='%H' --since="$today_start" --until="$today_end" \
$_log_options $_pathspec | LC_ALL=C awk -F'\t' ' $_log_options $_pathspec | LC_ALL=C awk -F'\t' '
/^[0-9a-f]{40}$/ { commits++; next } /^[0-9a-f]{40}$/ { commits++; next }
@ -801,9 +777,9 @@ function myDailyStats() {
} }
printf "%d|%d|%d|%d", commits + 0, file_count + 0, ins + 0, del + 0; printf "%d|%d|%d|%d", commits + 0, file_count + 0, ins + 0, del + 0;
} }
') ')"
yday_stats=$(git -c log.showSignature=false log --use-mailmap $_merges \ local -r yday_stats="$(git -c log.showSignature=false log --use-mailmap $_merges \
--numstat --format='%H' --since="$yesterday_start" --until="$yesterday_end" \ --numstat --format='%H' --since="$yesterday_start" --until="$yesterday_end" \
$_log_options $_pathspec | LC_ALL=C awk -F'\t' ' $_log_options $_pathspec | LC_ALL=C awk -F'\t' '
/^[0-9a-f]{40}$/ { commits++; next } /^[0-9a-f]{40}$/ { commits++; next }
@ -818,17 +794,17 @@ function myDailyStats() {
} }
printf "%d|%d|%d|%d", commits + 0, file_count + 0, ins + 0, del + 0; printf "%d|%d|%d|%d", commits + 0, file_count + 0, ins + 0, del + 0;
} }
') ')"
today_commits=$(echo "$today_stats" | awk -F'|' '{print $1}') local -r today_commits="$(echo "$today_stats" | awk -F'|' '{print $1}')"
today_files=$(echo "$today_stats" | awk -F'|' '{print $2}') local -r today_files="$(echo "$today_stats" | awk -F'|' '{print $2}')"
today_insertions=$(echo "$today_stats" | awk -F'|' '{print $3}') local -r today_insertions="$(echo "$today_stats" | awk -F'|' '{print $3}')"
today_deletions=$(echo "$today_stats" | awk -F'|' '{print $4}') local -r today_deletions="$(echo "$today_stats" | awk -F'|' '{print $4}')"
yday_commits=$(echo "$yday_stats" | awk -F'|' '{print $1}') local -r yday_commits="$(echo "$yday_stats" | awk -F'|' '{print $1}')"
yday_files=$(echo "$yday_stats" | awk -F'|' '{print $2}') local -r yday_files="$(echo "$yday_stats" | awk -F'|' '{print $2}')"
yday_insertions=$(echo "$yday_stats" | awk -F'|' '{print $3}') local -r yday_insertions="$(echo "$yday_stats" | awk -F'|' '{print $3}')"
yday_deletions=$(echo "$yday_stats" | awk -F'|' '{print $4}') local -r yday_deletions="$(echo "$yday_stats" | awk -F'|' '{print $4}')"
echo -e "\n\trepo comparison (${yesterday_date} -> ${today_date}):" echo -e "\n\trepo comparison (${yesterday_date} -> ${today_date}):"
echo -e "\t commits: ${yday_commits} -> ${today_commits} (delta: $((today_commits - yday_commits)))" echo -e "\t commits: ${yday_commits} -> ${today_commits} (delta: $((today_commits - yday_commits)))"
@ -838,8 +814,7 @@ function myDailyStats() {
# Most changed files today (all authors) # Most changed files today (all authors)
echo -e "\n\tmost changed files today:" echo -e "\n\tmost changed files today:"
local top_changed_files local -r top_changed_files="$(git -c log.showSignature=false log --use-mailmap $_merges \
top_changed_files=$(git -c log.showSignature=false log --use-mailmap $_merges \
--numstat --format='' --since="$today_start" --until="$today_end" \ --numstat --format='' --since="$today_start" --until="$today_end" \
$_log_options $_pathspec | LC_ALL=C awk -F'\t' ' $_log_options $_pathspec | LC_ALL=C awk -F'\t' '
$1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+$/ { $1 ~ /^[0-9]+$/ && $2 ~ /^[0-9]+$/ {
@ -852,7 +827,7 @@ function myDailyStats() {
printf "%d\t%d\t%d\t%s\n", files[f], ins[f], del[f], f; printf "%d\t%d\t%d\t%s\n", files[f], ins[f], del[f], f;
} }
} }
' | sort -nr | head -n 5) ' | sort -nr | head -n 5)"
if [[ -n "$top_changed_files" ]]; then if [[ -n "$top_changed_files" ]]; then
echo "$top_changed_files" | LC_ALL=C awk -F'\t' '{ echo "$top_changed_files" | LC_ALL=C awk -F'\t' '{
@ -863,14 +838,12 @@ function myDailyStats() {
fi fi
# First and last commit of today # First and last commit of today
local first_today_commit local -r first_today_commit="$(git -c log.showSignature=false log --use-mailmap $_merges \
local last_today_commit
first_today_commit=$(git -c log.showSignature=false log --use-mailmap $_merges \
--reverse --since="$today_start" --until="$today_end" \ --reverse --since="$today_start" --until="$today_end" \
--date=iso --format='%ad | %aN | %s' $_log_options $_pathspec | head -n 1) --date=iso --format='%ad | %aN | %s' $_log_options $_pathspec | head -n 1)"
last_today_commit=$(git -c log.showSignature=false log --use-mailmap $_merges \ local -r last_today_commit="$(git -c log.showSignature=false log --use-mailmap $_merges \
--since="$today_start" --until="$today_end" \ --since="$today_start" --until="$today_end" \
--date=iso --format='%ad | %aN | %s' $_log_options $_pathspec | head -n 1) --date=iso --format='%ad | %aN | %s' $_log_options $_pathspec | head -n 1)"
echo -e "\n\ttoday commit window:" echo -e "\n\ttoday commit window:"
if [[ -n "$first_today_commit" ]]; then if [[ -n "$first_today_commit" ]]; then
@ -882,11 +855,10 @@ function myDailyStats() {
# Most active users today (excluding current user) # Most active users today (excluding current user)
echo -e "\n\tmost active users today (excluding you):" echo -e "\n\tmost active users today (excluding you):"
local active_users local -r active_users="$(git -c log.showSignature=false log --use-mailmap $_merges \
active_users=$(git -c log.showSignature=false log --use-mailmap $_merges \
--since="$today_start" --until="$today_end" --format='%aN|%aE' \ --since="$today_start" --until="$today_end" --format='%aN|%aE' \
$_log_options $_pathspec | LC_ALL=C awk -F'|' -v me_name="$my_name" -v me_email="$my_email" ' $_log_options $_pathspec | LC_ALL=C awk -F'|' -v git_name="$my_name" -v git_email="$my_email" '
$1 != me_name && $2 != me_email { $1 != git_name && $2 != git_email {
key = $1 " <" $2 ">"; key = $1 " <" $2 ">";
count[key]++; count[key]++;
} }
@ -895,7 +867,7 @@ function myDailyStats() {
printf "%d\t%s\n", count[k], k; printf "%d\t%s\n", count[k], k;
} }
} }
' | sort -nr | head -n 5) ' | sort -nr | head -n 5)"
if [[ -n "$active_users" ]]; then if [[ -n "$active_users" ]]; then
echo "$active_users" | LC_ALL=C awk -F'\t' '{ echo "$active_users" | LC_ALL=C awk -F'\t' '{