Skip to content

Instantly share code, notes, and snippets.

@StefanHamminga
Last active April 24, 2016 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StefanHamminga/44233d48eeff4c510829 to your computer and use it in GitHub Desktop.
Save StefanHamminga/44233d48eeff4c510829 to your computer and use it in GitHub Desktop.
Wrapper script for GitHub Gist repositories. Allows keeping multiple Gist files in one directory and one command updates.
#!/bin/bash -e
# gist - GitHub Gist tool.
# Created by Stefan Hamminga <stefan@prjct.net>. Use any of these licenses: MIT,
# GPL v2.0 or higher, LGPL v2 or higher, or CC BY-4.0.
# Repository: https://gist.github.com/44233d48eeff4c510829.git
usage() {
echo -e "
\e[1mgist\e[0m - Wrapper script for GitHub Gist repositories. Allows keeping multiple
Gist files in one directory.
Usage:
\e[1mgist clone https://gist.github.com/my_gist_id.git\e[0m
\e[1mgist clone git@gist.github.com:my_gist_id.git\e[0m
Use either SSH or HTTP git url to clone a gist. if no '.gist' directory
exists, one will be created. Each gist Git repository is stored here,
repository files are linked to the initial directory by symlink.
\e[1mgist pull my_gist_file\e[0m
Runs a 'git pull' on the repository corresponding to the specified file.
\e[1mgist push my_gist_file [commit message]\e[0m
Commits 'my_gist_file' to the appropriate repository and pushes the
update to GitHub. If no commit message is provided an automated message
will be provided.
"
exit $1
}
if [ "$1" = "" ]; then
usage 0
fi
case "$1" in
clone)
shift
GIST_ID=`echo $1 | sed -r 's,.*[/:]([^:/]+).git,\1,'`
if [ "$GIST_ID" = "" ]; then
echo "Error parsing URL: $1"
usage 2
fi
if [ ! -d "./.gist" ]; then
mkdir .gist
fi
cd .gist
git clone $1
cd ..
find .gist/${GIST_ID}/ -maxdepth 1 -not -path '*/\.*' -type f -exec ln -s "{}" \;
;;
pull)
shift
GIST_REPO=`readlink "$1" | sed -r 's,.*(.gist/[^/]+/).*,\1,'`
if [ ! -d "${GIST_REPO}/.git" ]; then
echo "Error, unable to locate git repo at: $GIST_REPO"
usage 3
fi
git -C "${GIST_REPO}" add "${FILE}"
git -C "${GIST_REPO}" commit -m "Automated local updates"
git -C "${GIST_REPO}" pull
;;
push)
shift
GIST_REPO=`readlink "$1" | sed -r 's,.*(.gist/[^/]+/).*,\1,'`
FILE=`readlink "$1" | sed -r 's,.*/([^/]+)$,\1,'`
shift
if [ ! -d "${GIST_REPO}/.git" ]; then
echo "Error, unable to locate git repo at: $GIST_REPO"
usage 4
fi
git -C "${GIST_REPO}" add "${FILE}"
if [ "$1" != "" ]; then
git -C "${GIST_REPO}" commit -m "$*"
else
git -C "${GIST_REPO}" commit -m "Automated update"
fi
git -C "${GIST_REPO}" push
;;
*)
echo "Error, no idea what to do with: '$0 $*'"
usage 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment