Repository navigation
Expand file tree
/
Copy pathgit-update-ghpages
More file actions
executable file
·193 lines (173 loc) · 4.21 KB
/
Copy pathgit-update-ghpages
File metadata and controls
executable file
·193 lines (173 loc) · 4.21 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/env bash
set -o errexit
copy_contents() {
local source="$1"
status "Copying contents from $source"
if [[ ! "$dryrun" == "1" ]]; then
(cd "$source" >/dev/null && tar c .) | tar xv
else
_ "(cd \"$source\" >/dev/null && tar c .) | tar xv"
fi
}
# Sets git config
set_config() {
if [ -n "$GIT_NAME" ]; then _ git config user.name "$GIT_NAME"; fi
if [ -n "$GIT_EMAIL" ]; then _ git config user.email "$GIT_EMAIL"; fi
}
# Runs the deployment
run() {
if [ ! -d "$source" ]; then
echo "Source is not a directory: $source"
exit 1
fi
local tmpdir="$(mktemp -d)"
if [[ "$force" == "1" ]]; then
_ cd "$tmpdir"
_ git init
_ git checkout -b "$branch"
copy_contents "$source"
if [[ "$useenv" == "1" ]]; then set_config; fi
_ git add -A .
git_commit
git_push --force
else
_ cd "$tmpdir"
_ git clone "$repo" . -b "$branch" || ( \
_ git init && \
_ git checkout -b "$branch")
if [[ "$keep" == "0" ]]; then _ rm -rf ./*; fi
copy_contents "$source"
if [[ "$useenv" == "1" ]]; then set_config; fi
_ git add -A .
git_commit || true
git_push
fi
_ rm -rf "$tmpdir"
status_ "Done"
}
git_commit() {
if [ -z "$author" ]; then
_ git commit -m "$message"
else
_ git commit -m "$message" --author "$author"
fi
}
git_push() {
if [ -z "$GITHUB_TOKEN" ]; then
_ git push "${repo}" "$branch" "$@"
else
status "Pushing via \$GITHUB_TOKEN $@"
_ git push "https://${GITHUB_TOKEN}@github.com/${repospec}.git" "$branch" "$@" \
--quiet >/dev/null 2>&1 || \
( status_ "Failed to push"; exit 1 )
fi
}
status() {
echo -e "\n\033[34m==>\033[0;1m" "$@\033[0m"
}
status_() {
echo -e "\033[33;1m==>\033[0m" "$@"
}
_() {
echo ""
status_ "$@"
if [[ ! "$dryrun" == "1" ]]; then "$@"; fi
}
help() {
local cmd="$(basename $0)"
echo 'Usage:'
echo " $cmd <REPO> <SOURCE>"
echo ''
echo 'Parameters:'
echo " REPO repository to push to in 'user/repo' form"
echo " SOURCE path to upload to repository's gh-pages branch"
echo ''
echo 'Options:'
echo ' -h, --help show help screen'
echo ' -f, --force force push'
echo ' -n, --dry-run run in simulation mode'
echo ' -e, --use-env pick up arguments from environment variables'
echo ' -b, --branch use this branch name (default: gh-pages)'
echo ' -a, --author set the author'
echo ' -k, --keep keep existing files in the repo'
echo ''
echo 'Env var options:'
echo ' GITHUB_TOKEN if set, use this to push to the repo'
echo ''
echo 'Optional env vars:'
echo " Run with '-e' to enable the use of these variables."
echo " GIT_NAME set this as the repos user.name"
echo ' GIT_EMAIL set this as the repos user.email'
echo ' GITHUB_REPO substitute as the REPO (1st argument)'
echo ' GIT_SOURCE substitute as the SOURCE (2nd argument)'
echo ' GIT_BRANCH use this branch name (--branch)'
echo ''
echo 'Example:'
echo " $cmd rstacruz/myproject doc"
echo " # pushes './doc' into the gh-pages branch of rstacruz/myproject"
echo ''
echo " export GITHUB_REPO='xyz/abc'"
echo " export GIT_SOURCE='docs'"
echo " $cmd -e"
echo " # pushes './doc' into the gh-pages branch of xyz/abc"
}
#
# Defaults
#
force=0
dryrun=0
repospec=
source=
branch=
message="Update"
useenv=0
author=""
keep=0
#
# Parse args
#
while [[ "$1" =~ ^- && ! "$1" == '--' ]]; do case $1 in
-h | --help )
help
exit
;;
-b | --branch )
shift
branch="$1"
;;
-n | --dry-run )
dryrun=1
;;
-e | --use-env )
useenv=1
;;
-k | --keep )
keep=1
;;
-a | --author)
shift
author="$1"
;;
-f | --force )
force=1
;;
esac; shift; done
if [[ "$1" == '--' ]]; then shift; fi
if [[ "$useenv" == "1" ]] && [[ -n "$GIT_BRANCH" ]] && [[ -z "$branch" ]]; then
branch="$GIT_BRANCH"
fi
if [[ "$useenv" == "1" ]] && [[ -n "$GITHUB_REPO" ]] && [[ -n "$GIT_SOURCE" ]] && [[ -z "$2" ]]; then
repospec="$GITHUB_REPO"
source="$GIT_SOURCE"
else
repospec="$1"
source="$2"
fi
: ${branch:="gh-pages"}
if [ -z "$source" ]; then
help
exit 1
fi
source="`pwd -LP`/$source"
repo="https://github.com/${repospec}.git"
run