#!/bin/sh
#
# git-unreleased - shows git commits since the last tagged version.
#
# Usage: git unreleased
#
# If library code appears to be contained in `lib` or `libexec` directories,
# show changes pertaining to just that code.

set -e

paths=""
num_libfiles="$(git ls-files -- lib libexec | wc -l)"
if [ "${num_libfiles}" -gt 0 ]; then
  paths="bin lib libexec"
fi

format='%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%ar)%Creset'

show_log() {
  # shellcheck disable=SC2086
  git log --graph --format="${format}" ${1:+${1}..} -- ${paths}
}

desc="$(git describe --tags HEAD 2>/dev/null)" || {
  echo "no releases exist; showing all changes"
  show_log
  exit
}

# check for "<tag>-<num_commits>-<sha>" format
# if echo "${desc}" | grep -q "\-g*"; then
case "${desc}" in
  \*-*-g*)
    tag_with_num="${desc%-*}"
    num_commits="${tag_with_num##*-}"
    tag="${tag_with_num%-*}"
    ;;
  *)
    num_commits=0
    tag="${desc}"
    ;;
esac

if [ "${num_commits}" -gt 0 ]; then
  echo "${num_commits} commits since ${tag}"
  show_log "${tag}"
else
  echo "no commits since ${tag}"
fi
