Something went wrong. Try again.
Mirroring: https://github.com/MunifTanjim/scripts.sh
Something went wrong. Try again.
scripts.sh gh-clone
1.4 kB · 77 lines
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778#!/usr/bin/env bash
declare REPO_DIR_BASE="${HOME}/Dev/github"
declare ownerdeclare repodeclare repo_dir
declare gh_flags=""declare git_flags=""
echo_err() { >&2 echo "$@"}
while (("$#")); do case "$1" in -u | --upstream-remote-name) gh_flags="${gh_flags} ${1} ${2}" shift 2 ;; --) shift 1 break ;; *) if test -z "${owner}"; then if [[ "${1}" = */* ]]; then owner="${1%%/*}" repo="${1##*/}" else owner="$(gh api graphql -f query="query{viewer{login}}" --jq ".data.viewer.login" | cat)" repo="${1}" fi elif test -z "${repo_dir}"; then repo_dir="${1}" else echo_err "unexpected argument" exit 1 fi shift 1 ;; esacdone
if test -z "${owner}"; then echo_err "missing argument: repository" exit 1fi
declare parent_ownerparent_owner="$(gh repo view "${owner}/${repo}" --json=parent --jq=.parent.owner.login)"if test -n "${parent_owner}"; then repo_dir="${parent_owner}/${repo}"fiif test -z "${repo_dir}"; then repo_dir="${owner}/${repo}"fi
repo_dir="${REPO_DIR_BASE}/${repo_dir}"
git_flags="${@}"
if ! test -d "$(dirname "${repo_dir}")"; then mkdir -p "$(dirname "${repo_dir}")"fi
if test -d "${repo_dir}"; then echo_err "${owner}/${repo} already exists!" exit 1fi
gh repo clone "${owner}/${repo}" "${repo_dir}" ${gh_flags} -- ${git_flags}
echo_errecho ${repo_dir}echo_err