Skip to content

Instantly share code, notes, and snippets.

@vandycknick
Last active January 3, 2023 23:08
Show Gist options
  • Save vandycknick/706da87dcdf029988ecac6ac3af92689 to your computer and use it in GitHub Desktop.
Save vandycknick/706da87dcdf029988ecac6ac3af92689 to your computer and use it in GitHub Desktop.
A little script to sync my notebook on different devices.
#!/usr/bin/env bash
#The MIT License (MIT)
#Copyright (c) 2023 Nick Van Dyck
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without #limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following #conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO #EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR #THE USE OR OTHER DEALINGS IN THE SOFTWARE.
usage() {
echo "Usage: notes-sync [OPTION]
Pull and push updates to a notebook managed in git.
options:
-h|--help Show command line help.
-n|--notebook Path to your notebook folder
-d|--device Device name used to sync commits"
}
log-err() {
echo "$@" 1>&2;
}
notes-sync() {
notebook="$1"
device="$2"
if [ -z "$notebook" ]; then
log-err "Please provide a path to your notebook with '--notebook'."
return 1
fi
if [ -z "$device" ]; then
device="$(hostname)"
fi
pushd "$notebook" > /dev/null || return 1
echo "Starting sync..."
dirty_files=$(git status --porcelain | wc -l)
if [ "$dirty_files" -gt 0 ]; then
echo "Stashing local changes."
git add .
git stash push
fi
echo "Fetching changes from remote."
git pull --rebase --quiet
if [ "$dirty_files" -gt 0 ]; then
echo "Applying stashed changes."
git stash apply 0
echo "Pushing local changes to remote."
git add .
git commit --quiet --message="Sync ($device): $(date +"%Y-%m-%dT%H:%M:%S")"
git push
echo "Dropping last stash"
git stash drop 0
fi
echo "Done!"
popd > /dev/null || return 1
return 0
}
main() {
argc="$#"
argv=$(getopt --options n:,d:,h --longoptions notebook:,device:,help -- "$@")
result="$?" # Capture getopt exit code.
if [ "$result" -ne 0 ]; then
exit "$result"
fi
if [ "$argc" -eq 0 ]; then
usage
exit 0
fi
eval "set -- $argv"
notebook=""
device=""
help=false
while true; do
case $1 in
-h | --help) help=true ; shift ;;
-n | --notebook) notebook=$2 ; shift 2 ;;
-d | --device) device=$2 ; shift 2 ;;
--) shift; break ;;
*) log-err "Unexpected option: $1. This should not happen."; usage; exit 1 ;;
esac
done
if [ "$help" = true ]; then
usage
exit 0
fi
notes-sync "$notebook" "$device"
result="$?"
exit $result
}
[[ "${BASH_SOURCE[0]}" != "$0" ]] || main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment