#!/bin/sh
set -e

usage () {
    cat <<'EOF'
usage: git modified-since [-iuqh] [<commit>]

Prints list of files that have been modified since the given branch.
Defaults to the repo's default branch.

This script is ideal for passing all locally modified files into your editor, like:
    $ vim `git modified-since`

Options:
-q    Be quiet, only return with 0 exit code when files are modified
-h    Show this help
EOF
}

quiet=0
while getopts qh flag; do
    case "$flag" in
        q) quiet=1;;
        h) usage; exit 0;;
    esac
done
shift $(($OPTIND - 1))

commit="$(git main-branch)"
if [ $# -ge 1 ]; then
    commit="$1"
fi

make_relative () {
    while read f; do
        git relative-path "$f"
    done
}

get_changed_files () {
    git diff --stat --name-only "$commit"... -- | make_relative
}

fail_if_empty () {
    empty=1
    while read line; do
        if [ $quiet -eq 0 ]; then
            echo "$line"
        fi
        empty=0
    done
    test $empty -eq 0
}

get_changed_files | fail_if_empty
