#!/bin/sh
set -e

usage () {
    cat <<'EOF'
usage: git skip [-ah] <file> [<file> ...]

Skip (ignore) local changes to git-aware files to prevent them from
showing up in status reports or diffs, as if they haven't been changed
locally at all.

To reset, use git-unskip.

Options:
-a    Skip all locally modified files
-h    Show this help
EOF
}

all=0
while getopts ah flag; do
    case "$flag" in
        a) all=1;;
        h) usage; exit 0;;
    esac
done
shift $(($OPTIND - 1))

if [ $all -eq 1 ]; then
    # NUL-delimited and passed after `--`, so filenames containing globs,
    # spaces, or a leading `-` can't glob-expand or pose as flags. `ls-files
    # -m` is the tracked worktree-modified/deleted set (git-status quotes such
    # names, ls-files does not).
    git ls-files -m -z | xargs -0 git update-index --skip-worktree --
else
    if [ $# -gt 0 ]; then
        git update-index --skip-worktree -- "$@"
    else
        usage >&2
        exit 2
    fi
fi
