Created
September 5, 2024 11:30
-
-
Save adam-zethraeus/3cda8c4f8254415df825f1a539d6e6f6 to your computer and use it in GitHub Desktop.
download a folder on github given its URL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -e | |
| URL="$*" | |
| START=$(echo "$URL" | sed 's%/tree/main/%\n%g' | head -n1) | |
| END=$(echo "$URL" | sed 's%/tree/main/%\n%g' | tail -n1) | |
| FOLDER=$(echo "$END" | rev | cut -d'/' -f1 | rev) | |
| REPO=$(echo "$START" | cut -d'/' -f5) | |
| CHECKOUT="${REPO}_$(uuidgen)" | |
| function get_folder { | |
| git clone -n --depth=1 --filter=tree:0 "$START" "$CHECKOUT" &>/dev/null | |
| cd "$CHECKOUT" | |
| git sparse-checkout set --no-cone "$END" &>/dev/null | |
| git checkout &>/dev/null | |
| mv "$END" ../"${FOLDER}@$(date '+%Y-%m-%dT%H:%M:%S')" | |
| cd .. | |
| rm -rf "$CHECKOUT" | |
| return $? | |
| } | |
| function await_last { | |
| pid=$! | |
| while ps -a | awk '{print $1}' | grep -q "${pid}"; do | |
| for s in / - \\ \|; do | |
| printf "\r%s" "$s" | |
| sleep .1 | |
| done | |
| done | |
| wait ${pid} | |
| ret=$? | |
| exit ${ret} | |
| } | |
| get_folder & | |
| await_last |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment