sublimino
7/18/2018 - 1:34 PM

clone.sh

clone () { 
    local SOURCE="${1}";
    local REPO=$(echo "${1//*\//}" | sed -E 's#(\.git|/)$##');
    local TARGET_PARENT_DIR="${2:-.}";
    local TARGET_DIR="${TARGET_PARENT_DIR}/${REPO}";
    local CURRENT_BRANCH DOMAIN;
    if [[ $(echo "${SOURCE}" | grep -o '[:@/]*' | wc -l) -lt 2 ]]; then
        printf "No domain for ${SOURCE}. Iterating providers...\n\n";
        for DOMAIN in git@github.com: git@bitbucket.com:;
        do
            if clone "${DOMAIN}${SOURCE}" "${TARGET_PARENT_DIR}"; then
                return 0;
            fi;
            echo;
        done;
        return 1;
    else
        printf "${SOURCE} | ";
        local CURRENT_BRANCH;
        git clone --depth 1 "${SOURCE}" "${TARGET_DIR}" && cd "${_}" && CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) && git branch --set-upstream-to=origin/"${CURRENT_BRANCH}" "${CURRENT_BRANCH}" && git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*";
        local STATUS=$?;
        [[ "${STATUS}" == 0 ]] && echo "Cloned from ${SOURCE}";
        return ${STATUS};
    fi
}

# e.g. clone controlplaneio/netassert