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 (3197B)


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