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
|
2022-06-09 21:18:38 +02:00
|
|
|
mkdir -p "./$WORKSPACE_DIR"
|
2022-06-02 16:09:43 +02:00
|
|
|
# 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
|
|
|
|
log_debug "$func:${LINENO} cloning ${1} done"
|
2022-06-12 13:10:16 +02:00
|
|
|
GIT_CLONE_EXECUTED=1
|
2023-03-31 09:05:21 +02:00
|
|
|
rm -f $GIT_PULL_TMP
|
|
|
|
rm -f $OPKG_CMDS_TMP
|
2022-06-09 18:34:31 +02:00
|
|
|
# after cloning, cd into repository, and re-initialize,
|
|
|
|
# setting the work-tree as "/". This has the effect that
|
|
|
|
# a "git pull" will automatically fetched files in the
|
|
|
|
# corresponding sytem-folders.
|
|
|
|
if cd ${CUSTOMER_ID_BASE_DIR}; then
|
2022-06-25 21:26:20 +02:00
|
|
|
# configure hooks directory
|
|
|
|
exec_git_command git config core.hooksPath .githooks
|
2022-06-12 13:10:16 +02:00
|
|
|
exec_git_command git checkout "$LOCAL_BRANCH"
|
2022-06-09 21:18:38 +02:00
|
|
|
if [[ $? -eq 0 ]]; then
|
2023-03-30 13:59:38 +02:00
|
|
|
log_debug "$func:${LINENO} checked out local branch $LOCAL_BRANCH"
|
|
|
|
exec_git_command git config core.worktree "/"
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
log_debug "$func:${LINENO} configured worktree"
|
|
|
|
exec_git_command git fetch --all
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
log_debug "$func:${LINENO} fetch repository"
|
|
|
|
exec_git_command git reset --hard "$LOCAL_BRANCH"
|
|
|
|
if [[ $? -eq 0 ]]; then
|
|
|
|
log_debug "$func:${LINENO} reset --hard $LOCAL_BRANCH"
|
2022-06-25 21:26:20 +02:00
|
|
|
CLONE_CUSTOMER_REPOSITORY=true
|
|
|
|
log_debug "$func:${LINENO} re-init of ${1} done"
|
|
|
|
cd_home; return 0
|
|
|
|
fi
|
|
|
|
fi
|
2022-06-09 18:34:31 +02:00
|
|
|
fi
|
2022-06-25 21:26:20 +02:00
|
|
|
fi
|
2022-06-06 20:05:05 +02:00
|
|
|
fi
|
2022-06-03 20:45:09 +02:00
|
|
|
fi
|
2022-06-09 18:34:31 +02:00
|
|
|
cd_home; return 1
|
2022-06-03 20:45:09 +02:00
|
|
|
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
|
2022-06-06 20:05:05 +02:00
|
|
|
cd "$WORKING_DIRECTORY"
|
2022-06-03 21:46:11 +02:00
|
|
|
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
|
|
|
|
|
2022-06-09 18:34:31 +02:00
|
|
|
#local commit_before_pull=$(latest_commit)
|
|
|
|
#if [ -z $commit_before_pull ]; then
|
|
|
|
# log_warn "$func:${LINENO}: commit_before_pull empty"
|
|
|
|
# cd_home ; return 1
|
|
|
|
#fi
|
|
|
|
#log_debug "$func:${LINENO}: commit_before_pull=$commit_before_pull"
|
2022-06-02 16:09:43 +02:00
|
|
|
|
2022-06-09 18:34:31 +02:00
|
|
|
rm -f "$OPKG_CMDS_TMP"
|
|
|
|
rm -f "$GIT_PULL_TMP"
|
2022-06-02 16:09:43 +02:00
|
|
|
|
2022-06-09 18:34:31 +02:00
|
|
|
log_debug "$func:${LINENO}: executing 'git pull'..."
|
|
|
|
exec_git_command 'git pull'
|
2022-06-02 16:09:43 +02:00
|
|
|
|
2022-06-09 18:34:31 +02:00
|
|
|
# GIT_PULL_TMP created by hook post-merge. it contains the names of the
|
|
|
|
# changed files.
|
|
|
|
if [[ -f $GIT_PULL_TMP ]]; then
|
|
|
|
cd_home; return 0
|
2022-06-02 16:09:43 +02:00
|
|
|
fi
|
|
|
|
|
2022-06-09 18:34:31 +02:00
|
|
|
log_warn "$func:${LINENO}: no data fetched form repository"
|
|
|
|
cd_home; return 1
|
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-25 17:18:13 +02:00
|
|
|
|
|
|
|
get_blob () { # get the blob of the file(name) passed as $1
|
|
|
|
# note: this can be used for any file in the filesystem
|
|
|
|
echo $(git hash-object $1)
|
|
|
|
}
|
|
|
|
|
|
|
|
get_commit_for_blob () {
|
|
|
|
blob=$(get_blob $1)
|
|
|
|
if [ ! -z $blob ]; then
|
|
|
|
# search for the blob in all commits for the file(name) $1
|
|
|
|
echo $(echo $(git log --all --pretty=format:%H -- $1) |
|
|
|
|
xargs -I{} sh -c "git ls-tree {} -- $1 |
|
|
|
|
grep -q $blob && echo {}")
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
|