summary refs log tree commit diff
path: root/bin/clone
diff options
context:
space:
mode:
authorRobert Günzler <r@gnzler.io>2020-12-07 19:51:41 +0100
committerRobert Günzler <r@gnzler.io>2021-01-21 19:39:12 +0100
commitb3d9281a36dd656b1c19e3733f60794a3eaea598 (patch)
treebaa537acdd95cef422d2b739f179d1411277002e /bin/clone
parentaf2e161ec08615d848eaf4c485e283f334719934 (diff)
bin/clone: modernize
Diffstat (limited to 'bin/clone')
-rwxr-xr-xbin/clone168
1 files changed, 78 insertions, 90 deletions
diff --git a/bin/clone b/bin/clone
index 1882a25..8aef6ae 100755
--- a/bin/clone
+++ b/bin/clone
@@ -8,120 +8,108 @@
 #
 # If you don't have gitconfig set up like I have (using aliases for github/bitbucket ssh access) this won't really work
 set -e
+[ -n "$DEBUG" ] && set -x
 
-SHALLOW=0
+SHALLOW=
 RECURSE_SUBMODULES=
 TEMP="$(mktemp -d -p /tmp clone.XXXXXXXXXX)"
+URL=
 
-URL=$1
-if [[ -z "${URL}" ]]; then
-    fail "no input"
-else
-    URL="${URL//(http|https):\/\//}"
-fi
+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
+}
 
-make_dirs() {
-    local url=$1
+fail() {
+    printf "error: %s\n" "${1:-error}"
+    exit "${2:-1}"
+}
 
-    if [[ -d "${url}" ]]
-    then
-        echo "exists: ${url}"
-        return
+confirm() {
+    printf "%s, ok (y/n)? " "${1:-'do something'}"
+    read -r yn
+    if [[ "$yn" = "${yn#[Yy]}" ]]; then
+        fail "ok, exiting" 0
     fi
-
-    confirm "create ${url}"
-    mkdir -p "${url}"
 }
 
-clone() {
-    local in=$1
-    local to=$2
-    IFS='/' read -r -a url <<< "${in}"
-
-    case "${url[0]}" in
-        github.com ) ssh_clone gh "${in}" "${to}";;
-        gitlab.com ) ssh_clone gl "${in}" "${to}";;
-        bitbucket.org ) ssh_clone bb "${in}" "${to}";;
-        git.sr.ht ) ssh_clone srht "${in}" "${to}";;
-        * ) http_clone "${url[@]// /\/}" "${to}";;
-    esac
+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[@]}" "${@}"
 }
-
 ssh_clone() {
-    local prefix=$1
-    local in=$2
-    local to=$3
+    prefix=$1
+    in=$2
+    to=$3
     IFS='/' read -r -a url <<< "${in}"
-    local ssh_url="${prefix}:${url[1]}/${url[2]}"
+    ssh_url="${prefix}:${url[1]}/${url[2]}"
 
-    # confirm "clone $ssh_url"
+    printf "* via ssh: %s\n" "${ssh_url}"
     git_clone "${ssh_url}" "${to}"
 }
-
 http_clone() {
-    local to=$2
-    local http_url="https://${URL}"
+    to=$2
+    http_url="https://${URL}"
 
-    # confirm "clone $http_url"
-    git_clone "${GIT_FLAGS}" "${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}"
 
-git_clone() {
-    declare -a GIT_FLAGS
-    if [[ ${SHALLOW} -ne 0 ]]; then
-        GIT_FLAGS+="--depth=${SHALLOW}"
-    fi
-    if [[ ! -z ${RECURSE_SUBMODULES} ]]; then
-        GIT_FLAGS+="--recurse-submodules"
+    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
-    git clone "${GIT_FLAGS[@]}" "${@}"
 }
 
-fail() {
-    echo "${1:-error}"
-    exit ${2:-1}
-}
 
-confirm() {
-    echo -n "${1:-'do something'}, ok (y/n)? "
-    read yn
-    if [[ "$yn" = "${yn#[Yy]}" ]]; then
-        fail "ok, exiting" 0
+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
-}
-
-usage() {
-    echo "usage: clone [-s][-r] URL"
-    echo ""
-    echo -e " -s\tshallow clone"
-    echo -e " -r\trecurse submodules"
-    echo -e " URL\texample.com/user/repo"
-    echo -e "\thttp[s]://example.com/user/repo"
-    exit 0
-}
 
-while getopts ":hsr" opt; do
-    case ${opt} in
-        s )
-            echo "Shallow clone"
-            SHALLOW=1
-            ;;
-        r )
-            echo "Recurse submodules"
-            RECURSE_SUBMODULES=1
-            ;;
-        h ) 
-            usage
-            ;;
-        \? ) 
-            usage
-            ;;
-    esac
-done
-shift $((OPTIND -1))
+    clone "${URL}" "${TEMP}/${URL}" || fail
 
-clone "${URL}" "${TEMP}/${URL}" || fail
+    target="${PWD}/$(dirname "${URL}")"
+    install -d "${target}" || fail
+    mv "${TEMP}/${URL}" "${target}" || fail
+}
 
-target="${PWD}/$(dirname "${URL}")"
-install -d "${target}" || fail
-mv "${TEMP}/${URL}" "${target}" || fail
+main "${@}"