#!/usr/bin/env zsh
## Adds and commits a file in one command.
## Source: git-extra-commands

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

added=()

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

# ...reset them when commit is aborted
git commit ${@:+-o} "$@" || git reset -q -- "$added"
