|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# change all submodules in the current project to the requested branch or tag |
| 4 | +# |
| 5 | +# optionally use the specified remote where the branch or tag are to be found |
| 6 | +# optionally fetch the specified remote, or all if none was specified |
| 7 | +# |
| 8 | +# if the specified parameters are invalid (e.g. the branch doesn't exist), |
| 9 | +# nothing is done and execution continues to the next submodule |
| 10 | + |
| 11 | +# colors |
| 12 | +NC='\033[0m' # no color |
| 13 | +BLUE='\033[0;34m' # blue |
| 14 | +GREEN='\033[0;32m' # green |
| 15 | +ORANGE='\033[0;33m' # orange |
| 16 | +RED='\033[0;31m' # red |
| 17 | + |
| 18 | +# vars |
| 19 | +BRANCH="" # branch to change to |
| 20 | +DIR="$(realpath "$(dirname "$0")")" # script path |
| 21 | +FETCH=0 # don't fetch by default |
| 22 | +REMOTE="" # remote where to look for revs |
| 23 | +TAG="" # tag to change to |
| 24 | + |
| 25 | +# helper functions |
| 26 | +_die() { |
| 27 | + printf "\n${RED}ERROR: %s${NC}\n" "$@" |
| 28 | + exit 1 |
| 29 | +} |
| 30 | + |
| 31 | +_head() { |
| 32 | + printf "${GREEN}%s${NC}\n" "$@" |
| 33 | +} |
| 34 | + |
| 35 | +_log() { |
| 36 | + printf "${BLUE}%s${NC}\n" "$@" |
| 37 | +} |
| 38 | + |
| 39 | +_war() { |
| 40 | + printf "${ORANGE}WARNING: %s${NC}\n" "$@" |
| 41 | +} |
| 42 | + |
| 43 | +# CLI handling |
| 44 | +help() { |
| 45 | + echo "$0 [-h] [-f] [-r <remote>] (-b) <branch>" |
| 46 | + echo "" |
| 47 | + echo "options:" |
| 48 | + echo " -h --help show this help message" |
| 49 | + echo " -b --branch change to the specified branch" |
| 50 | + echo " -f --fetch fetch provided remote (or all if none specified)" |
| 51 | + echo " -r --remote remote to be used" |
| 52 | + echo " -t --tag change to the specified tag" |
| 53 | +} |
| 54 | + |
| 55 | +while [ -n "$1" ]; do |
| 56 | + case $1 in |
| 57 | + -h | --help) |
| 58 | + help |
| 59 | + exit 0 |
| 60 | + ;; |
| 61 | + -b | --branch) |
| 62 | + BRANCH="$2" |
| 63 | + shift |
| 64 | + ;; |
| 65 | + -f | --fetch) |
| 66 | + FETCH=1 |
| 67 | + ;; |
| 68 | + -r | --remote) |
| 69 | + REMOTE="$2" |
| 70 | + shift |
| 71 | + ;; |
| 72 | + -t | --tag) |
| 73 | + TAG="$2" |
| 74 | + shift |
| 75 | + ;; |
| 76 | + *) |
| 77 | + help |
| 78 | + _die "unsupported argument \"$1\"" |
| 79 | + ;; |
| 80 | + esac |
| 81 | + shift |
| 82 | +done |
| 83 | + |
| 84 | +# check a branch or tag have been specified |
| 85 | +if [ -z "$BRANCH" ] && [ -z "$TAG" ]; then |
| 86 | + _die "please specify a branch or a tag to switch to" |
| 87 | +fi |
| 88 | +if [ -n "$BRANCH" ] && [ -n "$TAG" ]; then |
| 89 | + _die "please either specify a branch or a tag, not both" |
| 90 | +fi |
| 91 | + |
| 92 | +# optionally add the remote |
| 93 | +if [ -n "$REMOTE" ]; then |
| 94 | + BRANCH="$REMOTE/$BRANCH" |
| 95 | +fi |
| 96 | + |
| 97 | +# change to the script directory (project root) |
| 98 | +pushd "$DIR" >/dev/null || exit 1 |
| 99 | + |
| 100 | +# get list of submodules |
| 101 | +if ! [ -r .gitmodules ]; then |
| 102 | + _die "project has no git submodules" |
| 103 | +fi |
| 104 | +SUBS=$(awk -F '"' '/submodule/ {print $2}' .gitmodules) |
| 105 | + |
| 106 | +# update submodule revs |
| 107 | +for sub in $SUBS; do |
| 108 | + _head "---- submodule: $sub" |
| 109 | + cd "$DIR/$sub" || exit 1 |
| 110 | + # make sure the remote exists, if specified |
| 111 | + if [ -n "$REMOTE" ]; then |
| 112 | + if ! git remote | grep -xq "$REMOTE"; then |
| 113 | + _war "remote \"$REMOTE\" not found" |
| 114 | + continue |
| 115 | + fi |
| 116 | + fi |
| 117 | + # update repo |
| 118 | + if [ "$FETCH" = 1 ]; then |
| 119 | + if [ -n "$REMOTE" ]; then |
| 120 | + git fetch "$REMOTE" |
| 121 | + else |
| 122 | + git fetch --all |
| 123 | + fi |
| 124 | + fi |
| 125 | + # check if specified branch exists |
| 126 | + if [ -n "$BRANCH" ]; then |
| 127 | + if ! git branch -r | grep -wq "$BRANCH"; then |
| 128 | + _war "branch \"$BRANCH\" not found" |
| 129 | + continue |
| 130 | + fi |
| 131 | + # checkout the specified branch |
| 132 | + git checkout "$BRANCH" |
| 133 | + # pull latest changes |
| 134 | + git pull |
| 135 | + fi |
| 136 | + if [ -n "$TAG" ]; then |
| 137 | + if ! git tag | grep -xq "$TAG"; then |
| 138 | + _war "tag \"$TAG\" not found" |
| 139 | + continue |
| 140 | + fi |
| 141 | + # checkout the specified tag |
| 142 | + git checkout "$TAG" |
| 143 | + fi |
| 144 | +done |
| 145 | + |
| 146 | +# go back to calling directory |
| 147 | +popd >/dev/null || exit 1 |
0 commit comments