#!/bin/sh -e
#
# git neck [-r] [COMMIT] - show commits until first branching point

[ "$1" = -r ] && shift && R=-r

COMMIT=$(git rev-parse --no-flags --default HEAD "$@")
# skip first elements of trail
# shellcheck disable=SC2086
TORSO=$(git trail ${R} ${COMMIT} | cut -d' ' -f2 | uniq | sed -n 2p)

# fall back to initial commit on empty trail
# shellcheck disable=SC2312
: "${TORSO:=$(git rev-list --max-parents=0 HEAD)}"

# shellcheck disable=SC2046,SC2086,SC2312
git log --oneline $(git rev-parse --no-revs "$@") ${COMMIT}...${TORSO}
