Skip to content

Instantly share code, notes, and snippets.

@npodonnell
Last active February 3, 2021 23:27
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 npodonnell/5f2c57c0755349a97fc6e81c55698588 to your computer and use it in GitHub Desktop.
Save npodonnell/5f2c57c0755349a97fc6e81c55698588 to your computer and use it in GitHub Desktop.
Sync Gists
#!/usr/bin/env bash
#
# sync-gists.sh
#
# Allows github gists to be edited locally or on gist.github.com and syncs changes between them.
# Currently gist SHA-1 hashes must be manually added to a file called gist_ids. An SSH key should
# be configured on github.com for ease of use.
#
# Format of $GIST_FILE file is one SHA-1 hash per line.
#
# N. P. O'Donnell, 2021
GIST_FILE=gist_ids
# Add, commit, pull, and push.
temp_gist_file=$(mktemp)
gists=$(for e in $(ls -1); do [[ -d $e && -d $e/.git ]] && echo -n "$e "; done;)
cp $GIST_FILE $temp_gist_file
echo $gists | sed -e 's| |\n|g' >> $temp_gist_file
for gist_id in $(cat $temp_gist_file | sort | uniq); do
if [ -d $gist_id ]
then
echo "Pulling, committing and pushing $gist_id..."
git -C $gist_id add . \
&& git -C $gist_id commit --allow-empty-message -m ""
git -C $gist_id pull --rebase
if [ $? == "1" ]
then
echo "*** PLEASE FIX MERGE CONFLICTS AND RERUN SCRIPT ***"
echo "Might be a good idea to cd into $gist_id"
exit 1
fi
git -C $gist_id push
else
echo "Cloning $gist_id..."
git clone git@gist.github.com:$gist_id.git
fi
done
# Update gist file.
> $temp_gist_file
for e in $(ls -1); do
[[ -d $e && -d $e/.git ]] && echo $e >> $temp_gist_file
done
mv $temp_gist_file $GIST_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment