#!/bin/sh
#
# git comma - like git commit, but adds unknown files automatically
#
# ---
# see: https://leahneukirchen.org/blog/archive/2013/01/a-grab-bag-of-git-tricks.html
# source: https://leahneukirchen.org/dotfiles/bin/git-comma

# added=()
added=""

# add unknown files...
# shellcheck disable=SC2312
for arg; do
  if [ -f "${arg}" ] && [ -n "$(git ls-files -o "${arg}")" ]; then
    git add "${arg}"
    # added+=($arg)
    added="${added}${arg}"
  fi
done

# ...reset them when commit is aborted
# shellcheck disable=SC2086
git commit ${@:+-o} "$@" || git reset --quiet -- ${added}
