-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheck-dev-environment.sh
More file actions
executable file
·203 lines (170 loc) · 5.87 KB
/
Copy pathcheck-dev-environment.sh
File metadata and controls
executable file
·203 lines (170 loc) · 5.87 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/bash
# shellcheck disable=SC2317
#
# Sync the developer's environment with what is required by the currently active branch.
#
# Colors for output
# Setting the color codes
GREEN='\033[0;32m' # Green
RED='\033[0;31m' # Red
BLUE='\033[0;34m' # Blue
YELLOW='\033[0;33m' # Yellow color for warning messages
NC='\033[0m' # No color
CYAN='\033[0;36m' # Cyan color for check messages
DIM='\033[2m'
# Initialize error counter
ERROR_COUNT=0
# Function to print success messages
print_success() {
echo -e "✅ ${GREEN}$1${NC}"
}
# Function to print error messages and increment error count
print_error() {
echo -e "❌ ${RED}$1${NC}"
((ERROR_COUNT++))
}
print_warning() {
echo -e " ⚠️ ${YELLOW}$1${NC}"
}
print_findings() {
local message="$1"
local findings="${2}"
local icon="${3:-📍}" # Default to a pin
if [ -n "$findings" ]; then
echo -e " ${icon} ${message}: ${DIM}${findings}${NC}"
else
echo -e " ${icon} ${message}"
fi
}
print_check() {
local message="$1"
local icon="${2:-🕵️}" # Default to a bullet point if not provided
echo -e "${CYAN}${icon} ${message}${NC}"
}
prompt_user() {
local prompt="$1"
local default="${2:-Y}"
local REPLY
local options
if [[ $default =~ ^[Yy]$ ]]; then
options="(Y/n)"
else
options="(y/N)"
fi
while true; do
read -p "$prompt $options: " -n 1 -r REPLY
if [ -n "$REPLY" ] ; then
echo
fi
REPLY=${REPLY:-$default}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0
elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 1
else
print_error "Invalid input. Please answer Y or N (or press Enter for the default)."
exit 1
fi
done
}
check_git_submodules() {
print_check "Checking git submodules..." "🧩"
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
print_error "Not in a git repository"
return 1
fi
# Check if there are any submodules
if ! submodule_status=$(git submodule status) ; then
print_error "'git submodule status' failed"
return 1
fi
if [ -z "$submodule_status" ] ; then
print_success "No submodules found"
return 0
fi
incorrect_submodules=0
while IFS= read -r line; do
status_char=${line:0:1}
submodule_path=$(echo "$line" | awk '{print $2}')
current_commit=$(echo "$line" | awk '{print $1}' | sed 's/[^0-9a-f]//g')
GITSM="git -C $submodule_path"
case $status_char in
"-")
print_error "Submodule not initialized: $submodule_path"
((incorrect_submodules++))
;;
"+")
target_commit=$(git ls-tree --object-only HEAD "$submodule_path")
if $GITSM merge-base --is-ancestor "$target_commit" "$current_commit" 2> /dev/null; then
if [ "$target_commit" = "$current_commit" ]; then
print_warning "Submodule has uncommitted changes: $submodule_path"
else
print_warning "Submodule is ahead of parent repo: $submodule_path"
fi
else
print_error "Submodule is behind parent repo: $submodule_path"
((incorrect_submodules++))
fi
;;
" ")
print_success "Submodule at correct commit: $submodule_path"
;;
"U")
print_error "Submodule has merge conflicts: $submodule_path"
((incorrect_submodules++))
;;
*)
print_warning "Unknown status for submodule: $submodule_path"
;;
esac
done <<< "$submodule_status"
if [ $incorrect_submodules -eq 0 ]; then
print_success "All submodules are at the correct commit or ahead"
return 0
fi
print_error "$incorrect_submodules submodule(s) are not at the correct commit"
echo -e "ℹ️${BLUE}To update all submodules to the correct commit, run:${NC}"
echo -e "${DIM} git submodule update --init --recursive${NC}"
if prompt_user "Would you like to run this command now?"; then
echo "Updating submodules..."
if git submodule update --init --recursive; then
print_success "Submodules updated successfully"
incorrect_submodules=0
else
print_error "Failed to update submodules"
fi
else
print_warning "Submodules not updated"
fi
# Add information about git config submodule.recurse true
echo -e "${BLUE}ℹ️ To automatically update submodules on git operations, you can set:${NC}"
echo -e "${GREEN} git config submodule.recurse true${NC}"
echo -e "${BLUE}ℹ️ This will ensure submodules are updated whenever you pull or checkout branches.${NC}"
return $incorrect_submodules
}
echo -e "${BLUE}🔍 Checking development environment...${NC}"
check_git_submodules
# Load and run individual check scripts
CHECK_DIR="./kausal_common/development/env-checks"
if [ -d "$CHECK_DIR" ]; then
for check_script in "$CHECK_DIR"/[0-9][0-9]-*.sh; do
if [ -f "$check_script" ]; then
echo -e "${BLUE}🔍 Running check: $(basename "$check_script")${NC}"
# Source the script, passing necessary functions and variables
# shellcheck disable=SC1090
. "$check_script"
fi
done
else
print_error "Check directory not found: $CHECK_DIR"
fi
echo -e "${BLUE}🏁 Environment check complete.${NC}"
# Print final status and exit
if [ $ERROR_COUNT -eq 0 ]; then
echo -e "${GREEN}✅ All checks passed successfully!${NC}"
exit 0
else
echo -e "❌ ${RED}$ERROR_COUNT error(s) detected during the environment check.${NC}"
exit 1
fi