forked from viscid-hub/Viscid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
squash_ghpages.sh
executable file
·107 lines (87 loc) · 2.68 KB
/
squash_ghpages.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env bash
#
# It's super annoying having so many automated commits just
# to get the automatic doc generation onto gh-pages. Since
# we probably don't care about the history of these automated
# commits on the gh-pages branch, we can use this script to
# periodically squash and force push the gh-pages branch.
set -e
if [ "${1}" == "--pretend" -o "${1}" == "pretend" -o "${1}" == "-p" ]; then
pretend=1
else
pretend=0
fi
dir0="${PWD}"
sdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
root_dir="${sdir}"
ghp_dir="${root_dir}/.ghpages"
squash_sha_cache=".ghp_squash_sha"
# Important, whoever is running this script must have ssh push
# access to the repository, and ideally they should know what
# they are doing... this script will do a soft reset and ammend
# a commmit followed by a force push upstream.
repo="[email protected]:viscid-hub/Viscid-docs.git"
# make sure we're on the dev branch
cd ${root_dir}
current_branch="$(git rev-parse --abbrev-ref HEAD)"
if [ ${current_branch} != "dev" ]; then
echo "Error: Squashing gh-pages must be done on the dev branch" >&2
cd ${dir0}
exit 1
fi
# clone the gh-pages branch separately
git clone -b gh-pages ${repo} "${ghp_dir}"
cd "${ghp_dir}"
squash_sha="$(cat ../${squash_sha_cache})"
if [ "$(git rev-parse HEAD)" == "${squash_sha}" ]; then
echo "Info: gh-pages is already squashed" >&2
cd ${root_dir}
rm -rf ${ghp_dir}
cd ${dir0}
exit 0
fi
echo "Info: Soft resetting gh-pages branch" >&2
git reset --soft ${squash_sha}
echo "Info: Ammending gh-pages branch" >&2
git commit --amend -a -m "squashed docs / summaries"
new_squash_sha="$(git rev-parse HEAD)"
if [ ${pretend} -eq 0 ]; then
echo "Info: Force push gh-pages branch to origin" >&2
git push --force origin
else
git log -n 3 >> ../DEBUG_LOG.txt
fi
cd ${root_dir}
rm -rf "${ghp_dir}"
# save the current state
has_changes=$(git diff-index --quiet HEAD --; echo $?)
if [ ${has_changes} -ne 0 ]; then
echo "Info: Stashing current changes" >&2
git stash save
else
echo "Info: No current changes to stash" >&2
fi
# update && commit the squash_sha_cache
echo "${new_squash_sha}" > ${squash_sha_cache}
if [ ${pretend} -eq 0 ]; then
echo "Info: Commiting updated gh-pages sha" >&2
git add ${squash_sha_cache}
git commit -m "update reference to squashed gh-pages"
else
git reset --hard HEAD
fi
# put back previous changes
if [ ${has_changes} -ne 0 ]; then
echo "Info: Resurrecting stashed changes" >&2
git stash pop --index
if [ $? -ne 0 ]; then
echo "Warning: Your previous changes didn't apply, they should be the" >&2
echo " top-most stash." >&2
fi
fi
# return us to wherever we were before all this nonsense happened
cd "${dir0}"
exit 0
##
## EOF
##