Skip to content

Commit 649c111

Browse files
trvswgnrTravis Wagner
authored andcommitted
add script to update submodules (#81)
* add script to update submodules * read from .gitmodules file
1 parent 2bd2be9 commit 649c111

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

update-submodules.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/env bash
2+
3+
fail() {
4+
printf '%s\n' "$1" >&2
5+
exit "${2-1}"
6+
}
7+
8+
# make sure we're in the root directory of the repository
9+
if [ ! -f "Cargo.toml" ]; then
10+
fail "Failed to find \"Cargo.toml\", are you sure you're in the root directory of the repository?"
11+
fi
12+
13+
cwd=$(pwd)
14+
15+
# update all submodules
16+
git submodule update --init --recursive
17+
18+
# array of submodule paths and their corresponding branch names
19+
submodules=()
20+
21+
# get the path and the branch name from the .gitmodules file
22+
while IFS=$'\n' read -r line; do
23+
if [[ "$line" =~ ^.*path\ =.*$ ]]; then
24+
path=${line#*= }
25+
elif [[ "$line" =~ ^.*branch\ =.*$ ]]; then
26+
branch=${line#*= }
27+
submodules+=("$path:$branch")
28+
fi
29+
done < .gitmodules
30+
31+
# iterate over the array and update each submodule
32+
for i in "${submodules[@]}"; do
33+
name=${i%%:*}
34+
branch=${i#*:}
35+
echo "Updating $name to $branch"
36+
# change directory to the submodule
37+
cd "$name" || fail "Failed to change directory into submodule \"$name\""
38+
# fetch the latest changes from the remote
39+
git fetch || fail "Failed to fetch latest changes for submodule \"$name\""
40+
# checkout the specified branch
41+
git checkout "$branch" || fail "Failed to checkout branch \"$branch\" for submodule \"$name\""
42+
# pull the latest changes from the remote
43+
git pull || fail "Failed to pull latest changes for submodule \"$name\""
44+
# change directory back to the root of the repository
45+
cd "$cwd" || fail "Failed to change directory back to root of repository"
46+
done

0 commit comments

Comments
 (0)