#!/bin/sh -e
#
# git-clone-subdirectory - clone only a subdirectory of a git repository
#
# ---
# see: https://overflow.adminforge.de/questions/600079/#52269934

if [ -z "$1" ]; then
  printf '%s\n' "$(basename "$0") [repo URL]" >&2
  exit 1
fi

repourl="$1"

# reponame="$(printf '%b' "${repourl}" | grep -o '/\w*.$')"
# dir="$(printf '%b' "${reponame}" | sed 's/\///g')"
repodir="$(printf '%b' "${repourl}" | cut -d '/' -f 5)"

git clone --no-checkout --depth=1 --filter=tree:0 "${repourl}"

cd "${repodir}" || exit
printf '%s' "Enter subdirectory name:" ' '
read -r ans
git sparse-checkout set --no-cone /"${ans}"
git checkout
