-
Notifications
You must be signed in to change notification settings - Fork 14
/
publish.sh
executable file
·41 lines (36 loc) · 1017 Bytes
/
publish.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/bin/bash -ex
REMOTE=${REMOTE:-origin}
BRANCH=master
SITEDIR=site
COMMIT=$(git rev-parse --short HEAD)
# Verify the site is already built, and move it to a temporary
# location
if [ ! -d "$SITEDIR" ]; then
echo "$SITEDIR is missing: run 'make build' before publishing" >&2
exit 1
fi
TMPDIR=$(mktemp -d)
mv "$SITEDIR" "$TMPDIR"
# Switch to the publishing branch
git fetch -p "$REMOTE"
if git show-ref "$REMOTE/$BRANCH" > /dev/null; then
git checkout "$REMOTE/$BRANCH"
else
git checkout "$COMMIT"
fi
# Delete all files currently in the repo, and move the site back from
# the temporary location to the root directory of the repo
git ls-tree --name-only HEAD | xargs git rm -r
TMPROOT="$TMPDIR/$(basename $SITEDIR)"
for source in $(find "$TMPROOT" -type f)
do
target=${source#"${TMPROOT}/"}
mkdir -p $(dirname $target)
mv $source $target
git add $target
done
# Commit, push and clean up
git commit -m "${COMMIT} published: $(date)"
git push origin HEAD:$BRANCH
git checkout @{-1}
rm -rf "$TMPDIR"