2022-06-02 16:09:43 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# set -x
|
|
|
|
|
2022-06-04 18:13:27 +02:00
|
|
|
source ./log_helpers
|
|
|
|
source ./general_utils
|
|
|
|
|
|
|
|
|
2022-06-03 20:45:09 +02:00
|
|
|
# if [ ${git_helpers_sourced:-1} = "1" ]; then
|
|
|
|
# readonly git_helpers_sourced=${BASH_SOURCE[0]}
|
2022-06-02 16:09:43 +02:00
|
|
|
|
2022-06-03 20:45:09 +02:00
|
|
|
readonly repository_already_up_to_date=2
|
2022-06-02 16:09:43 +02:00
|
|
|
|
|
|
|
#
|
|
|
|
exec_git_command () {
|
|
|
|
local func="${FUNCNAME[0]}"
|
2022-06-03 20:45:09 +02:00
|
|
|
log_debug "$func:${LINENO} exec-ing [$*]"
|
2022-06-04 14:48:30 +02:00
|
|
|
|
|
|
|
local __git_result=$(exec_process_substitution $*)
|
|
|
|
log_debug "$func:${LINENO} result=$__git_result"
|
2022-06-02 16:09:43 +02:00
|
|
|
|
2022-06-04 14:48:30 +02:00
|
|
|
printf '%s' "$__git_result"
|
2022-06-02 16:09:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
latest_commit () {
|
|
|
|
local func="${FUNCNAME[0]}"
|
2022-06-04 18:13:27 +02:00
|
|
|
# git reflog -> 46c5896 HEAD@{0}: commit: Made update_helpers executable
|
2022-06-02 16:09:43 +02:00
|
|
|
local c=$(git reflog | grep "HEAD@{0}" | cut -d" " -f1)
|
|
|
|
if ! [ -z "$c" ]; then
|
|
|
|
if grep -qE "^[[:xdigit:]]{6,}$" <<< $c; then
|
2022-06-03 20:45:09 +02:00
|
|
|
log_debug "$func:${LINENO} commit -> $c"
|
2022-06-06 17:02:31 +02:00
|
|
|
printf "%s\n" "$c"
|
2022-06-02 16:09:43 +02:00
|
|
|
else
|
2022-06-03 20:45:09 +02:00
|
|
|
log_crit "$func:${LINENO} wrong format for commit c=$c"
|
2022-06-02 16:09:43 +02:00
|
|
|
fi
|
|
|
|
else
|
2022-06-03 20:45:09 +02:00
|
|
|
log_crit "$func:${LINENO} 'git reflog' result empty"
|
2022-06-02 16:09:43 +02:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
# fallback if something went wrong: revert to last valid commit
|
|
|
|
revert_to_commit_before_pull () {
|
|
|
|
local func="${FUNCNAME[0]}"
|
|
|
|
if ! [ -z "$commit_before_pull" ]; then
|
|
|
|
if grep -qE "^[[:xdigit:]]{6,}$" <<< $commit_before_pull; then
|
|
|
|
`git reset --hard "$commit_before_pull"`
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
log_info "$func: git reset --hard $commit_before_pull"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
log_crit "$func: 'git reset --hard $commit_before_pull' failed!"
|
|
|
|
fi
|
|
|
|
log_crit "$func: wrong format for commit_before_pull"
|
|
|
|
fi
|
|
|
|
log_crit "$func: empty commit_before_pull"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
# revert_to_commit_before_pull
|
|
|
|
|
2022-06-03 21:46:11 +02:00
|
|
|
# clone the customer repository in ./workspace.
|
2022-06-02 16:09:43 +02:00
|
|
|
# this is done only once.
|
2022-06-03 21:46:11 +02:00
|
|
|
#
|
|
|
|
# Cloning into 'customer_281'...
|
|
|
|
# remote: Enumerating objects: 1087, done.
|
|
|
|
# remote: Counting objects: 100% (1087/1087), done.
|
|
|
|
# remote: Compressing objects: 100% (946/946), done.
|
|
|
|
# remote: Total 1087 (delta 404), reused 0 (delta 0), pack-reused 0
|
|
|
|
# Receiving objects: 100% (1087/1087), 103.27 KiB | 873.00 KiB/s, done.
|
|
|
|
# Resoving deltas: 100% (410/410), done.
|
|
|
|
#
|
2022-06-02 16:09:43 +02:00
|
|
|
clone_customer_repository () {
|
|
|
|
local func="${FUNCNAME[0]}"
|
2022-06-03 21:46:11 +02:00
|
|
|
if [ "$PWD" = "$WORKING_DIRECTORY" ]; then
|
|
|
|
if ! [[ -d "./$WORKSPACE_DIR" ]]; then
|
|
|
|
{ mkdir -p ./$WORKSPACE_DIR; }
|
2022-06-02 16:09:43 +02:00
|
|
|
fi
|
|
|
|
# check if the directory is empty. If so, clone the
|
|
|
|
# customer repository
|
2022-06-03 21:46:11 +02:00
|
|
|
if ! find ./$WORKSPACE_DIR -mindepth 1 -maxdepth 1 | read; then
|
2022-06-03 20:45:09 +02:00
|
|
|
log_debug "$func:${LINENO} cloning ${1} ..."
|
2022-06-03 21:46:11 +02:00
|
|
|
if cd "./$WORKSPACE_DIR"
|
|
|
|
then
|
2022-06-03 20:45:09 +02:00
|
|
|
$(exec_git_command git clone "$1")
|
|
|
|
if [ $? -eq 0 ]; then
|
2022-06-03 21:46:11 +02:00
|
|
|
CLONE_CUSTOMER_REPOSITORY=true
|
2022-06-03 20:45:09 +02:00
|
|
|
log_debug "$func:${LINENO} cloning ${1} done"
|
|
|
|
cd - ; return 0
|
|
|
|
fi
|
|
|
|
cd -
|
|
|
|
fi
|
|
|
|
else
|
2022-06-04 18:13:27 +02:00
|
|
|
# the directory is not empty, so we assume the
|
|
|
|
# customer-repository has been cloned already
|
2022-06-03 21:46:11 +02:00
|
|
|
if ! [[ -d "./${WORKSPACE_DIR}/$CUSTOMER_ID" ]]; then
|
|
|
|
log_fatal "$func:${LINENO} $PWD $WORKSPACE_DIR/$CUSTOMER_ID"\
|
|
|
|
"wrong repository: $(ls -d './${WORKSPACE_DIR}/*')"
|
2022-06-03 20:45:09 +02:00
|
|
|
else
|
2022-06-03 21:46:11 +02:00
|
|
|
local __m="./${WORKSPACE_DIR}/$CUSTOMER_ID exists"
|
2022-06-04 18:13:27 +02:00
|
|
|
log_debug "$func:${LINENO} $__m"
|
2022-06-02 16:09:43 +02:00
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
fi
|
2022-06-04 21:41:41 +02:00
|
|
|
|
|
|
|
update_psa_clone_error # message to ISMAS
|
|
|
|
return $?
|
2022-06-02 16:09:43 +02:00
|
|
|
}
|
|
|
|
# clone_customer_repository ->
|
|
|
|
# https://git.mimbach49.de/GerhardHoffmann/customer_281.git
|
|
|
|
|
|
|
|
cd_customer_repository () {
|
|
|
|
local func="${FUNCNAME[0]}"
|
2022-06-03 21:46:11 +02:00
|
|
|
# has to be called in WORKING_DIRECTORY
|
|
|
|
if [ "$PWD" = "${WORKING_DIRECTORY}" ]; then
|
|
|
|
repository_dir="./$WORKSPACE_DIR/$CUSTOMER_ID"
|
2022-06-02 16:09:43 +02:00
|
|
|
if ! [[ -d "$repository_dir" ]]; then
|
|
|
|
log_crit "$func:${LINENO}: $repository_dir does not exist!"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2022-06-03 20:45:09 +02:00
|
|
|
if ! { cd $repository_dir; } ; then
|
2022-06-04 18:13:27 +02:00
|
|
|
log_crit "$func:${LINENO}: cannot cd to $repository_dir!"
|
2022-06-02 16:09:43 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
log_debug "$func:${LINENO}: cd to $repository_dir!"
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
cd_home () {
|
|
|
|
if cd - &>/dev/null ; then
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
pull_customer_repository () {
|
|
|
|
local func="${FUNCNAME[0]}"
|
|
|
|
|
2022-06-05 19:23:43 +02:00
|
|
|
if ! cd_customer_repository; then
|
2022-06-02 16:09:43 +02:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local commit_before_pull=$(latest_commit)
|
|
|
|
if [ -z $commit_before_pull ]; then
|
2022-06-05 19:23:43 +02:00
|
|
|
log_warn "$func:${LINENO}: commit_before_pull empty"
|
2022-06-02 16:09:43 +02:00
|
|
|
cd_home ; return 1
|
|
|
|
fi
|
|
|
|
log_debug "$func:${LINENO}: commit_before_pull=$commit_before_pull"
|
2022-06-05 19:23:43 +02:00
|
|
|
log_debug "$func:${LINENO}: executing 'git pull'..."
|
2022-06-03 22:17:27 +02:00
|
|
|
|
2022-06-03 20:45:09 +02:00
|
|
|
local git_result=$(exec_git_command 'git pull')
|
2022-06-02 16:09:43 +02:00
|
|
|
|
|
|
|
if [ -z "$git_result" ]; then
|
|
|
|
log_warn "$func:${LINENO}: git result empty" ; cd_home;
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2022-06-02 22:25:08 +02:00
|
|
|
# see https://www.gnu.org/
|
|
|
|
# software/bash/manual/html_node/Shell-Parameter-Expansion.html
|
|
|
|
# [...]
|
|
|
|
#
|
|
|
|
# ${parameter//pattern/string}
|
|
|
|
#
|
|
|
|
# If there are two slashes separating parameter and pattern (...),
|
|
|
|
# all matches of pattern are replaced with string.
|
|
|
|
#
|
|
|
|
# If the expansion of string is null, matches of pattern are deleted.
|
|
|
|
# [...]
|
2022-06-03 20:45:09 +02:00
|
|
|
git_result=${git_result//[$'\r\n\t']/ }
|
|
|
|
|
2022-06-02 16:09:43 +02:00
|
|
|
log_debug "$func:${LINENO} git-pull-result=${git_result}"
|
|
|
|
|
2022-06-02 22:25:08 +02:00
|
|
|
if grep -qE "^Already\s+\up\s+\to\s+date.*$" <<< "$git_result"; then
|
2022-06-03 20:45:09 +02:00
|
|
|
log_warn "$func:${LINENO}: repository $PWD already up to date."
|
|
|
|
read $1 <<< 'yes'
|
2022-06-02 16:09:43 +02:00
|
|
|
cd_home ; return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
local commit_after_pull=$(latest_commit)
|
|
|
|
if [ -z $commit_after_pull ]; then
|
|
|
|
cd_home ; return 1
|
|
|
|
fi
|
2022-06-03 20:45:09 +02:00
|
|
|
|
2022-06-02 16:09:43 +02:00
|
|
|
log_debug "$func:${LINENO}: commit_after_pull=$commit_after_pull"
|
|
|
|
|
|
|
|
# Note: # 'git pull' is a 'git fetch' followed by a 'git merge'.
|
|
|
|
# Here's the fetch portion:
|
|
|
|
#
|
|
|
|
# remote: Counting objects: 11, done.
|
|
|
|
# remote: Compressing objects: 100% (5/5), done.
|
|
|
|
# remote: Total 7 (delta 2), reused 0 (delta 0)
|
|
|
|
#
|
|
|
|
# At this point, you've told the remote what you want.
|
|
|
|
# It finds all the objects it needs to give you,
|
|
|
|
# compresses them for faster transfer over the network,
|
|
|
|
# and then reports what it's sending you.
|
|
|
|
#
|
|
|
|
# Unpacking objects: 100% (7/7), done.
|
|
|
|
#
|
|
|
|
# You receive the pack (set of compressed objects) and unpack it.
|
|
|
|
#
|
|
|
|
# From ssh://my.remote.host.com/~/git/myproject
|
|
|
|
# * branch master -> FETCH_HEAD
|
|
|
|
|
|
|
|
# You've fetched the branch 'master' from the given remote;
|
|
|
|
# the ref FETCH_HEAD now points to it.
|
|
|
|
# Now we move on to the merge - precisely, git will merge
|
|
|
|
# FETCH_HEAD (the remote's master branch) into your current branch
|
|
|
|
# (presumably master).
|
|
|
|
#
|
|
|
|
#######################################################################
|
|
|
|
# here starts "message"
|
|
|
|
#
|
|
|
|
# Updating 9d447d2..f74fb21
|
|
|
|
# Fast forward
|
|
|
|
#
|
|
|
|
# It turns out that you haven't diverged from the remote's master branch,
|
|
|
|
# so the merge is a fast-forward (a trivial merge where it simply moves
|
|
|
|
# you forward in the history).
|
|
|
|
#
|
|
|
|
# Git notes the original position of your master branch (9d447d2)
|
|
|
|
# and the new position (f74fb21) it's been fast-forwarded to.
|
|
|
|
# If you had diverged from the remote's master branch,
|
|
|
|
# you'd see the output of a recursive merge here - Merge made
|
|
|
|
# by recursive, possibly along with some Auto-merged <file>
|
|
|
|
# and (oh no!) merge conflicts!
|
|
|
|
#
|
|
|
|
# szeged/1/1/etc/psa_config/device.conf | 13 +++++++------
|
|
|
|
# szeged/update.conf | 2 +-
|
|
|
|
# 2 files changed, 8 insertions(+), 7 deletions(-)
|
|
|
|
#
|
|
|
|
# Finally, it shows you the diffstat between the original and post-merge
|
|
|
|
# position of your master branch;
|
|
|
|
# this is basically what you'd get from
|
|
|
|
#
|
|
|
|
# git diff --stat master@{1} master.
|
|
|
|
#
|
|
|
|
|
|
|
|
update_commit="${commit_before_pull}..${commit_after_pull}"
|
|
|
|
if ! grep -qE ".*Updating\s+${update_commit}.*?" <<< $git_result; then
|
|
|
|
log_crit "$func:${LINENO}: no $update_commit in [ $git_result ]"
|
|
|
|
cd_home ; return 1
|
|
|
|
fi
|
|
|
|
|
2022-06-03 20:45:09 +02:00
|
|
|
cd_home ; return 0
|
2022-06-02 16:09:43 +02:00
|
|
|
}
|
|
|
|
# pull_customer_repository customer_281
|
|
|
|
|
2022-06-03 20:45:09 +02:00
|
|
|
changed_file_names () {
|
2022-06-02 16:09:43 +02:00
|
|
|
local func="${FUNCNAME[0]}"
|
|
|
|
if cd_customer_repository ; then
|
2022-06-03 20:45:09 +02:00
|
|
|
local git_res=$(exec_git_command 'git diff --stat master@{1} master')
|
|
|
|
git_res=${git_res//[$'\r\n\t']/ }
|
|
|
|
log_debug "$func:${LINENO}: git_res=$git_res"
|
|
|
|
local file_names=""
|
2022-06-03 21:46:11 +02:00
|
|
|
for f in ${KNOWN_FILES[@]} ; do
|
2022-06-05 19:23:43 +02:00
|
|
|
if grep -qE "${f}" <<< "$git_res"; then
|
2022-06-04 21:28:28 +02:00
|
|
|
if ! [ -z $file_names ]; then
|
|
|
|
file_names="$f $file_names"
|
|
|
|
else
|
|
|
|
file_names="$f"
|
|
|
|
fi
|
2022-06-05 19:23:43 +02:00
|
|
|
log_debug "$func:${LINENO}: $f found in $git_res"
|
2022-06-03 20:45:09 +02:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2022-06-04 21:28:28 +02:00
|
|
|
cd_home; log_debug "$func:${LINENO}: file_names=$file_names" ;
|
|
|
|
printf '%s' "$file_names"
|
2022-06-03 20:45:09 +02:00
|
|
|
else
|
|
|
|
log_crit "$func:${LINENO}: cannot cd to $customer_repository "\
|
|
|
|
"while in $PWD"
|
2022-06-02 16:09:43 +02:00
|
|
|
fi
|
|
|
|
}
|
2022-06-04 18:13:27 +02:00
|
|
|
# fi
|