Skip to content

Instantly share code, notes, and snippets.

@arrjay
Created January 12, 2024 20:57
Show Gist options
  • Save arrjay/e9a942fe58835d4706f21247aab635ce to your computer and use it in GitHub Desktop.
Save arrjay/e9a942fe58835d4706f21247aab635ce to your computer and use it in GitHub Desktop.
gitsync.bash
#!/usr/bin/env bash
# trivially sync git + submodules
submodule="${1}"
check_sneaky_paths "${submodule}"
# jump into password store dir now and commit (or not)
cd "${PREFIX}" || exit 1
git commit -am "gitsync $(date +%s)"
# if we do _not_ supply a submodule, push/pull the main tree
# and all the submodules currently instantiated.
[ -n "${submodule:-}" ] || {
pass git pull
pass git push
pass git submodule foreach git pull
pass git submodule foreach git push
}
# if we do supply a submodule, sync or init that one.
# assume submodule +/- semantics of git 1.7 or newer, details at
# https://longair.net/blog/2010/06/02/git-submodules-explained/
[ -n "${submodule}" ] && {
# is this _really_ a submodule?
sm_status="$(pass git submodule status -- "${submodule}")"
[ -n "${sm_status:-}" ] && {
# init submodule here if needed
case "${sm_status}" in
-*) pass git submodule update --init "${submodule}" ;;
esac
}
# pop into submodule checkout at this point - NOTE: we never return ;)
cd "${PREFIX}/${submodule}" 2> /dev/null 1>&2 || exit 1
# if we're in a direct checkout, get a branch plz
git_head="$(git rev-parse --abbrev-ref HEAD)"
[ "${git_head}" == "HEAD" ] && {
git checkout master
}
# push/pull git at this point.
git pull
git push
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment