From 89ef5c69b6845623a1ae72d84f7b7ac60c369d27 Mon Sep 17 00:00:00 2001 From: Gerhard Hoffmann Date: Fri, 3 Jun 2022 20:45:09 +0200 Subject: [PATCH] Fixed exec_git_command(), clone_customer_repository(), pull_customer_repository() --- git_helpers | 91 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 57 insertions(+), 34 deletions(-) diff --git a/git_helpers b/git_helpers index 1343269..9ccbf25 100755 --- a/git_helpers +++ b/git_helpers @@ -1,17 +1,25 @@ #!/bin/bash # set -x -source ./log_helpers.sh +# if [ ${git_helpers_sourced:-1} = "1" ]; then +# readonly git_helpers_sourced=${BASH_SOURCE[0]} +#else +# return 0 +#fi -GIT_SSL_NO_VERIFY=true + +# source ./log_helpers + +readonly GIT_SSL_NO_VERIFY=true +readonly repository_already_up_to_date=2 # exec_git_command () { local func="${FUNCNAME[0]}" - log_debug "$func:${LINENO} exec-ing $1" + log_debug "$func:${LINENO} exec-ing [$*]" - exec {fd}< <($1) + exec {fd}< <($@) local ps_pid=$! # remember pid of process substitution local git_result="" @@ -35,15 +43,13 @@ latest_commit () { local c=$(git reflog | grep "HEAD@{0}" | cut -d" " -f1) if ! [ -z "$c" ]; then if grep -qE "^[[:xdigit:]]{6,}$" <<< $c; then - log_debug "$func: commit -> $c" + log_debug "$func:${LINENO} commit -> $c" printf "%s\n" $c else - log_crit "$func: wrong format for commit c=$c" - printf "" + log_crit "$func:${LINENO} wrong format for commit c=$c" fi else - log_crit "$func: 'git reflog' result empty" - printf "" + log_crit "$func:${LINENO} 'git reflog' result empty" fi } @@ -79,10 +85,22 @@ clone_customer_repository () { # check if the directory is empty. If so, clone the # customer repository if ! find ./workspace -mindepth 1 -maxdepth 1 | read; then - log_debug "$func: cloning ${1} ..." - ( cd ./workspace && git clone "$1" ; ) - if [ $? -eq 0 ]; then - log_debug "$func: cloning ${1} done" + log_debug "$func:${LINENO} cloning ${1} ..." + if { cd ./workspace ; }; then + $(exec_git_command git clone "$1") + if [ $? -eq 0 ]; then + log_debug "$func:${LINENO} cloning ${1} done" + cd - ; return 0 + fi + cd - + fi + else + # the directory is not empty, so we assume the customer-repository + # has been cloned alread + if ! [[ -d ./workspace/$customer_id ]]; then + local m="wrong repository: $(ls -d './workspace/*')" + log_fatal "$func:${LINENO} $m" + else return 0 fi fi @@ -104,7 +122,7 @@ cd_customer_repository () { return 1 fi - if ! cd $repository_dir; then + if ! { cd $repository_dir; } ; then log_crit "$func:${LINENO}: cannot change to $repository_dir!" return 1 fi @@ -136,29 +154,22 @@ pull_customer_repository () { fi log_debug "$func:${LINENO}: commit_before_pull=$commit_before_pull" - - exec {fd}< <(git pull) - local ps_pid=$! # remember pid of process substitution - - local git_result="" - while read t <&$fd; do - if ! [ -z "$t" ]; then - git_result="${git_result}$t" - fi - done - - exec {fd}>&- # close fd (i.e. process substitution) - wait $ps_pid # wait for the subshell to finish + + local git_result=$(exec_git_command 'git pull') if [ -z "$git_result" ]; then log_warn "$func:${LINENO}: git result empty" ; cd_home; return 1 fi + # see 'man -Pless\ +/parameter/pattern/string/bash' + git_result=${git_result//[$'\r\n\t']/ } + log_debug "$func:${LINENO} git-pull-result=${git_result}" - if [ "$git_result" = "Already up to date." ]; then - log_warn "$func:${LINENO}: $PWD already up to date." + if grep -qE "^Already\s+\up\s+\to\s+date.*$" <<< $git_result; then + log_warn "$func:${LINENO}: repository $PWD already up to date." + read $1 <<< 'yes' cd_home ; return 1 fi @@ -166,7 +177,7 @@ pull_customer_repository () { if [ -z $commit_after_pull ]; then cd_home ; return 1 fi - + log_debug "$func:${LINENO}: commit_after_pull=$commit_after_pull" # Note: # 'git pull' is a 'git fetch' followed by a 'git merge'. @@ -228,14 +239,26 @@ pull_customer_repository () { cd_home ; return 1 fi - return 0 + cd_home ; return 0 } # pull_customer_repository customer_281 -changed_files () { +changed_file_names () { local func="${FUNCNAME[0]}" if cd_customer_repository ; then - printf "%s\n" $(exec_git_command 'git diff --stat master@{1} master') - cd_home + 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 'update.conf' 'opkg_commands' ; do + if grep -qE ".*${f}.*?" <<< $git_res; then + file_names="$f $file_names" + fi + done + + cd_home ; printf "$file_names" + else + log_crit "$func:${LINENO}: cannot cd to $customer_repository "\ + "while in $PWD" fi }