pub / git_activity

Generates Github-style activity graphs as HTML
git clone https://src.jayvii.de/pub/git_activity.git
Home | Log | Files | Exports | Refs | README | RSS

activity.sh (3302B)


      1 #!/usr/bin/env sh
      2 # SPDX-License-Identifier: AGPL-3.0-or-later
      3 # SPDX-FileCopyrightText: 2024 JayVii <jayvii[AT]posteo[DOT]de>
      4 
      5 # Configuration ----------------------------------------------------------------
      6 
      7 # ensure pretty names are always in english
      8 LANG="en"
      9 
     10 # Fetch Inputs -----------------------------------------------------------------
     11 
     12 # all given arguments are interpreted as git-directories
     13 DIRS="$@"
     14 
     15 # Output Functions -------------------------------------------------------------
     16 
     17 # HTML output
     18 html_output() {
     19   # grab function input
     20   DTE="$1"
     21   CMT="$2"
     22   OPC="$3"
     23   TTL=""
     24   # if date is present, add it to the title
     25   if [ ! -z $DTE ]; then
     26     TTL=${TTL}$(date --date "${DTE}-01" "+%b, %Y");
     27   fi
     28   # if date and comment are present, add separator to the title
     29   if [ ! -z $DTE ] && [ ! -z $CMT ]; then
     30     TTL="${TTL}: "
     31   fi
     32   # if comment is present, add it to the title
     33   if [ ! -z $CMT ]; then TTL="${TTL}${CMT} commits"; fi
     34   # re-scale opacity linearly between 10-100
     35   OPC=$(echo "(($OPC * 90) / 100) + 10" | bc)
     36   # create HTML arguments
     37   ID="activity-$DTE"
     38   CL="activitypoint"
     39   STL="opacity:calc($OPC/100);"
     40   ARGS="id=\"$ID\" class=\"activitypoint\" style=\"$STL\" title=\"$TTL\""
     41   # return HTML string
     42   printf "<span $ARGS>&#9632;</span>"
     43 }
     44 
     45 # Collect activity -------------------------------------------------------------
     46 
     47 # By Months
     48 ACTIVITY=""
     49 for DIR in $DIRS; do
     50   ACTIVITY="${ACTIVITY}$(
     51     git \
     52       -C $DIR \
     53       log --date=short --pretty=format:%ad | \
     54       awk '{print substr($1, 1, 7)}'
     55   )\n"
     56 done
     57 
     58 # aggregate activity
     59 AGGREGATE=$(
     60   printf "$ACTIVITY" | \
     61     sort | \
     62     uniq -c
     63 )
     64 
     65 # Process Activity -------------------------------------------------------------
     66 
     67 # get timeframe
     68 MIN_TIME=$(echo "$AGGREGATE" | head -n 1 | awk '{ print $NF }')
     69 MAX_TIME=$(date +%Y-%m -d "next month")
     70 
     71 # get value range
     72 MIN_VAL=0
     73 MAX_VAL=$(echo "$AGGREGATE" | awk '{ print $1 }' | sort -n | tail -n 1)
     74 if [ -z $MAX_VAL ]; then MAX_VAL=1; fi
     75 
     76 # Generate output --------------------------------------------------------------
     77 
     78 # initilise html
     79 MIN_TIME_PRETTY=$(date --date "${MIN_TIME}-01" "+%b, %Y")
     80 MAX_TIME_PRETTY=$(date --date "${MAX_TIME}-01 -1 day" "+%b, %Y")
     81 HTML="<h6>Activity: ${MIN_TIME_PRETTY} - ${MAX_TIME_PRETTY}</h6>"
     82 
     83 # cycle through months from earliest timepoint
     84 CUR_TIME=$MIN_TIME
     85 while [ $CUR_TIME != $MAX_TIME ]; do
     86 
     87   # fetch current value
     88   CUR_VAL=$(echo "$AGGREGATE" | grep $CUR_TIME | awk '{ print $1 }')
     89   if [ -z $CUR_VAL ]; then CUR_VAL=0; fi
     90 
     91   # normalise current value [0, 100]
     92   NRM_VAL=$(echo "${CUR_VAL} * 100 / ${MAX_VAL}" | bc)
     93   if [ -z $NRM_VAL ]; then NRM_VAL=0; fi
     94 
     95   # output HTML
     96   HTML=${HTML}$(html_output "$CUR_TIME" "$CUR_VAL" "$NRM_VAL")
     97 
     98   # update current time
     99   CUR_TIME=$(date --date "${CUR_TIME}-01 +1 month" +%Y-%m)
    100 
    101   # reset variables for next round
    102   unset CUR_VAL
    103   unset NRM_VAL
    104 
    105 done
    106 
    107 # add legend
    108 HTML="${HTML}<br><p id=\"activitylegend\">Less "
    109 for LV in $(seq 0 25 100); do
    110 
    111   # Calculate label value
    112   LND_VAL=$(($LV * $MAX_VAL / 100))
    113 
    114   # Generate HTML
    115   HTML=${HTML}$(html_output "" $LND_VAL $LV)
    116 
    117 done
    118 HTML="${HTML} More</p>"
    119 
    120 # add "activitybar" div
    121 HTML="<div id=\"activitybar\">$HTML</div>"
    122 
    123 # Print output -----------------------------------------------------------------
    124 
    125 printf "$HTML"