#!/bin/sh
#
# ---
# see: https://tbaggery.com/2011/08/08/effortless-ctags-with-git.html

# exit as soon as something fails
set -o errexit

# remove tags if exiting unexpected, such as due to the above "set -e"
trap 'rm -f ".git/tags.$$"' EXIT

# generate tags in a temporary file
# shellcheck disable=SC2312
ctags --tag-relative -f.git/tags.$$ "$(for f in $(git ls-files); do
  [ -e "${f}" ] && [ ! -d "${f}" ] && echo "${f}"
done | xargs)"

# move the new tags over the previous tags (atomic-ish?)
mv .git/tags.$$ .git/tags
