forked from crablang/crab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-submodules.sh
executable file
·46 lines (39 loc) · 1.5 KB
/
update-submodules.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
#!/usr/bin/env bash
fail() {
printf '%s\n' "$1" >&2
exit "${2-1}"
}
# make sure we're in the root directory of the repository
if [ ! -f "Cargo.toml" ]; then
fail "Failed to find \"Cargo.toml\", are you sure you're in the root directory of the repository?"
fi
cwd=$(pwd)
# update all submodules
git submodule update --init --recursive
# array of submodule paths and their corresponding branch names
submodules=()
# get the path and the branch name from the .gitmodules file
while IFS=$'\n' read -r line; do
if [[ "$line" =~ ^.*path\ =.*$ ]]; then
path=${line#*= }
elif [[ "$line" =~ ^.*branch\ =.*$ ]]; then
branch=${line#*= }
submodules+=("$path:$branch")
fi
done < .gitmodules
# iterate over the array and update each submodule
for i in "${submodules[@]}"; do
name=${i%%:*}
branch=${i#*:}
echo "Updating $name to $branch"
# change directory to the submodule
cd "$name" || fail "Failed to change directory into submodule \"$name\""
# fetch the latest changes from the remote
git fetch || fail "Failed to fetch latest changes for submodule \"$name\""
# checkout the specified branch
git checkout "$branch" || fail "Failed to checkout branch \"$branch\" for submodule \"$name\""
# pull the latest changes from the remote
git pull || fail "Failed to pull latest changes for submodule \"$name\""
# change directory back to the root of the repository
cd "$cwd" || fail "Failed to change directory back to root of repository"
done