-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzda-update
executable file
·192 lines (167 loc) · 3.83 KB
/
zda-update
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
#!/bin/bash
function exit_with_error() {
echo "$1" >&2
exit 2
}
function print_usage() {
echo "${usagestring}"
exit 0
}
progname=$(basename $0)
usagestring="usage: ${progname} -d <directory> [ -s -k <key> ]"
# Execute getopt
ARGS=$(getopt d:hk:s $*)
#Bad arguments
if [ $? -ne 0 ]
then
exit_with_error "${usagestring}"
fi
eval set -- "$ARGS"
while true
do
case "$1" in
-d|--directory)
shift
if [ -n "$1" ]
then
articles_dir=$1
shift
fi
;;
-k|--key)
shift
if [ -n "$1" ]
then
keyfile=$1
shift
fi
;;
-s|--ssh)
shift
ssh=true
;;
-h|--help)
shift
print_usage
;;
--)
shift
break
;;
esac
done
if [ -z "${articles_dir}" ]
then
exit_with_error "Please specify a directory containing Zendesk articles with the -d flag."
elif [ ! -e "${articles_dir}" ]
then
exit_with_error "${articles_dir}: no such file or directory"
elif [ ! -d "${articles_dir}" ]
then
exit_with_error "${articles_dir}: not a directory"
fi
cwd=$(pwd -P)
exitcode=0
# Add an SSH key to ssh-agent if possible and required.
if [ "${ssh}" = true ]
then
is_registered=$(ssh-add -l | grep ${keyfile})
if [[ -z "${is_registered}" ]]
then
ssh-add ${keyfile} || exit_with_error "Cannot register SSH identity with ssh-agent"
fi
fi
serverbranch="master"
for localrepo in ${articles_dir}
do
echo "Updating local repository in ${localrepo}"
cd ${localrepo}
if [[ ! -d ".git" ]]
then
echo "Warning: ${localrepo}: not a git repository. Skipping."
exitcode=1
continue
fi
git fetch --prune --all
allremotebranches=$(git branch -r | awk '{print $1}')
# Note that the sed command on a Mac (which this script is designed for)
# observes different syntax to that on Linux.
current_branch=$(git branch | grep "^*" | sed -E -e 's/^\*[[:space:]]+//')
if [[ "${current_branch}" != "${serverbranch}" ]]
then
git checkout "${serverbranch}"
if [ $? -ne 0 ]
then
echo "Warning: ${localrepo}: cannot switch to \"${serverbranch}\" branch. Skipping."
exitcode=1
continue
fi
fi
git pull
if [ $? -ne 0 ]
then
echo "Warning: ${localrepo}: cannot update \"${serverbranch}\" branch from remote. Skipping."
exitcode=1
continue
fi
for branch in $(git branch | sed -e 's/^\**\s*//')
do
if [[ "${branch}" == "${serverbranch}" ]]
then
continue
fi
remote_branch=$(git branch -vv | sed -E -e 's/^\**[[:space:]]*//' | grep "^${branch}" | awk '{print $3}' | grep "^\[.*[]:]$" | sed -e 's/^\[\([^]:]*\)[]:]$/\1/')
if [[ -z "${remote_branch}" ]]
then
echo "Warning: local branch \"${branch}\" is not tracking a remote branch. Skipping."
exitcode=1
continue
fi
remote_exists=false
for rb in ${allremotebranches}
do
if [[ "${remote_branch}" == "${rb}" ]]
then
remote_exists=true
fi
done
if [[ "${remote_exists}" == false ]]
then
echo "Warning: remote branch \"${remote_branch}\", tracked by local branch \"${branch}\", does not exist. Skipping."
exitcode=1
continue
fi
remote=$(echo ${remote_branch} | sed -e 's,/.*$,,')
actual_remote_branch=$(echo ${remote_branch} | sed -e 's,^.*/,,')
git checkout "${branch}"
if [ $? -ne 0 ]
then
echo "Warning: ${localrepo}: cannot switch to \"${branch}\" branch. Skipping."
exitcode=1
continue
fi
git merge "${serverbranch}"
if [ $? -ne 0 ]
then
echo "Warning: could not merge from \"${serverbranch}\" into \"${branch}\". Skipping."
exitcode=1
continue
fi
branch_status=$(git status | tail -n 1)
echo "${branch}: ${branch_status}"
if [[ ! "${branch_status}" =~ "nothing to commit" ]]
then
echo "Warning: local branch \"${branch}\" has uncommitted changes. Skipping."
exitcode=1
continue
fi
git push ${remote} ${actual_remote_branch}
done
git checkout ${current_branch}
done
cd ${cwd}
if [[ "${exitcode}" -gt 0 ]]
then
echo "There were some warnings. Please review the output."
fi
exit ${exitcode}