UpdateController/git_helpers.sh

97 lines
2.8 KiB
Bash
Raw Normal View History

2022-06-02 15:25:07 +02:00
#!/bin/bash
2022-06-02 16:00:07 +02:00
# set -x
2022-06-05 07:28:14 +02:00
source ./log_helpers.sh
2022-06-02 16:00:07 +02:00
GIT_SSL_NO_VERIFY=true
2022-06-02 15:25:07 +02:00
commit_before_pull=""
2022-06-03 09:31:33 +02:00
# save the last commit before executing 'git pull'
2022-06-02 15:25:07 +02:00
save_commit_before_pull () {
2022-06-03 09:31:33 +02:00
# git reflog -> 46c5896 HEAD@{0}: commit: Made update_helpers.sh executable
2022-06-02 15:25:07 +02:00
commit_before_pull=$(git reflog | grep "HEAD@{0}" | cut -d" " -f1)
if ! [ -z "$commit_before_pull" ]; then
if grep -qE "^[[:xdigit:]]{6,}$" <<< $commit_before_pull; then
return 0
fi
fi
commit_before_pull=""
return 1
}
# save_commit_before_pull
2022-06-03 09:31:33 +02:00
# fallback if something went wrong: revert to last valid commit
2022-06-02 15:25:07 +02:00
revert_to_commit_before_pull () {
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
return 0
fi
fi
fi
return 1
}
# revert_to_commit_before_pull
2022-06-03 09:31:33 +02:00
# clone the customer repository in ./UpdateController/workspace.
# this is done only once.
2022-06-02 15:25:07 +02:00
clone_customer_repository () {
2022-06-05 07:28:14 +02:00
local func="${FUNCNAME[0]}"
2022-06-03 09:31:33 +02:00
current_dir=${PWD##*/}
current_dir="./${current_dir:-/}"
if [ "$current_dir" = "./UpdateController" ]; then
if ! [[ -d ./workspace ]]; then
{ mkdir -p ./workspace; }
fi
# check if the directory is empty. If so, clone the
# customer repository
if ! find ./workspace -mindepth 1 -maxdepth 1 | read; then
2022-06-05 07:28:14 +02:00
log_debug "$func: cloning ${1} ..."
2022-06-03 09:31:33 +02:00
( cd ./workspace && git clone "$1" ; )
2022-06-05 07:28:14 +02:00
if [ $? -eq 0 ]; then
log_debug "$func: cloning ${1} done"
return 0
fi
2022-06-03 09:31:33 +02:00
fi
2022-06-02 15:25:07 +02:00
fi
2022-06-05 07:28:14 +02:00
return 1
2022-06-02 15:25:07 +02:00
}
2022-06-03 09:31:33 +02:00
# clone_customer_repository https://git.mimbach49.de/GerhardHoffmann/customer_281.git
2022-06-02 16:00:07 +02:00
pull_customer_repository () {
2022-06-03 09:31:33 +02:00
# has to be called in ./UpdateController
2022-06-05 07:28:14 +02:00
local func="${FUNCNAME[0]}"
2022-06-03 09:31:33 +02:00
current_dir=${PWD##*/}
current_dir="./${current_dir:-/}"
if [ "$current_dir" = "./UpdateController" ]; then
repository_dir="./workspace/$1"
if ! [[ -d "$repository_dir" ]]; then
2022-06-05 07:28:14 +02:00
log_crit "$func: $repository_dir does not exist!"
2022-06-03 09:31:33 +02:00
return 1
fi
2022-06-05 07:28:14 +02:00
message=$(cd $repository_dir && git pull)
if [ "$message" = "Already up to date." ]; then
log_warn "$func: $repository_dir already up to date."
2022-06-03 09:31:33 +02:00
return 1
fi
2022-06-05 07:28:14 +02:00
log_info "$func: $message"
# Updating a6dd5d7..46d6b37
while IFS= read -r $line; do
echo "$line"
# sed -E 's/(Updating\s+)([[:xdigit:]]+)(..)([[:xdigit:]]+).*?/\2 \4/'
done < <(printf "%s\n" "$message")
2022-06-03 09:31:33 +02:00
else
2022-06-05 07:28:14 +02:00
log_crit "$func not called in ./UpdateController"
2022-06-02 16:00:07 +02:00
return 1
2022-06-02 15:25:07 +02:00
fi
2022-06-03 09:31:33 +02:00
2022-06-02 16:00:07 +02:00
return 0
2022-06-02 15:25:07 +02:00
}
2022-06-03 09:31:33 +02:00
# pull_customer_repository customer_281