Skip to content

Instantly share code, notes, and snippets.

@strazto
Last active June 16, 2021 06:35
Show Gist options
  • Save strazto/bd625510310b5397c0fb97c65457891a to your computer and use it in GitHub Desktop.
Save strazto/bd625510310b5397c0fb97c65457891a to your computer and use it in GitHub Desktop.
bash script for pushing a directory in your repo to the gh-pages branch, and for initializing orphaned gh-pages branches
#!/usr/bin/env bash
# Copyright (c) 2021 Matthew Strasiotto
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
function ascend_to_root {
local top_level="$(pwd)"
if top_level="$(git rev-parse --show-toplevel 2>/dev/null)" ; then
cd "$top_level"
return 0
else
echo "not in a repo"
return 1
fi
}
function gh-pages_submodule_exists {
if [ "$#" -ne "1" ]; then
echo "gh-pages_submodule_exists takes path to submodule as arg"
return 2
fi
local status=$(git submodule status "$1" | grep '(heads/gh-pages)')
if [ -z "$status" ]; then
return 1
fi
return 0
}
function deploy_site {
local submodule_path="${1:-./docs}"
ascend_to_root
if ! gh-pages_submodule_exists; then
echo "no gh-pages submodule found!"
echo "try calling: scriptname init_submodules"
return 2
fi
local current_sha="$(git log -1 --format='%h')"
local current_branch="$(git rev-parse --abbrev-ref HEAD)"
local commit_msg=""
read -d '' commit_msg << EOF
Publish from $current_branch @ $current_sha on $(date)
EOF
cd "$submodule_path"
git add --all
git commit -m "$commit_msg"
git push origin HEAD:gh-pages
}
function get_current_origin {
git remote -v | \
grep "(push)" | \
cut -f2 | cut -f1 -d' '
}
function gh-pages_branch_init {
if git ls-remote --quiet --exit-code --heads origin gh-pages; then
echo "gh-pages branch found, will try to use that"
return 0
fi
echo "initializing orphaned gh-pages branch"
if ! git diff-index --quiet HEAD --; then
echo "Please stash or discard changes in working tree before trying to initialize empty gh-pages branch"
exit 3
fi
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Initialize gh-pages"
git push -u origin gh-pages
git checkout "$previous_branch"
}
function init_submodules {
ascend_to_root
local status="$(git submodule status)"
local submodule_path="${1:-./docs}"
local previous_branch="$(git branch --show-current)"
local origin="$(get_current_origin)"
if ! gh-pages_submodule_exists; then
echo "No submodule exists"
echo "Will attempt to init submodule at: origin@gh-pages"
gh-pages_branch_init
git submodule add --branch gh-pages -- "$origin" "$submodule_path"
fi
git submodule update --init --recursive
}
command="${1:-deploy_site}"
shift
($command $@)
@strazto
Copy link
Author

strazto commented Jun 16, 2021

Usage:

./gh-pages-submodule-deploy init_submodule <optional path, default: ./docs>
# will make a submodule at repo-root/<path>

./gh-pages-submodule-deploy deploy-site <optional path, default: ./docs>
# will push whatever is in <path> to gh-pages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment