From 7886d6d785444d54383c35fa1da7ac5b0863aece Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Tue, 14 Feb 2023 23:22:03 +0100 Subject: More robust handling of arguments. --- gh-clone.sh | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'gh-clone.sh') diff --git a/gh-clone.sh b/gh-clone.sh index 4f3fc64..1cefa32 100755 --- a/gh-clone.sh +++ b/gh-clone.sh @@ -8,15 +8,28 @@ cd /home/pymirrors/repos/ ORIG_REPO="$1" +# Argument can be both full URL as well as just owner/reponame string +if [[ "$ORIG_REPO" =~ "http" ]]; then + OR="${ORIG_REPO##*.com/}" + ORIG_REPO="${OR%%.git}" +fi + GH_TMP_DATA=$(mktemp /tmp/gh_data.XXXXXX.json) || exit 1 trap 'rm -f "${GH_TMP_DATA}"' EXIT +DIRNAME="$(basename "${ORIG_REPO}")" +NEW_REPO="${ORGANIZATION}/${DIRNAME}" + +# Repository has been already checked out +[ -d "$DIRNAME" ] && exit 0 + git clone "https://github.com/${ORIG_REPO}.git" -cd "$(basename ${ORIG_REPO})" +cd "${DIRNAME}" curl -s -u "${USERNAME}:${PASSWORD}" \ - --json "{ \"organization\": \"${ORGANIZATION}\" }" \ + --json "{ \"organization\": \"${ORGANIZATION}\" , \ + \"default_branch_only\": \"true\" }" \ "https://api.github.com/repos/${ORIG_REPO}/forks" >"${GH_TMP_DATA}" git remote add github \ "$(awk -F '"' '/clone_url/ {print $4 ; exit}' "${GH_TMP_DATA}")" -- cgit