]> git.nbdom.net Git - nb.git/commitdiff
git config --local remote.origin.url generate an error when not the same user
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 17 Apr 2023 14:54:35 +0000 (16:54 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Mon, 17 Apr 2023 14:54:35 +0000 (16:54 +0200)
bin/nb-update
lib/nb-update/repo.sh

index be6d523d23c6a034bae7c7d7d2218251b3af098b..836853a9d5c11858303b2d5228590015bd1b380b 100755 (executable)
@@ -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
index 6d0a4feb80c5cdb7361c8dbc83cf0042602130e4..edf13ee73478fb66cb44a1ec969e80104f3c261f 100644 (file)
@@ -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"