-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-public-branch.sh
executable file
·305 lines (244 loc) · 9.37 KB
/
sync-public-branch.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env bash
GIT_DEFAULT_BRANCH=main
GIT_PUBLIC_BRANCH=public
## ref: https://intoli.com/blog/exit-on-errors-in-bash-scripts/
# exit when any command fails
set -e
function gitcommitpush() {
local LOCAL_BRANCH="$(git symbolic-ref --short HEAD)" && \
local REMOTE_AND_BRANCH=$(git rev-parse --abbrev-ref ${LOCAL_BRANCH}@{upstream}) && \
IFS=/ read REMOTE REMOTE_BRANCH <<< ${REMOTE_AND_BRANCH} && \
echo "Staging changes:" && \
git add -A || true && \
echo "Committing changes:" && \
git commit -am "group updates to public branch" || true && \
echo "Pushing branch '${LOCAL_BRANCH}' to remote '${REMOTE}' branch '${REMOTE_BRANCH}':" && \
git push -f -u ${REMOTE} ${LOCAL_BRANCH}:${REMOTE_BRANCH} || true
}
function gitresetpublicsub() {
echo "Resetting/re-initializing submodule for public branch" && \
git submodule deinit -f . && \
rm -fr ansible && \
git submodule add -branch main --force --name ansible-github https://github.com/lj020326/ansible-datacenter.git ansible && \
git config -f .gitmodules submodule.ansible.branch main && \
echo "Pull latest changes from submodules:" && \
git submodule update --init --recursive --remote && \
gitcommitpush
}
## https://www.pixelstech.net/article/1577768087-Create-temp-file-in-Bash-using-mktemp-and-trap
TMP_DIR=$(mktemp -d -p ~)
# keep track of the last executed command
trap 'last_command=$current_command; current_command=$BASH_COMMAND' DEBUG
# echo an error message before exiting
trap 'echo "\"${last_command}\" command filed with exit code $?."' EXIT
trap 'rm -fr "$TMP_DIR"' EXIT
GIT_REMOVE_CACHED_FILES=0
CONFIRM=0
#SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCRIPT_DIR="$(dirname "$0")"
## PURPOSE RELATED VARS
#PROJECT_DIR=$( git rev-parse --show-toplevel )
PROJECT_DIR="$(cd "${SCRIPT_DIR}" && git rev-parse --show-toplevel)"
MIRROR_DIR_LIST="
.github
.jenkins
docs
inspec
templates
tools
"
PUBLIC_GITIGNORE=pub.gitignore
PUBLIC_GITMODULES=pub.gitmodules
## ref: https://stackoverflow.com/questions/53839253/how-can-i-convert-an-array-into-a-comma-separated-string
declare -a EXCLUDES_ARRAY
EXCLUDES_ARRAY+=('.git')
EXCLUDES_ARRAY+=('.gitmodule')
EXCLUDES_ARRAY+=('.idea')
EXCLUDES_ARRAY+=('.vscode')
EXCLUDES_ARRAY+=('**/.DS_Store')
EXCLUDES_ARRAY+=('venv')
EXCLUDES_ARRAY+=('*.log')
EXCLUDES_ARRAY+=('ansible')
printf -v EXCLUDES '%s,' "${EXCLUDES_ARRAY[@]}"
EXCLUDES="${EXCLUDES%,}"
## https://serverfault.com/questions/219013/showing-total-progress-in-rsync-is-it-possible
## https://www.studytonight.com/linux-guide/how-to-exclude-files-and-directory-using-rsync
RSYNC_OPTS_GIT_MIRROR=(
-dar
--links
--delete-excluded
--exclude={"${EXCLUDES}"}
)
RSYNC_OPTS_GIT_UPDATE=(
-ari
--links
)
function isInstalled() {
command -v "${1}" >/dev/null 2>&1 || return 1
}
function checkRequiredCommands() {
missingCommands=""
for currentCommand in "$@"
do
isInstalled "${currentCommand}" || missingCommands="${missingCommands} ${currentCommand}"
done
if [[ ! -z "${missingCommands}" ]]; then
fail "checkRequiredCommands(): Please install the following commands required by this script:${missingCommands}"
fi
}
function search_repo_keywords () {
local LOG_PREFIX="search_repo_keywords():"
local REPO_EXCLUDE_DIR_LIST=(".git")
REPO_EXCLUDE_DIR_LIST+=(".idea")
REPO_EXCLUDE_DIR_LIST+=("venv")
REPO_EXCLUDE_DIR_LIST+=("private")
REPO_EXCLUDE_DIR_LIST+=("save")
#export -p | sed 's/declare -x //' | sed 's/export //'
if [ -z ${REPO_EXCLUDE_KEYWORDS+x} ]; then
echo "${LOG_PREFIX} REPO_EXCLUDE_KEYWORDS not set/defined"
exit 1
fi
#echo "${LOG_PREFIX} REPO_EXCLUDE_KEYWORDS=${REPO_EXCLUDE_KEYWORDS}"
IFS=',' read -ra REPO_EXCLUDE_KEYWORDS_ARRAY <<< "$REPO_EXCLUDE_KEYWORDS"
#echo "${LOG_PREFIX} REPO_EXCLUDE_KEYWORDS_ARRAY=${REPO_EXCLUDE_KEYWORDS_ARRAY[*]}"
# ref: https://superuser.com/questions/1371834/escaping-hyphens-with-printf-in-bash
#'-e' ==> '\055e'
local GREP_DELIM=' \055e '
printf -v GREP_PATTERN_SEARCH "${GREP_DELIM}%s" "${REPO_EXCLUDE_KEYWORDS_ARRAY[@]}"
## strip prefix
local GREP_PATTERN_SEARCH=${GREP_PATTERN_SEARCH#"$GREP_DELIM"}
## strip suffix
#local GREP_PATTERN_SEARCH=${GREP_PATTERN_SEARCH%"$GREP_DELIM"}
#echo "${LOG_PREFIX} GREP_PATTERN_SEARCH=${GREP_PATTERN_SEARCH}"
local GREP_COMMAND="grep ${GREP_PATTERN_SEARCH}"
echo "${LOG_PREFIX} GREP_COMMAND=${GREP_COMMAND}"
local FIND_DELIM=' -o '
# printf -v FIND_EXCLUDE_DIRS "\055path %s${FIND_DELIM}" "${REPO_EXCLUDE_DIR_LIST[@]}"
# printf -v FIND_EXCLUDE_DIRS "! -path %s${FIND_DELIM}" "${REPO_EXCLUDE_DIR_LIST[@]}"
printf -v FIND_EXCLUDE_DIRS "\055path '*/%s/*'${FIND_DELIM}" "${REPO_EXCLUDE_DIR_LIST[@]}"
local FIND_EXCLUDE_DIRS=${FIND_EXCLUDE_DIRS%$FIND_DELIM}
#echo "${LOG_PREFIX} FIND_EXCLUDE_DIRS=${FIND_EXCLUDE_DIRS}"
## this works:
## find . \( -path '*/.git/*' \) -prune -name '.*' -o -exec grep -i example {} 2>/dev/null +
## ref: https://stackoverflow.com/questions/6565471/how-can-i-exclude-directories-from-grep-r#8692318
## ref: https://unix.stackexchange.com/questions/342008/find-and-echo-file-names-only-with-pattern-found
# local FIND_CMD="find ${PROJECT_DIR}/ -type f \( ${FIND_EXCLUDE_DIRS} \) -prune -o -exec ${GREP_COMMAND} {} 2>/dev/null \;"
# local FIND_CMD="find ${PROJECT_DIR}/ -name '.*' -type f \( ${FIND_EXCLUDE_DIRS} \) -prune -o -exec ${GREP_COMMAND} {} 2>/dev/null +"
local FIND_CMD="find ${PROJECT_DIR}/ \( ${FIND_EXCLUDE_DIRS} \) -prune -name '.*' -o -exec ${GREP_COMMAND} {} 2>/dev/null +"
echo "${LOG_PREFIX} ${FIND_CMD}"
local EXCEPTION_COUNT=$(eval "${FIND_CMD} | wc -l")
if [[ $EXCEPTION_COUNT -eq 0 ]]; then
echo "${LOG_PREFIX} SUCCESS => No exclusion keyword matches found!!"
else
echo "${LOG_PREFIX} There are [${EXCEPTION_COUNT}] exclusion keyword matches found:"
eval "${FIND_CMD}"
exit 1
fi
return "${EXCEPTION_COUNT}"
}
function sync_public_branch() {
git fetch --all
git checkout ${GIT_DEFAULT_BRANCH}
#RSYNC_OPTS=${RSYNC_OPTS_GIT_MIRROR[@]}
echo "copy project to temporary dir $TMP_DIR"
#local RSYNC_CMD="rsync ${RSYNC_OPTS} ${PROJECT_DIR}/ ${TMP_DIR}/"
local RSYNC_CMD="rsync ${RSYNC_OPTS_GIT_MIRROR[*]} ${PROJECT_DIR}/ ${TMP_DIR}/"
echo "${RSYNC_CMD}"
eval $RSYNC_CMD
echo "Checkout public branch"
git checkout ${GIT_PUBLIC_BRANCH}
if [ $GIT_REMOVE_CACHED_FILES -eq 1 ]; then
echo "Removing files cached in git"
git rm -r --cached .
fi
#echo "Removing existing non-dot files for clean sync"
#rm -fr *
echo "Copy ${TMP_DIR} to project dir $PROJECT_DIR"
#echo "rsync ${RSYNC_OPTS_GIT_UPDATE[@]} ${TMP_DIR}/ ${PROJECT_DIR}/"
RSYNC_CMD="rsync ${RSYNC_OPTS_GIT_UPDATE[*]} ${TMP_DIR}/ ${PROJECT_DIR}/"
echo "${RSYNC_CMD}"
eval ${RSYNC_CMD}
IFS=$'\n'
for dir in ${MIRROR_DIR_LIST}
do
echo "Mirror ${TMP_DIR}/${dir}/ to project dir $PROJECT_DIR/${dir}/"
# RSYNC_CMD="rsync ${RSYNC_OPTS_GIT_UPDATE[*]} --delete --update --exclude=save ${TMP_DIR}/${dir}/ ${PROJECT_DIR}/${dir}/"
RSYNC_CMD="rsync ${RSYNC_OPTS_GIT_MIRROR[*]} ${TMP_DIR}/${dir}/ ${PROJECT_DIR}/${dir}/"
echo "${RSYNC_CMD}"
eval ${RSYNC_CMD}
done
if [ -e $PUBLIC_GITIGNORE ]; then
echo "Update public files:"
cp -p $PUBLIC_GITIGNORE .gitignore
fi
if [ -e $PUBLIC_GITMODULES ]; then
echo "Update public submodules:"
cp -p $PUBLIC_GITMODULES .gitmodules
git submodule deinit -f . && \
git submodule update --init --recursive --remote
fi
echo "Show changes before push:"
git status
## https://stackoverflow.com/questions/5989592/git-cannot-checkout-branch-error-pathspec-did-not-match-any-files-kn
## git diff --name-only public ${GIT_DEFAULT_BRANCH} --
if [ $CONFIRM -eq 0 ]; then
## https://www.shellhacks.com/yes-no-bash-script-prompt-confirmation/
read -p "Are you sure you want to merge the changes above to public branch ${TARGET_BRANCH}? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
exit 1
fi
fi
## https://stackoverflow.com/questions/5738797/how-can-i-push-a-local-git-branch-to-a-remote-with-a-different-name-easily
echo "Add all the files:"
gitcommitpush
echo "Checkout ${GIT_DEFAULT_BRANCH} branch:" && \
git checkout ${GIT_DEFAULT_BRANCH}
echo "Resetting ansible submodule for private"
git submodule deinit -f . && \
git submodule update --init --recursive --remote && \
gitcommitpush
cd ${PROJECT_DIR}
chmod +x *.sh
}
function usage() {
echo "Usage: ${0} [options]"
echo ""
echo " Options:"
echo " -L [ERROR|WARN|INFO|TRACE|DEBUG] : run with specified log level (default INFO)"
echo " -v : show script version"
echo " -h : help"
echo " [TEST_CASES]"
echo ""
echo " Examples:"
echo " ${0} "
echo " ${0} -L DEBUG"
echo " ${0} -v"
[ -z "$1" ] || exit "$1"
}
function main() {
checkRequiredCommands rsync
while getopts "L:vh" opt; do
case "${opt}" in
L) setLogLevel "${OPTARG}" ;;
v) echo "${VERSION}" && exit ;;
h) usage 1 ;;
\?) usage 2 ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
echo "EXCLUDES=${EXCLUDES}"
echo "PROJECT_DIR=${PROJECT_DIR}"
echo "TMP_DIR=${TMP_DIR}"
# search_repo_keywords
eval search_repo_keywords
local RETURN_STATUS=$?
if [[ $RETURN_STATUS -ne 0 ]]; then
echo "${LOG_PREFIX} search_repo_keywords: FAILED"
exit ${RETURN_STATUS}
fi
sync_public_branch
}
main "$@"