From 9809eb029c3ca5ace3fc52a2c0c9888b0648fefe Mon Sep 17 00:00:00 2001 From: GabrielInTheWorld Date: Thu, 2 Dec 2021 12:09:56 +0100 Subject: [PATCH] Automatically checkout the latest commit for every submodule Adds a new script "services-to-master.sh" and updates the Makefile --- Makefile | 6 +---- services-to-master.sh | 62 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) create mode 100755 services-to-master.sh diff --git a/Makefile b/Makefile index 8acb01b2d..6a90df9ef 100644 --- a/Makefile +++ b/Makefile @@ -28,11 +28,7 @@ reload-proxy: docker-compose -f docker/docker-compose.dev.yml exec -w /etc/caddy proxy caddy reload services-to-master: - # Note: This script updates all submodules to upstream/master[1]. For setting the submodules to the linked - # commits use `git submodule update`. The `upstream` remote must be set up correctly to point to the main repo. - # - # [1] ...or main, or whatever branch the OS4 one is. See .gitmodules. - git submodule foreach -q --recursive 'git checkout $(git config -f $$toplevel/.gitmodules submodule.$$name.branch || echo master); git pull upstream $$(git config -f $$toplevel/.gitmodules submodule.$$name.branch || echo master)' + ./services-to-master.sh submodules-origin-to-upstream: # You may only use this one time after cloning this repository. diff --git a/services-to-master.sh b/services-to-master.sh new file mode 100755 index 000000000..1214a4a58 --- /dev/null +++ b/services-to-master.sh @@ -0,0 +1,62 @@ +# !/bin/bash + +function get_upstream_branch { + local SUBMODULE_NAME=$0 + local MEDIA_SERVICE_NAME="openslides-media-service" + # We have to treat the media-service differently to the other services + # until its "main" branch is neither master nor main + if [ "$SUBMODULE_NAME" == "$MEDIA_SERVICE_NAME" ]; then + echo "openslides4-dev" + return + fi; + + local BRANCH_NAME=master + local exists=`git show-ref refs/heads/$BRANCH_NAME` + if [[ -z $exists ]]; then + BRANCH_NAME=main + fi; + echo "$BRANCH_NAME" +} + +function get_upstream_name { + { + # try + git ls-remote --exit-code upstream 2>/dev/null && echo "upstream" + } || { + # catch + echo "origin" + } +} + +function pull_latest_commit { + local SUBMODULE_NAME=$0 + echo "Pulling latest commit for: $SUBMODULE_NAME" + echo "$SUBMODULE_NAME: Checking which branch is the upstream branch" + + local BRANCH_NAME=$(get_upstream_branch) + + echo "$SUBMODULE_NAME: Upstream branch is $BRANCH_NAME" + + git checkout $BRANCH_NAME; + + local REMOTE_NAME=$(get_upstream_name) + git pull $REMOTE_NAME $BRANCH_NAME; + + echo "$SUBMODULE_NAME: Successfully pulled latest commit" +} + +export -f pull_latest_commit +export -f get_upstream_branch +export -f get_upstream_name + +git submodule update --init +git submodule foreach -q --recursive "bash -c pull_latest_commit \$name" + +echo "Successfully pulled latest commits for every submodule!" + +# Old command, if we need to checkout another branch than master or main: +# git submodule foreach -q --recursive +# ' +# git checkout $(git config -f $$toplevel/.gitmodules submodule.$$name.branch || echo master); +# git pull upstream $$(git config -f $$toplevel/.gitmodules submodule.$$name.branch || echo master) +# ' \ No newline at end of file