Skip to content

Instantly share code, notes, and snippets.

@arzzen
Last active June 21, 2025 14:08
Show Gist options
  • Save arzzen/ded9aed22d5d9c0b746221ddfa781361 to your computer and use it in GitHub Desktop.
Save arzzen/ded9aed22d5d9c0b746221ddfa781361 to your computer and use it in GitHub Desktop.
git_commit_goal_calendar.sh
#!/bin/bash
_goal=${_GIT_GOAL_COMMITS:-}
if [[ -n "${_goal}" ]]; then
_goal=$_goal
else
_goal=5
fi
git_commit_goal_calendar() {
local GOAL="${_goal}"
echo -e "Git Commit Goal Calendar\n"
local months=()
local today=$(date +%Y-%m-01)
for ((offset=11; offset>=0; offset--)); do
months+=("$(date -d "$today -$offset month" +%Y-%m)")
done
render_month() {
local ym=$1
local year=${ym%-*}
local month=${ym#*-}
#local days_in_month=$(cal "$((10#$month))" "$year" | awk 'NF {d = $NF}; END {print d}')
local days_in_month=$(date -d "$year-$month-01 +1 month -1 day" +%d)
local first_weekday=$(date -d "$year-$month-01" +%u) # 1=Mon
local rows=()
header="$(date -d "$ym-01" +%B) $year"
headerlen=$(echo $header | wc -c)
headeroffset=$(((21 - headerlen)))
rows+=$(printf "${header} %${headeroffset}s")
rows+=("Mo Tu We Th Fr Sa Su ")
local line=""
local day=1
local started=0
while (( ${#rows[@]} < 8 )); do
line=""
for dow in {1..7}; do
if (( !started && dow < first_weekday )); then
line+=" "
elif (( day <= days_in_month )); then
date_str=$(printf "$year-%02d-%02d" "$((10#$month))" "$((10#$day))")
commit_count=$(git log --since="$date_str 00:00" --until="$date_str 23:59" --pretty=oneline --no-merges 2>/dev/null | wc -l)
if [[ $commit_count -eq 0 ]]; then
color="\033[90m"
elif [[ $commit_count -lt $GOAL ]]; then
color="\033[31m"
elif [[ $commit_count -eq $GOAL ]]; then
color="\033[33m"
else
color="\033[32m"
fi
if [[ "$date_str" == "$(date +%Y-%m-%d)" ]]; then
color="\033[1;37m"
fi
line+=$(printf "$color%2d\033[0m " "$day")
((day++))
started=1
else
line+=" "
fi
done
rows+=("$line")
line=""
done
for row in "${rows[@]}"; do
echo "$row"
done
rows=()
}
for ((i=0; i<12; i+=3)); do
mapfile -t M0 < <(render_month "${months[i]}")
mapfile -t M1 < <(render_month "${months[i+1]}")
mapfile -t M2 < <(render_month "${months[i+2]}")
for ((r=0; r<8; r++)); do
printf "%-10s %-10s %-25s\n" "${M0[r]}" "${M1[r]}" "${M2[r]}"
done
echo
done
echo -e "Legend:"
echo -e " \033[90m■\033[0m no commits \033[31m■\033[0m less than $GOAL \033[33m■\033[0m exactly $GOAL \033[32m■\033[0m more than $GOAL"
}
git_commit_goal_calendar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment