-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup-gh-pages
120 lines (94 loc) · 2.77 KB
/
setup-gh-pages
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
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#
# setup-gh-pages
# John Simpson <[email protected]> 2022-06-02
#
# Add a 'gh-pages' branch to the repo
#
# Last update: 2022-11-14 jms1
# - Make sure we're running git v2.0.7 or higher.
# - If a 'book/' directory already exists (i.e. if 'mdbook' has already
# generated HTML files at least once), copy that instead of creating the
# simple 'index.html' file in the new branch.
set -e
########################################
# Make sure git is version 2.0.7 or higher
REQD="2.0.7"
GITV="$( git --version | sed 's/[^0-9\.]//g' )"
XMIN="$( echo -e "$GITV\n$REQD" | sort -V | head -1 )"
if [[ "$XMIN" != "$REQD" ]]
then
echo "ERROR: this requires git version $REQD, you have $GITV"
exit 1
fi
########################################
# Make sure we don't already have a 'gh-pages' branch
if [[ -n "$( git branch -a | grep gh-pages )" ]]
then
echo "ERROR: this repo already appears to have a 'gh-pages' branch"
exit 1
fi
########################################
# Remember what directory the primary branch is checked out in, so we can
# check and possibly copy the book/ directory.
MAIN="$( pwd )"
########################################
# Create a work directory
WORK="$( mktemp -d )"
########################################
# Add a new working directory for the repo
# - the 'git worktree' commands were added in git v2.0.7
git worktree add --detach "$WORK"
########################################
# In the new working directory, start a new 'gh-pages' branch.
pushd "$WORK"
git checkout --orphan gh-pages
git rm -rf * .gitignore
########################################
# Populate the new directory.
# - If 'mdbook' has already generated the book at least once, copy that.
# - If not, create a simple "Coming soon..." page.
if [[ -f "$MAIN/book/index.html" ]]
then
FULL=true
cp -rv "$MAIN/book/" ./
else
FULL=false
touch .nojekyll
cat > index.html <<EOF
<html>
<head>
<title>Coming soon...</title>
</head>
<body>
<h1>Coming soon...</h1>
<p>This is a placeholder page, so you can tell that GitHub Pages is working.
This will be replaced with the actual book once you run "<tt>make push</tt>"
for the first time.</p>
</body>
</html>
EOF
fi
########################################
# Commit the new files.
git add .
git commit -m 'Started gh-pages branch'
git tag -m 'Initial commit on gh-pages branch' gh-pages-initial
########################################
# Done, remove the temp directory.
popd
git worktree remove "$WORK"
########################################
# Tell the user what's going on
if $FULL
then
cat <<EOF
The 'gh-pages' branch now exists on this machine, containing a copy of the
book's most recently rendered content.
EOF
else
cat <<EOF
The 'gh-pages' branch now exists on this machine, containing a simple
"Coming soon" page.
EOF
fi