Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Last active October 29, 2023 13:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save brennanMKE/046e83aa54415d56c1e4a59bb16eb902 to your computer and use it in GitHub Desktop.
Save brennanMKE/046e83aa54415d56c1e4a59bb16eb902 to your computer and use it in GitHub Desktop.
Clone All Gists

Clone All Gists

This is a shell script version of the Node.js version created by mbostock.

Place it in a directory in your PATH and set the permissions.

chmod u+x clone-all-gists

It will first collect all of the IDs for your Gists to Gists.txt and then create a directory named Gists and clone each Gist to that directory.

Configuration

The environment variables should be set when running this command. These can be set in the shell with ~/.zshrc. Using an access token will allow for a higher rate limit.

  • GITHUB_USERNAME
  • GITHUB_API_READ_ACCESS_TOKEN
#!/bin/sh
# clone all gists
JQ=$(which jq)
if [ -z "${JQ}" ]; then
echo "Install JQ to continue: brew install jq" && exit 1
fi
if [ -z "${GITHUB_USERNAME}" ]; then
echo "Define GITHUB_USERNAME to run" && exit 1
fi
if [ -z "${GITHUB_API_READ_ACCESS_TOKEN}" ]; then
echo "Define GITHUB_API_READ_ACCESS_TOKEN to run" && exit 1
fi
PER_PAGE=100
fetch_page() {
local PAGE=$1
curl \
-H "Accept: application/vnd.github.v3+json" \
-H "User-Agent: brennanMKE/gist-clone-all" \
-u "${GITHUB_USERNAME}:${GITHUB_API_READ_ACCESS_TOKEN}" \
"https://api.github.com/users/${GITHUB_USERNAME}/gists?page=${PAGE}&per_page=${PER_PAGE}" | \
${JQ} -r ".[].id"
}
fetch_pages() {
local OUTPUT_FILE=$1
local PAGE=1
local MAX_PAGE=100
local DONE=false
TEMP_FILE=$(mktemp -t gists)
cat /dev/null >${TEMP_FILE}
until [ $PAGE -gt $MAX_PAGE -o $DONE = true ]; do
echo "# Page ${PAGE}" >>${TEMP_FILE}
local LIST=$(fetch_page $PAGE)
local COUNT=$(echo "${LIST}" | wc -l | bc)
if [ $PER_PAGE -gt $COUNT ]; then
DONE=true
else
PAGE=$(expr $PAGE + 1)
fi
echo "${LIST}" >>"${TEMP_FILE}"
done
cat ${TEMP_FILE} | grep -v "#" | grep -v -e '^$' | uniq >$OUTPUT_FILE
rm ${TEMP_FILE}
}
clone_gist() {
local GIST_ID=$1
if [ ! -d "${GIST_ID}" ]; then
echo "Cloning ${GIST_ID}"
git clone git@gist.github.com:${GIST_ID}.git
else
echo "Gist exists: ${GIST_ID}"
cd "${GIST_ID}"
git pull
cd ..
fi
}
clone_gists() {
local INPUT_PATH=$1
local CURRENT_DIR=$(pwd)
mkdir -p Gists
cd Gists
while read -r GIST_ID; do
clone_gist "${GIST_ID}"
done <"${CURRENT_DIR}/${INPUT_PATH}"
cd ..
}
fetch_pages Gists.txt
clone_gists Gists.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment