From 811e68ea640e807d5f5aa86baa06e39c18852d60 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Fri, 18 Nov 2022 17:46:25 +0100 Subject: bin/clone: refactor --- bin/clone | 161 +++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 85 insertions(+), 76 deletions(-) diff --git a/bin/clone b/bin/clone index 8aef6ae..9ee160b 100755 --- a/bin/clone +++ b/bin/clone @@ -6,7 +6,15 @@ # ./clone github.com/robertgzr/dotfiles # ./clone https://bitbucket.org/foo/bar # -# If you don't have gitconfig set up like I have (using aliases for github/bitbucket ssh access) this won't really work +# TODO: does it still? +# This requires url aliases to be setup for the git-clone to work properly: +# +# [url "git@github.com:"] +# insteadOf = "gh:" +# insteadOf = "https://github.com/" +# pushInsteadOf = "github:" +# pushInsteadOf = "git://github.com/" +# set -e [ -n "$DEBUG" ] && set -x @@ -15,101 +23,102 @@ RECURSE_SUBMODULES= TEMP="$(mktemp -d -p /tmp clone.XXXXXXXXXX)" URL= -usage() { - printf "usage: clone [-s][-r] URL\n" - printf "\n" - printf " URL example.com/user/repo\n" - printf " http[s]://example.com/user/repo\n" - printf "\n" - printf " -s shallow clone\n" - printf " -r recurse submodules\n" - printf "\n" - exit 0 -} +while getopts ":hsr" opt; do + case ${opt} in + s) SHALLOW=1 ;; + r) RECURSE_SUBMODULES=1 ;; + h | \?) + printf "usage: clone [-s][-r] URL\n" + printf "\n" + printf " URL example.com/user/repo\n" + printf " http[s]://example.com/user/repo\n" + printf "\n" + printf " -s shallow clone\n" + printf " -r recurse submodules\n" + printf " -n don't cd after cloning\n" + printf "\n" + exit 0 + ;; + esac +done +shift $((OPTIND - 1)) fail() { - printf "error: %s\n" "${1:-error}" - exit "${2:-1}" + printf "error: %s\n" "${1:-error}" + exit "${2:-1}" } confirm() { - printf "%s, ok (y/n)? " "${1:-'do something'}" - read -r yn - if [[ "$yn" = "${yn#[Yy]}" ]]; then - fail "ok, exiting" 0 - fi + printf "%s, ok (y/n)? " "${1:-'do something'}" + read -r yn + if [[ "$yn" = "${yn#[Yy]}" ]]; then + fail "ok, exiting" 0 + fi } git_clone() { - declare -a GIT_FLAGS - if [[ -n "${SHALLOW}" ]]; then - printf "* shallow clone\n" - GIT_FLAGS+=("--depth=${SHALLOW}") - fi - if [[ -n "${RECURSE_SUBMODULES}" ]]; then - printf "* recurse submodules\n" - GIT_FLAGS+=("--recurse-submodules") - fi - git clone "${GIT_FLAGS[@]}" "${@}" + declare -a GIT_FLAGS + if [[ -n "${SHALLOW}" ]]; then + printf "* shallow clone\n" + GIT_FLAGS+=("--depth=${SHALLOW}") + fi + if [[ -n "${RECURSE_SUBMODULES}" ]]; then + printf "* recurse submodules\n" + GIT_FLAGS+=("--recurse-submodules") + fi + git clone "${GIT_FLAGS[@]}" "${@}" } ssh_clone() { - prefix=$1 - in=$2 - to=$3 - IFS='/' read -r -a url <<< "${in}" - ssh_url="${prefix}:${url[1]}/${url[2]}" + prefix=$1 + in=$2 + to=$3 + IFS='/' read -r -a url <<<"${in}" + ssh_url="${prefix}:${url[1]}/${url[2]}" - printf "* via ssh: %s\n" "${ssh_url}" - git_clone "${ssh_url}" "${to}" + printf "* via ssh: %s\n" "${ssh_url}" + git_clone "${ssh_url}" "${to}" } http_clone() { - to=$2 - http_url="https://${URL}" + to=$2 + http_url="https://${URL}" - printf "* via http: %s\n" "${ssh_url}" - git_clone "${http_url}" "${to}" + printf "* via http: %s\n" "${ssh_url}" + git_clone "${http_url}" "${to}" } clone() { - in=$1 - to=$2 - IFS='/' read -r -a url <<< "${in}" + in=$1 + to=$2 - case "${url[0]}" in - github.com) ssh_clone git@github.com "${in}" "${to}";; - gitlab.com) ssh_clone git@gitlab.com "${in}" "${to}";; - bitbucket.org) ssh_clone git@bitbucket.org "${in}" "${to}";; - git.sr.ht) ssh_clone git@git.sr.ht "${in}" "${to}";; - * ) http_clone "${url/ /\/}" "${to}";; - esac + mkdir -p "${to}" - if [ -e "${to}/.gitmodules" ]; then - git -C "${to}" submodule update --init --recursive - fi -} + IFS='/' read -r -a url <<<"${in}" + case "${url[0]}" in + github.com) ssh_clone git@github.com "${in}" "${to}" ;; + gitlab.com) ssh_clone git@gitlab.com "${in}" "${to}" ;; + bitbucket.org) ssh_clone git@bitbucket.org "${in}" "${to}" ;; + git.sr.ht) ssh_clone git@git.sr.ht "${in}" "${to}" ;; + *) http_clone "${url/ /\/}" "${to}" ;; + esac + if [ -e "${to}/.gitmodules" ]; then + git -C "${to}" submodule update --init --recursive + fi +} -main() { - while getopts ":hsr" opt; do - case ${opt} in - s) SHALLOW=1 ;; - r) RECURSE_SUBMODULES=1 ;; - h|\?) usage ;; - esac - done - shift $((OPTIND -1)) - - URL=$1 - if [[ -z "${URL}" ]]; then - fail "no input" - else - URL="${URL//(http|https):\/\//}" - fi +URL=$1 +if [[ -z "${URL}" ]]; then + fail "no input" +else + URL="${URL//http*:\/\//}" + URL="${URL//#*/}" +fi - clone "${URL}" "${TEMP}/${URL}" || fail +target="${PWD}/$(dirname "${URL}")" +name="$(basename "${URL}")" - target="${PWD}/$(dirname "${URL}")" - install -d "${target}" || fail - mv "${TEMP}/${URL}" "${target}" || fail -} +if [ ! -d "${target}/${name}" ]; then + clone "${URL}" "${TEMP}/${URL}" || fail -main "${@}" + install -d "${target}" || fail + mv "${TEMP}/${URL}" "${target}" || fail +fi -- cgit 1.4.1