summaryrefslogtreecommitdiff
path: root/git-activity
blob: 5d5e09dd7d08a01103cbc6506a1deade956b45b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash

set -o nounset

function usage() {
  cat <<EOF
Usage: $0 [-h] [-n] [-s style] [-a name]

Display an activity graph (like the contribution graph on GitHub) for the
current git repository and branch

Available options:

-h, --help      Print this help and exit
-n, --no-space  Remove spaces between week columns
-s, --style     Sets style of the graph: square (default), block, or plus
-a, --author    Select for an author (passed to git log)
EOF
}

# Parse options
space=" "
style=square
author=""
while [[ $# -gt 0 ]]; do
  case "$1" in
    -s|--style)
      style="$2"
      shift 2
      ;;
    -a|--author)
        author="$2"
        shift 2
        ;;
    -n|--no-space) space="" ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "Unexpected option ${1}" >&2
      usage
      exit 1
      ;;
  esac
  shift
done

# Style argument
case "${style}" in
square)
  char_full="■"
  char_void="${char_full}"
  ;;
block)
  char_full="█"
  char_void="${char_full}"
  ;;
plus)
  char_full="✚"
  char_void="•"
  ;;
*)
  echo "error: style '${style}' not recognized (square block plus)" >&2;
  exit 1;;
esac

# Use GNU date if available
if date --version >/dev/null 2>&1 ; then
  function _date { date "$@" ;}
elif gdate >/dev/null 2>&1 ; then
  function _date { gdate "$@" ;}
else
  echo "You need to install coreutils to run this script (command 'gdate' is not available).";
  exit 1;
fi

# Process commits per day
declare -A commits_per_day
commits_max=0
since=$(_date -d "$(_date -d '1 year ago + 1 day' +"%F -%u day")" +"%s")
while read -r commits_n commits_date; do
  (( commits_n > commits_max )) && commits_max=$commits_n
  date_diff=$(( ($(_date --date="${commits_date} 13:00 UTC" "+%s") - since) / (60*60*24) ))
  commits_per_day["${date_diff}"]=$commits_n
done <<< $(git log --author="${author}" --since="${since}" --date=short --pretty=format:'%ad'  | uniq -c)

# Print name of months
current_month=$(_date "+%b")
limit_columns=$(( 2 - ${#space} ))
weeks_in_month=$(( limit_columns + 1 ))
printf "\e[m    "
for week_n in $(seq 0 52); do
  month_week=$(_date -d "1 year ago + ${week_n} weeks" "+%b")
  if [[ "${current_month}" != "${month_week}" ]]; then
    current_month=$month_week
    weeks_in_month=0
    printf "%-3s%s" "${current_month:0:3}" "$space"
  elif [[ $weeks_in_month -gt $limit_columns ]]; then
    printf " %s" "$space"
  fi
  weeks_in_month=$(( weeks_in_month + 1 ))
done
printf "\n"

# Print activity
last_day=$(( ($(_date "+%s") - since) / (60*60*24) ))
name_of_days=("" "Mon" "" "Wed" "" "Fri" "" "")
for day_n in $(seq 0 6); do
  printf '\e[m%-4s' "${name_of_days[day_n]}"
  for week_n in $(seq 0 52); do
    key=$(( week_n * 7 + day_n ))
    if [[ -v commits_per_day["${key}"] ]]; then
      value=$(( ${commits_per_day["${key}"]}00 / commits_max))
      if (( value <= 25 )); then
        # Low activity
        printf "\x1b[38;5;22m%s%s" "$char_full" "$space"
      elif (( value <= 50 )); then
        # Mid-low activity
        printf "\x1b[38;5;28m%s%s" "$char_full" "$space"
      elif (( value <= 75 )); then
        # Mid-high activity
        printf "\x1b[38;5;34m%s%s" "$char_full" "$space"
      else
        # High activity
        printf "\x1b[38;5;40m%s%s" "$char_full" "$space"
      fi
    elif [ $key -lt $last_day ]; then
      # No activity
      printf "\x1b[38;5;250m%s%s" "$char_void" "$space"
    fi
  done
  printf "\n"
done
printf "\n"

# Print legend
printf "\e[m Less "
printf "\x1b[38;5;250m%s " "$char_void"
printf "\x1b[38;5;22m%s " "$char_full"
printf "\x1b[38;5;28m%s " "$char_full"
printf "\x1b[38;5;34m%s " "$char_full"
printf "\x1b[38;5;40m%s " "$char_full"
printf "\e[mMore"
printf "\n"