Skip to content

Instantly share code, notes, and snippets.

@MoserMichael
Last active November 23, 2025 03:37
Show Gist options
  • Select an option

  • Save MoserMichael/b4fec70c95a558ac7cd7bdcdcd2c911a to your computer and use it in GitHub Desktop.

Select an option

Save MoserMichael/b4fec70c95a558ac7cd7bdcdcd2c911a to your computer and use it in GitHub Desktop.
github-tools
#!/bin/bash
GITHUB_USER_NAME=$1
if [[ "x${GITHUB_USER_NAME}" == "x" ]]; then
cat <<EOF
Usage: $0 <github-user-name>"
Check out all gists or given github user - under the current directory.
By default all public gists are checked out, if you have a ~/.github file with GITHUB_API_TOKEN=api-token-here
where the token has acces rights to the user, then the private gists are also checked out.
directory name for each local repo is the git description, if description is empty then first file name of a gist is used.
Advice: put something in the description, as you may add additional files.
EOF
exit 1
fi
set -x
if [[ -f ~/.github ]]; then
source ~/.github
fi
add_opt=""
if [[ $GITHUB_API_TOKEN != "" ]]; then
add_opt="-H 'Authorization: Bearer ${GITHUB_API_TOKEN}'"
fi
temp_file_name=$(tempfile)
bash -c "curl -H 'Accept: application/vnd.github.v3+json' ${add_opt} https://api.github.com/users/${GITHUB_USER_NAME}/gists >${temp_file_name}"
#cat "${temp_file_name}"
readarray -t pull_urls < <(cat ${temp_file_name} | jq -r '.[].git_pull_url')
readarray -t descriptions < <(cat ${temp_file_name} | jq -r '.[].description')
readarray -t first_file_name < <(cat ${temp_file_name} | jq -c '.[].files|map(.filename) | map(.) ' | jq -rc '.[]')
#printf '%s\n' "${pull_urls[@]}
#printf '%s\n' "${titles[@]}
array_length=${#pull_urls[@]} # Get the number of elements in the array
for (( i=0; i<array_length; i++ )); do
repo_name=${descriptions[$i]}
if [[ -z $repo_name ]]; then
repo_name=${first_file_name[$i]}
fi
repo_name="${repo_name// /_}"
if [[ ! -d ${repo_name} ]]; then
git clone ${pull_urls[$i]} ${repo_name}
else
echo "directory ${repo_name} already exists, already checked out"
fi
done
# function in .bashrc for pushing stuff to github
# assumes you hav the env variabls GITHUB_USER and GITHUB_TOKEN (which points to an access token)
# the repo is cloned via https.
# (and you can hand multiple functions like this, if you need to work with several accounts)
function mypush() {
url=$(git config --get remote.origin.url)
# check if it https url
echo "$url" | grep -q '^https://'
if [[ $? -eq "0" ]]; then
# does the url have login credentials?
echo "$url" | grep -q '^https://.*@'
if [ $? -eq "0" ]; then
cmd=$(echo "$url" | sed -e 's#https://.*@#https://'$GITHUB_USER':'$GITHUB_TOKEN'@#g')
else
cmd=$(echo "$url" | sed -e 's#https://#https://'$GITHUB_USER':'$GITHUB_TOKEN'@#g')
fi
set -x
git push $cmd
set +x
else
echo "works with https origin urls only"
fi
}
# when working in a mixed windows shop, you might want to ignore cr lf differences
# if doing git diff / git log -p
# a more general solution (might be a bit scary, i would be afraid of forgetting to turn this off)
git config --global core.whitespace cr-at-eol
# on a case-by-case basis:
git diff --ignore-cr-at-eol
git log -p --ignore-cr-at-eol
# you may add this to your .bashrc file,
# now I am not sure what is easier to remember - the alias or the option name.
# (or what may be considered to be more worthwhile...)
alias gitlog='git log -p --ignore-cr-at-eol '
alias gitdiff='git diff --ignore-cr-at-eol '
# -------------------------
# Git gist editor doesn't have a spell checker? No problemo
#
# Each gist is a separate git repository. You can check it out to a local directory, edit it in your favorite editor (that will naturally #have a spell checker) and push it back to the repo.
#
# Instructions for cloning the git repo of a gist can be found here: https://docs.github.com/en/get-started/writing-on-github/editing-and-sharing-content-with-gists/forking-and-cloning-gists
#
# This way you can also add additional files to gists.
#
# (this advice defeats the purpose of gists a bit, but it solves the problem)
# The following script will checkout all public gits of a given git user.
# the list of public gists is obtained via github REST api.
# if environment variable ~/.github has environment variable GITHUB_API_TOKEN, then this is used as github api token
# in this case you also get the private gists of that user. Somehow.
# (curious security concept: that you can't get the private gists, because there is no way to guess the repo name...)
#---
#!/bin/bash
GITHUB_USER_NAME=$1
if [[ "x${GITHUB_USER_NAME}" == "x" ]]; then
cat <<EOF
Usage: $0 <github-user-name>"
Check out all gists or given github user - under the current directory.
By default all public gists are checked out, if you have a ~/.github file with GITHUB_API_TOKEN=api-token-here
where the token has acces rights to the user, then the private gists are also checked out.
repo name for each local repo is the git description, if empty it is the first file name of a gist.
EOF
exit 1
fi
set -x
if [[ -f ~/.github ]]; then
source ~/.github
fi
add_opt=""
if [[ $GITHUB_API_TOKEN != "" ]]; then
add_opt="-H 'Authorization: Bearer ${GITHUB_API_TOKEN}'"
fi
temp_file_name=$(tempfile)
bash -c "curl -H 'Accept: application/vnd.github.v3+json' ${add_opt} https://api.github.com/users/${GITHUB_USER_NAME}/gists >${temp_file_name}"
#cat "${temp_file_name}"
readarray -t pull_urls < <(cat ${temp_file_name} | jq -r '.[].git_pull_url')
readarray -t descriptions < <(cat ${temp_file_name} | jq -r '.[].description')
readarray -t first_file_name < <(cat ${temp_file_name} | jq -c '.[].files|map(.filename) | map(.) ' | jq -rc '.[]')
#printf '%s\n' "${pull_urls[@]}
#printf '%s\n' "${titles[@]}
array_length=${#pull_urls[@]} # Get the number of elements in the array
for (( i=0; i<array_length; i++ )); do
repo_name=${descriptions[$i]}
if [[ -z $repo_name ]]; then
repo_name=${first_file_name[$i]}
fi
repo_name="${repo_name// /_}"
if [[ ! -d ${repo_name} ]]; then
git clone ${pull_urls[$i]} ${repo_name}
else
echo "directory ${repo_name} already exists, already checked out"
fi
done
#-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment