-
Notifications
You must be signed in to change notification settings - Fork 2
/
clean-repo.sh
31 lines (27 loc) · 1.36 KB
/
clean-repo.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
git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
while read path_key path
do
url_key=$(echo $path_key | sed 's/\.path/.url/');
branch_key=$(echo $path_key | sed 's/\.path/.branch/');
# If the url_key doesn't yet exist then backup up the existing
# directory if necessary and add the submodule
if [ ! $(git config --get "$url_key") ]; then
if [ -d "$path" ] && [ ! $(git config --get "$url_key") ]; then
mv "$path" "$path""_backup_""$(date +'%Y%m%d%H%M%S')";
fi;
url=$(git config -f .gitmodules --get "$url_key");
# If a branch is specified then use that one, otherwise
# default to master
branch=$(git config -f .gitmodules --get "$branch_key");
if [ ! "$branch" ]; then branch="main"; fi;
git submodule add -f -b "$branch" "$url" "$path";
fi;
done;
# In case the submodule exists in .git/config but the url is out of date
git submodule sync;
# Now actually pull all the modules. I used to use this...
#
# git submodule update --init --remote --force --recursive
# ...but the submodules would check out in detached HEAD state and I
# didn't like that, so now I do this...
git submodule foreach --recursive 'git checkout $(git config -f $toplevel/.gitmodules submodule.$name.branch || echo main)';