UpdateController/git_helpers

243 lines
8.1 KiB
Plaintext
Raw Normal View History

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
# if [ ${git_helpers_sourced:-1} = "1" ]; then
# readonly git_helpers_sourced=${BASH_SOURCE[0]}
2022-06-02 16:09:43 +02:00
readonly repository_already_up_to_date=2
2022-06-02 16:09:43 +02:00
#
exec_git_command () {
local func="${FUNCNAME[0]}"
log_debug "$func:${LINENO} exec-ing [$*]"
local __git_result=$(exec_process_substitution $*)
log_debug "$func:${LINENO} result=$__git_result"
2022-06-02 16:09:43 +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
log_debug "$func:${LINENO} commit -> $c"
printf "%s\n" "$c"
2022-06-02 16:09:43 +02:00
else
log_crit "$func:${LINENO} wrong format for commit c=$c"
2022-06-02 16:09:43 +02:00
fi
else
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
git_branch () {
# always relative to a git repository
echo $(git branch -a | grep '*' | awk '{ print $2 }')
}
git_customer_branch () {
if cd_customer_repository; then
customer_branch=$(git_branch)
cd_home;
echo $customer_branch
fi
}
# clone the customer repository in ./workspace.
2022-06-02 16:09:43 +02:00
# this is done only once.
#
# 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]}"
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
if ! find ./$WORKSPACE_DIR -mindepth 1 -maxdepth 1 | read; then
log_debug "$func:${LINENO} cloning ${1} ..."
if cd "./$WORKSPACE_DIR"
then
exec_git_command git clone "$1"
if [ $? -eq 0 ]; then
log_debug "$func:${LINENO} cloning ${1} done"
GIT_CLONE_EXECUTED=1
# 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
# configure hooks directory
exec_git_command git config core.hooksPath .githooks
exec_git_command git checkout "$LOCAL_BRANCH"
2022-06-09 21:18:38 +02:00
if [[ $? -eq 0 ]]; then
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"
CLONE_CUSTOMER_REPOSITORY=true
log_debug "$func:${LINENO} re-init of ${1} done"
cd_home; return 0
fi
fi
fi
fi
fi
fi
cd_home; return 1
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
if ! [[ -d "./${WORKSPACE_DIR}/$CUSTOMER_ID" ]]; then
log_fatal "$func:${LINENO} $PWD $WORKSPACE_DIR/$CUSTOMER_ID"\
"wrong repository: $(ls -d './${WORKSPACE_DIR}/*')"
else
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
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]}"
# has to be called in WORKING_DIRECTORY
cd "$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
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]}"
if ! cd_customer_repository; then
2022-06-02 16:09:43 +02:00
return 1
fi
log_crit "$func:${LINENO}: executing 'git pull'..."
exec_git_command 'git pull'
2022-06-02 16:09:43 +02:00
# GIT_UPDATE_LOG updated by hook post-merge. it contains the names of the
# changed files or opkg-commands to execute.
# If 'git pull' has fetched new data, then there are lines starting with
# DOWNLOAD or EXECUTE.
grep -E -q "^[[:space:]]*(DOWNLOAD|EXECUTE)" "$GIT_UPDATE_LOG"
if [ $? -eq 0]; then
log_info "$func:${LINENO}: new data fetched from repository"
cd_home; return 0
2022-06-02 16:09:43 +02:00
fi
log_warn "$func:${LINENO}: no data fetched from repository"
cd_home; return 1
2022-06-02 16:09:43 +02:00
}
# pull_customer_repository customer_281
changed_file_names () {
2022-06-02 16:09:43 +02:00
local func="${FUNCNAME[0]}"
if cd_customer_repository ; then
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=""
for f in ${KNOWN_FILES[@]} ; do
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
log_debug "$func:${LINENO}: $f found in $git_res"
fi
done
2022-06-04 21:28:28 +02:00
cd_home; log_debug "$func:${LINENO}: file_names=$file_names" ;
printf '%s' "$file_names"
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
}