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