From 8fb500ce46d9f7b19a9d81119c315c0433cf6310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sl=C3=A1vek=20Banko?= Date: Sat, 7 Jul 2018 17:25:25 +0200 Subject: Revamp switch_all_submodules_to_head_and_clean + script revised according to update_all_submodules + files .gitmodules are processed instead of call git submodule foreach + modules are initialized during processing instead of at the beginning + initialize submodules only if they have not yet been initialized + use --no-recurse-submodules at git pull for better overview of the update progress MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Slávek Banko --- switch_all_submodules_to_head_and_clean | 122 +++++++++++++++++++++----------- 1 file changed, 80 insertions(+), 42 deletions(-) diff --git a/switch_all_submodules_to_head_and_clean b/switch_all_submodules_to_head_and_clean index 3aaec01..92f715b 100755 --- a/switch_all_submodules_to_head_and_clean +++ b/switch_all_submodules_to_head_and_clean @@ -1,26 +1,37 @@ #!/bin/bash -if [[ ! -e .git ]] || +# echo in bold +echobd () { + if [ -p /dev/stdout ]; then + echo "$1" + else + echo -ne "\033[1m" + echo -n "$1" + echo -e "\033[0m" + fi +} + +# verify git directory and branch +if [[ ! -e .git ]] && [[ -z "`git rev-parse --git-dir 2>/dev/null`" ]]; then - echo "This script can only be run from a top level git directory. Exiting..." + echo "Current directory does not contain a .git folder. Exiting..." exit 1 fi branch=`git symbolic-ref -q HEAD | sed "s|^refs/heads/||"` +if [[ -z "$branch" ]]; then + branch=`git branch --contains HEAD | egrep -v "no branch|detached" | head -n1 | cut -c 3-` +fi if [[ -z "$branch" ]] || [[ -z "`git rev-parse --symbolic-full-name --remotes=\"*/$branch\"`" ]]; then echo "There is not active upstream branch. Exiting..." exit 1 fi -# check git abilities -if [[ -n "`git status --help 2>/dev/null|grep -- '--ignore-submodules'`" ]]; then - GIT_IGNORE_SUBMODULES="--ignore-submodules" -fi - -echo "Preparing $PWD for development use" +# get git user +echobd "Preparing $(git rev-parse --show-toplevel | xargs -r basename) $branch branch for development use" if [[ $1 == "" ]]; then - gituser=`sed -n "/^\[remote \"origin\"\]/,/url/s/\turl = http:\/\/\([^@]*\)@.*/\1/p" <\`git rev-parse --git-dir\`/config | grep -v "\(anonymous\|system\)"` + gituser=`git config --local remote.origin.url | sed -n "s/https\?:\/\/\([^@]*\)@.*/\1/p" | grep -v "\(anonymous\|system\)"` else gituser=$1 fi @@ -33,41 +44,68 @@ if [[ $gituser == "" ]]; then gituser="anonymous" fi -THISSCRIPT=$(readlink -f $0) - -if [[ ! -e "$THISSCRIPT" ]]; then - echo "Unable to find myself! Exiting..." - exit 1 -fi - -if [[ ! -z "`git status --porcelain $GIT_IGNORE_SUBMODULES`" ]]; then - git reset --hard HEAD - git clean -dxff +# check git abilities +if [[ -n "`git status --help 2>/dev/null|grep -- '--ignore-submodules'`" ]]; then + GIT_IGNORE_SUBMODULES="--ignore-submodules" fi -git pull --rebase -if [[ ! -z "`git status --porcelain $GIT_IGNORE_SUBMODULES`" ]]; then - git reset --hard HEAD - git clean -dxff +if [[ -n "`git pull --help |grep -- '--\[no-\]recurse-submodules'`" ]]; then + GIT_NO_RECURSE_SUBMODULES="--no-recurse-submodules" fi -if [[ -e .gitmodules ]]; then - if [[ $gituser == "anonymous" ]]; then - sed -i 's/system@//g' .gitmodules - else - sed -i "s/system@/$gituser@/g" .gitmodules - fi - REPO_URL=$(git config --get remote.origin.url |\ - sed "s|\(https\?\)://\([^@]*@\)\?\(.*\)/[^/]*$|\3|") - REPO_PROTO=$(git config --get remote.origin.url |\ - sed "s|\(https\?\)://\([^@]*@\)\?\(.*\)/[^/]*$|\1|") - REPO_MASTER=scm.trinitydesktop.org/scm/git - if [[ "$REPO_URL" != "$REPO_MASTER" ]]; then - sed -i "s#https\?://\([^@]*@\)\?$REPO_MASTER#$REPO_PROTO://\1$REPO_URL#g" .gitmodules - fi +# update module and submodules +updateModule() { + local MODULE + if [[ "$1" != "" ]]; then + MODULE=$1/ + else + MODULE="" + fi - git submodule init - git submodule update - git submodule foreach "git checkout $branch && $THISSCRIPT $gituser" + cd $PARENTDIR/$MODULE + if [[ ! -z "`git status --porcelain $GIT_IGNORE_SUBMODULES`" ]]; then + git reset --hard HEAD + git clean -dxff + fi + git checkout $branch + git pull --rebase $GIT_NO_RECURSE_SUBMODULES + if [[ ! -z "`git status --porcelain $GIT_IGNORE_SUBMODULES`" ]]; then + git reset --hard HEAD + git clean -dxff + fi - git checkout -- .gitmodules -fi + if [[ -e $PARENTDIR/$MODULE.gitmodules ]]; then + if [[ $gituser == "anonymous" ]]; then + sed -i 's/system@//g' $PARENTDIR/$MODULE.gitmodules + else + sed -i "s/system@/$gituser@/g" $PARENTDIR/$MODULE.gitmodules + fi + REPO_URL=$(git config --get remote.origin.url |\ + sed "s|\(https\?\)://\([^@]*@\)\?\(.*\)/[^/]*$|\3|") + REPO_PROTO=$(git config --get remote.origin.url |\ + sed "s|\(https\?\)://\([^@]*@\)\?\(.*\)/[^/]*$|\1|") + REPO_MASTER=scm.trinitydesktop.org/scm/git + if [[ "$REPO_URL" != "$REPO_MASTER" ]]; then + sed -i "s#https\?://\([^@]*@\)\?$REPO_MASTER#$REPO_PROTO://\1$REPO_URL#g" $PARENTDIR/$MODULE.gitmodules + fi + sed -n "s|^\[submodule \"\([^\"]*\)\"\]$|\1|p" <$PARENTDIR/$MODULE.gitmodules |\ + while read submodule; do + echobd "Attempting to switch submodule ${MODULE}${submodule}" + cd $PARENTDIR/$MODULE + if [[ -z "`git config --get submodule.$submodule.url`" ]]; then + git submodule init -- $submodule + fi + if [[ ! -e "$submodule/.git" ]]; then + git submodule update -- $submodule + fi + updateModule ${MODULE}${submodule} + done + git checkout -- $PARENTDIR/$MODULE.gitmodules + fi +} + +# Update from top module +cd `git rev-parse --show-toplevel` +PARENTDIR=$PWD +echobd "Working in $PARENTDIR" +updateModule +echobd "Done in $PARENTDIR" -- cgit v1.2.3