-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-submodules.sh
More file actions
executable file
·78 lines (63 loc) · 2.47 KB
/
update-submodules.sh
File metadata and controls
executable file
·78 lines (63 loc) · 2.47 KB
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
#!/bin/bash
# Exit on any error
set -e
echo "📦 Checking submodule branches and updating..."
# Function to check and update submodules
check_and_update_submodules() {
local has_issues=0
# Get list of all submodules
if [ ! -f .gitmodules ]; then
echo "❌ No .gitmodules file found in current directory"
exit 1
fi
# Parse submodules from .gitmodules and store in array
local submodules=()
while IFS= read -r line; do
if [[ $line =~ submodule\..*\.path ]]; then
local path=$(echo "$line" | cut -d' ' -f2)
submodules+=("$path")
fi
done < <(git config -f .gitmodules --get-regexp '^submodule\..*\.path$')
# Process each submodule
for path in "${submodules[@]}"; do
if [ -d "$path" ]; then
echo "🔍 Checking submodule: $path"
# Enter the submodule directory and check current branch
cd "$path"
# Get current branch name
current_branch=$(git branch --show-current 2>/dev/null || echo "")
if [ -z "$current_branch" ]; then
echo " ⚠️ Repository is in detached HEAD state - please checkout main manually"
has_issues=1
elif [ "$current_branch" != "main" ]; then
echo " ⚠️ Currently on '$current_branch' - please checkout main manually"
has_issues=1
else
echo " ✅ On main branch"
# Go back to parent directory to run submodule update
cd - > /dev/null
echo " 🔄 Updating..."
if git submodule update --remote --merge "$path"; then
echo " ✅ Updated successfully"
else
echo " ❌ Failed to update"
has_issues=1
fi
# Go back to submodule to continue loop
cd "$path"
fi
# Return to parent directory
cd - > /dev/null
echo ""
fi
done
return $has_issues
}
# Run the check and update function
if check_and_update_submodules; then
echo "🎉 All submodules updated successfully!"
echo "Run 'git status' to review changes before committing."
else
echo "⚠️ Some submodules need manual attention. Fix them and re-run the script."
exit 1
fi