Skip to content

Commit 6d3869d

Browse files
authored
🚸🐛 addressing various issues with commiting changes (#33)
🐛 also touched on bugs dealing with improperly set arguments 📝 added some more status messages to the script for logs
1 parent 3b5ef81 commit 6d3869d

File tree

1 file changed

+48
-21
lines changed

1 file changed

+48
-21
lines changed

wp-git-sync.sh

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#!/bin/bash
22

33
# WordPress SSH Git CI Script
4-
# v1.1.0
4+
# v1.0.2
55

66
# Set argument parameters on start
77
BRANCH=live
88
DEVBRANCH=dev
99
STAGEBRANCH=stage
1010
PROJDIR=wordpress
11-
SOFTERROR=0
1211
ERRORMESSAGES=""
12+
TRACKEDONLY="false"
13+
COMMIT="true"
1314
while :; do
1415
case "$1" in
1516

@@ -22,11 +23,11 @@ while :; do
2223
fi
2324
;;
2425
-c|--commit) #optional -- default value is true
25-
if [[ $2 ]] && [[ $2 = "no" || $2 = "n" ]] ; then
26-
COMMIT=0
26+
if [[ $2 = "no" || $2 = "n" ]] ; then
27+
COMMIT="false"
2728
shift
2829
else
29-
COMMIT=1
30+
COMMIT="true"
3031
shift
3132
fi
3233
;;
@@ -87,7 +88,7 @@ while :; do
8788
fi
8889
;;
8990
-se|--softerror) #optional
90-
SOFTERROR=1
91+
SOFTERROR="true"
9192
shift
9293
;;
9394
-t|--token) #semi-optional -- if empty ORIGIN string is required
@@ -98,6 +99,10 @@ while :; do
9899
ERRORMESSAGES="ERROR: '-t | --token' requires a non-empty option argument."
99100
fi
100101
;;
102+
-tr|--tracked) #optional
103+
TRACKEDONLY="true"
104+
shift
105+
;;
101106
-u|--tokenuser) #semi-optional -- if empty ORIGIN string is required
102107
if [[ $2 ]]; then
103108
TOKENUSER=$2
@@ -135,17 +140,17 @@ add_or_remove_devops() {
135140
output=$(git remote )
136141
case $output in
137142
*+devops*)
138-
remoteAdded=1;;
143+
remoteAdded="true";;
139144
*)
140-
remoteAdded=0;;
145+
remoteAdded="false";;
141146
esac
142147
case $1 in
143148
add)
144-
if [ $remoteAdded -eq 0 ] ; then
149+
if [ $remoteAdded == "false" ] ; then
145150
add_devops_remote
146151
fi;;
147152
rm)
148-
if [ $remoteAdded -eq 0 ] ; then
153+
if [ $remoteAdded == "true" ] ; then
149154
rm_devops_remote
150155
fi;;
151156
esac
@@ -182,7 +187,7 @@ is_stashes() {
182187

183188
git_stash() {
184189
is_stashes "check"
185-
if [[ $stashes -eq 1 ]] ; then
190+
if [[ $stashes == "true" ]] ; then
186191
case $1 in
187192
clear)
188193
git stash clear 2>&1;;
@@ -200,7 +205,7 @@ git_stash() {
200205
## Committing changes
201206

202207
commit_git() {
203-
if [[ $COMMIT -eq 1 ]] ; then
208+
if [[ $COMMIT == "commit" ]] ; then
204209
git add -A . 2>&1
205210
git commit -m "$MESSAGE" 2>&1
206211
print_status_msg "Commited on $BRANCH with message:
@@ -212,9 +217,22 @@ commit_git() {
212217

213218
## Pull branch from remote
214219

220+
create_or_rm_branch() {
221+
case $1 in
222+
add)
223+
if [[ $remoteAdded == "true" ]] ; then
224+
add_devops_remote
225+
fi;;
226+
rm)
227+
if [[ $remoteAdded == "true" ]] ; then
228+
rm_devops_remote
229+
fi;;
230+
esac
231+
}
232+
215233
pull_branch() {
216234
msg="Pull branch "
217-
if [ -z "$2" ] ; then
235+
if [[ -z $2 ]] ; then
218236
git pull "$1" 2>&1
219237
msg=$msg$1
220238
else
@@ -260,7 +278,7 @@ set_defaults() {
260278
throw_errors
261279

262280
# Set origin url to use
263-
if [ -z "$ORIGIN" ] ; then
281+
if [[ -z $ORIGIN ]] ; then
264282
# if origin isn't set and the other arguments needed to make an origin aren't set, throw an error!
265283
if [[ -z $TOKENUSER || -z $TOKEN || -z $GITREPO ]] ; then
266284
error3
@@ -276,8 +294,14 @@ set_defaults() {
276294
fi
277295

278296
# Set if branch has changes; and if so, how to handle those changes
279-
if output=$(git status --untracked-files=no --porcelain) && [ -z "$output" ] ; then
280-
DIRTYBRANCH=0
297+
if [[ $TRACKEDONLY == "true" ]] ; then
298+
output=$(git status --untracked-files=no --porcelain)
299+
else
300+
output=$(git status --porcelain)
301+
fi
302+
303+
if [ -z "$output" ] ; then
304+
DIRTYBRANCH="false"
281305
else
282306
if [[ $ONCHANGE = "Stop" || $ONCHANGE = "stop" ]] ; then
283307
error3
@@ -287,7 +311,7 @@ set_defaults() {
287311
error3
288312
else
289313
ONCHANGE="commit"
290-
DIRTYBRANCH=1
314+
DIRTYBRANCH="true"
291315
fi
292316
fi
293317
fi
@@ -296,6 +320,7 @@ set_defaults() {
296320
# Method models
297321

298322
get_new_release() {
323+
print_status_msg "Getting latest release now"
299324
# Checkout staging release branch
300325
checkout_branch "$STAGEBRANCH"
301326

@@ -314,25 +339,27 @@ get_new_release() {
314339

315340
clean_repository() {
316341
# Stash changes
342+
print_status_msg "Stashing changes"
317343
git_stash "clear"
318344
git_stash "u"
319345

320346
# Get latest release from Azure DevOps
321347
get_new_release
322348

323349
# Add changes back
350+
print_status_msg "Finished updating, adding changes back"
324351
git_stash "pop"
325352

326353
# If soft error isn't enabled, add files and commit them
327-
if [ $SOFTERROR -eq 0 ] ; then
354+
if [ $SOFTERROR == "false" ] ; then
328355
commit_git
356+
# Push changes back to dev branch
357+
push_branch "devops" "$DEVBRANCH"
329358
else
330359
print_error_msg "Changes on server, soft error selected. Release is pulled, changes are popped back, branch is waiting user intervention."
331360
error3
332361
fi
333362

334-
# Push changes back to dev branch
335-
push_branch "devops" "$DEVBRANCH"
336363

337364
print_status_msg "Changes on the WordPress site have been commited, and were pushed to $DEVBRANCH branch"
338365
}
@@ -363,7 +390,7 @@ add_or_remove_devops "add"
363390

364391
# Option selected to just query for changes
365392

366-
if [ $DIRTYBRANCH -eq 0 ] ; then
393+
if [[ $DIRTYBRANCH == "true" ]] ; then
367394
# Working directory clean
368395
print_status_msg "Working directory clean"
369396
get_new_release

0 commit comments

Comments
 (0)