From 841e9adc504fece6a4e07385ba4a2dbeaf4405a9 Mon Sep 17 00:00:00 2001 From: Nicolas Boisselier Date: Mon, 17 Apr 2023 16:54:35 +0200 Subject: [PATCH] git config --local remote.origin.url generate an error when not the same user --- bin/nb-update | 60 ++++++++++++++++++++++++++++++++++++------- lib/nb-update/repo.sh | 2 +- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/bin/nb-update b/bin/nb-update index be6d523d..836853a9 100755 --- a/bin/nb-update +++ b/bin/nb-update @@ -35,15 +35,59 @@ export GIT_SSH="$TMP" # Repos # filter_name="$@" +exec_user() { + declare user="$1" + shift + + if [ "$USER" = "$user" ]; then + eval "$*" + elif [ "$USER" = "root" ]; then + su - $user -c "cd \"$(pwd)\" && $*" + else + echo "Wrong user current user=$USER repo user=$user" + return 1 + fi +} + +_git_name() { +# NB 17.04.23: Should replace other git_name in the future +# NB 17.04.23: git config --local remote.origin.url generate an error when not the same user + awk -F'= *' ' \ +match($0,/^\[([^]]+)\]/,m){ cat=match(m[1],/^remote .origin.$/) } \ +cat && /url =/{ \ + sub(/^[^:]+:/,"",$2); \ + sub(/^.*\//,"",$2); \ + sub(/(\/|\.git)$/,"",$2); \ + sub(/^.*\/$/,"",$2); \ + print $2; \ +} \ +' .git/config +} + for dir in $(nb_repos); do [ -d "$dir/.git" ] || continue [ ! -w "$dir/.git" ] && echo ">No Permission $dir" && echo && continue + + # + # Get user + # + user=`ls -dl "$dir/.git" | awk '{print $3}'` + if [ -z "$user" ] + then + echo "Can't read user owner for '$dir'" 1>&2 + fi + cd "$dir" || continue # name from git not from path - name=$(git_name) || continue - [ -z "$name" ] && continue + name=$(exec_user $user git_name) || continue + # NB 17.04.23 echo "$name" + if [ -z "$name" ] + then + echo "Can't read remote.origin.url from '$dir'" + continue + fi # args filter on name @@ -59,17 +103,14 @@ for dir in $(nb_repos); do fi # branch - branch=$(git_branch) || continue + branch=$(exec_user $user git_branch) || continue [ -z "$branch" ] && continue + # NB 17.04.23 echo " $branch" && continue + # preff to post report preff="git.$dir" - # - # Get user - # - user=`ls -dl "$dir/.git" | awk '{print $3}'` - # include extra scripts ! [ -r "$NB_ROOT/lib/$NAME/repo.sh" ] || . "$NB_ROOT/lib/$NAME/repo.sh" || exit @@ -94,12 +135,13 @@ for dir in $(nb_repos); do cmd="${cmd}GIT_SSH=$GIT_SSH cd \"$dir\" && git pull" [ "$CLEANUP" = "1" ] && cmd="$cmd && git gc" - if [ 1 = 1 ] && ! git_conn_check; then + if [ 1 = 1 ] && ! exec_user $user git_conn_check; then echo elif [ "$USER" = "$user" ]; then bash -c "$cmd" elif [ "$USER" = "root" ]; then su - $user -c "$cmd" + echo " $user | $cmd" && continue else echo "Wrong user current user=$USER repo user=$user" false diff --git a/lib/nb-update/repo.sh b/lib/nb-update/repo.sh index 6d0a4feb..edf13ee7 100644 --- a/lib/nb-update/repo.sh +++ b/lib/nb-update/repo.sh @@ -2,7 +2,7 @@ git_change_url() { local usage="Usage: git_change_url URL_FROM URL_TO" local ufrom=${1:?$usage}; shift local uto=${1:?$usage}; shift - [ "$(git config --get remote.origin.url)" = "$ufrom" ] || return + [ "$(exec_user $user git config --get remote.origin.url)" = "$ufrom" ] || return echo "Update git url for $ufrom to $uto" git remote set-url origin $uto nb_api_post_host_info "${preff:-git.$(pwd)}.set-url" "$uto" -- 2.47.3