forked from gitleaks/gitleaks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-all-repos.sh
More file actions
executable file
Β·629 lines (544 loc) Β· 21.3 KB
/
update-all-repos.sh
File metadata and controls
executable file
Β·629 lines (544 loc) Β· 21.3 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#!/bin/bash
# Script to install gitleaks pre-commit hooks in existing repositories
# Supports both Husky-managed repos and native Git hooks
# Based on git-secrets update-all-repos.sh
# Usage examples:
# ./update-all-repos.sh # Smart mode: scans current dir + common locations
# ./update-all-repos.sh --all # Scans home + system dirs (auto-sudo if needed)
# ./update-all-repos.sh ~/Projects # Updates all repos in ~/Projects (recursively)
# ./update-all-repos.sh ~/Sites ~/Projects # Updates repos in multiple directories
# sudo ./update-all-repos.sh /var # Updates repos in system directories (requires root)
#
# Environment variables:
# MAX_DEPTH=3 ./update-all-repos.sh ~/ # Limit recursion depth (default: unlimited)
#
# WARNING: Scanning large directories like ~ or / can take a very long time!
# It's better to specify specific project directories.
HIGHLIGHT="\e[01;34m"
SUCCESS="\e[01;32m"
ERROR="\e[01;31m"
WARNING="\e[01;33m"
NORMAL='\e[00m'
# Configuration
MAX_DEPTH="${MAX_DEPTH:-}" # Default: unlimited depth
DRY_RUN="${DRY_RUN:-false}" # Set to true to only show what would be updated
# Temporary files for tracking stats across subshells
STATS_DIR=$(mktemp -d)
trap "rm -rf $STATS_DIR" EXIT
touch "$STATS_DIR/found"
touch "$STATS_DIR/updated"
touch "$STATS_DIR/failed"
touch "$STATS_DIR/skipped"
function increment_stat {
local stat_file="$STATS_DIR/$1"
echo "1" >> "$stat_file"
}
function get_stat {
local stat_file="$STATS_DIR/$1"
wc -l < "$stat_file" 2>/dev/null | tr -d ' ' || echo "0"
}
# Check if gitleaks is installed
if ! command -v gitleaks &> /dev/null; then
echo -e "${ERROR}Error: gitleaks is not installed${NORMAL}"
echo "Please install gitleaks first:"
echo " β’ macOS: brew install gitleaks"
echo " β’ Linux: Download from https://github.com/gitleaks/gitleaks/releases"
echo " β’ Go: go install github.com/gitleaks/gitleaks/v8@latest"
exit 1
fi
# Check if global template is set up
TEMPLATE_DIR="$HOME/.git-template"
# Handle case when running with sudo - use the actual user's home
if [ -n "$SUDO_USER" ]; then
TEMPLATE_DIR=$(eval echo ~$SUDO_USER)/.git-template
fi
if [ ! -d "$TEMPLATE_DIR/hooks" ]; then
echo -e "${ERROR}Error: Git template directory not found${NORMAL}"
echo "Expected location: $TEMPLATE_DIR/hooks"
echo "Please run ./install-gitleaks-global.sh first"
exit 1
fi
# Function to sync global gitleaks config from repository
function sync_global_config {
# Determine the script directory (where .gitleaks.toml should be)
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local source_config="$script_dir/.gitleaks.toml"
# Determine the target config directory (handle sudo case)
local config_dir="$HOME/.config/gitleaks"
if [ -n "$SUDO_USER" ]; then
config_dir=$(eval echo ~$SUDO_USER)/.config/gitleaks
fi
local target_config="$config_dir/gitleaks.toml"
# Check if source config exists
if [ ! -f "$source_config" ]; then
echo -e "${WARNING}β ${NORMAL} Warning: Source config not found: $source_config"
echo -e "${HIGHLIGHT}β${NORMAL} Skipping config sync"
return 1
fi
# Create config directory if it doesn't exist
mkdir -p "$config_dir" 2>/dev/null || {
echo -e "${ERROR}β${NORMAL} Failed to create config directory: $config_dir"
return 1
}
# Copy the config file
if cp "$source_config" "$target_config" 2>/dev/null; then
echo -e "${SUCCESS}β${NORMAL} Synced global config: $target_config"
return 0
else
echo -e "${ERROR}β${NORMAL} Failed to sync config to: $target_config"
return 1
fi
}
# Function to check if gitleaks is already in a file
function has_gitleaks {
local file="$1"
grep -q "gitleaks" "$file" 2>/dev/null
}
# Function to safely inject gitleaks into Husky pre-commit hook
function inject_gitleaks_husky {
local hook_file="$1"
# Check if gitleaks is already present
if has_gitleaks "$hook_file"; then
echo -e " ${SUCCESS}β${NORMAL} Gitleaks already configured in Husky pre-commit"
return 0
fi
# Create temporary file with injected gitleaks
local temp_file=$(mktemp)
# Read the file and inject gitleaks after the husky.sh line
local injected=false
while IFS= read -r line; do
echo "$line" >> "$temp_file"
# Inject after the husky.sh sourcing line
if [[ ! "$injected" == true ]] && [[ "$line" =~ \.\s+.*husky\.sh ]] || [[ "$line" =~ source.*husky\.sh ]]; then
cat >> "$temp_file" << 'GITLEAKS_INJECT'
# Gitleaks secret scanning (auto-injected by gitleaks)
# Add common gitleaks installation paths to PATH for Husky non-login shell
export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH"
if command -v gitleaks &> /dev/null; then
echo "π Scanning for secrets with gitleaks..."
GITLEAKS_CONFIG="$HOME/.config/gitleaks/gitleaks.toml"
if [ -f "$GITLEAKS_CONFIG" ]; then
gitleaks protect --staged --redact --verbose --config="$GITLEAKS_CONFIG" || exit 1
else
gitleaks protect --staged --redact --verbose || exit 1
fi
echo "β No secrets detected"
else
echo "β Warning: gitleaks not found, skipping secret scan"
fi
GITLEAKS_INJECT
injected=true
fi
done < "$hook_file"
# If we didn't find the husky.sh line, append at the end
if [ "$injected" != true ]; then
cat >> "$temp_file" << 'GITLEAKS_INJECT'
# Gitleaks secret scanning (auto-injected by gitleaks)
if command -v gitleaks &> /dev/null; then
echo "π Scanning for secrets with gitleaks..."
GITLEAKS_CONFIG="$HOME/.config/gitleaks/gitleaks.toml"
if [ -f "$GITLEAKS_CONFIG" ]; then
gitleaks protect --staged --redact --verbose --config="$GITLEAKS_CONFIG" || exit 1
else
gitleaks protect --staged --redact --verbose || exit 1
fi
echo "β No secrets detected"
else
echo "β Warning: gitleaks not found, skipping secret scan"
fi
GITLEAKS_INJECT
fi
# Replace original file with modified version
mv "$temp_file" "$hook_file" 2>/dev/null || {
echo -e " ${ERROR}β${NORMAL} Failed to update $hook_file"
rm -f "$temp_file"
return 1
}
# Ensure it's executable
chmod +x "$hook_file" 2>/dev/null || {
echo -e " ${WARNING}β ${NORMAL} Warning: Could not make hook executable"
}
echo -e " ${SUCCESS}β${NORMAL} Injected gitleaks into Husky pre-commit hook"
return 0
}
# Function to create Husky pre-commit hook if it doesn't exist
function create_husky_precommit {
local hook_file="$1"
mkdir -p "$(dirname "$hook_file")" 2>/dev/null || {
echo -e " ${ERROR}β${NORMAL} Failed to create .husky directory"
return 1
}
cat > "$hook_file" << 'HUSKY_HOOK'
# Gitleaks secret scanning (auto-injected by gitleaks)
if command -v gitleaks &> /dev/null; then
echo "π Scanning for secrets with gitleaks..."
GITLEAKS_CONFIG="$HOME/.config/gitleaks/gitleaks.toml"
if [ -f "$GITLEAKS_CONFIG" ]; then
gitleaks protect --staged --redact --verbose --config="$GITLEAKS_CONFIG" || exit 1
else
gitleaks protect --staged --redact --verbose || exit 1
fi
echo "β No secrets detected"
else
echo "β Warning: gitleaks not found, skipping secret scan"
fi
HUSKY_HOOK
chmod +x "$hook_file" 2>/dev/null || {
echo -e " ${WARNING}β ${NORMAL} Warning: Could not make hook executable"
}
echo -e " ${SUCCESS}β${NORMAL} Created Husky pre-commit hook with gitleaks"
return 0
}
# Function to install native Git hooks
function install_native_hooks {
mkdir -p .git/hooks 2>/dev/null || {
echo -e " ${ERROR}β${NORMAL} Failed to create hooks directory"
return 1
}
local success=true
# Check if pre-commit already exists and has gitleaks
if [ -f .git/hooks/pre-commit ]; then
if has_gitleaks .git/hooks/pre-commit; then
echo -e " ${SUCCESS}β${NORMAL} Native pre-commit hook already has gitleaks"
else
# Backup existing hook
cp .git/hooks/pre-commit .git/hooks/pre-commit.backup.$(date +%s) 2>/dev/null
echo -e " ${WARNING}β ${NORMAL} Existing pre-commit hook backed up"
# Install new hook (overwriting)
if [ -f "$TEMPLATE_DIR/hooks/pre-commit" ]; then
cp "$TEMPLATE_DIR/hooks/pre-commit" .git/hooks/pre-commit 2>/dev/null && \
chmod +x .git/hooks/pre-commit 2>/dev/null && \
echo -e " ${SUCCESS}β${NORMAL} Installed pre-commit hook"
else
success=false
fi
fi
else
# No existing hook, just install
if [ -f "$TEMPLATE_DIR/hooks/pre-commit" ]; then
cp "$TEMPLATE_DIR/hooks/pre-commit" .git/hooks/pre-commit 2>/dev/null && \
chmod +x .git/hooks/pre-commit 2>/dev/null && \
echo -e " ${SUCCESS}β${NORMAL} Installed pre-commit hook"
else
success=false
fi
fi
# Install commit-msg hook
if [ -f .git/hooks/commit-msg ]; then
if has_gitleaks .git/hooks/commit-msg; then
echo -e " ${SUCCESS}β${NORMAL} Native commit-msg hook already has gitleaks"
else
cp .git/hooks/commit-msg .git/hooks/commit-msg.backup.$(date +%s) 2>/dev/null
if [ -f "$TEMPLATE_DIR/hooks/commit-msg" ]; then
cp "$TEMPLATE_DIR/hooks/commit-msg" .git/hooks/commit-msg 2>/dev/null && \
chmod +x .git/hooks/commit-msg 2>/dev/null && \
echo -e " ${SUCCESS}β${NORMAL} Installed commit-msg hook"
fi
fi
else
if [ -f "$TEMPLATE_DIR/hooks/commit-msg" ]; then
cp "$TEMPLATE_DIR/hooks/commit-msg" .git/hooks/commit-msg 2>/dev/null && \
chmod +x .git/hooks/commit-msg 2>/dev/null && \
echo -e " ${SUCCESS}β${NORMAL} Installed commit-msg hook"
fi
fi
# Test the installation
if [ -x .git/hooks/pre-commit ]; then
echo -e " ${SUCCESS}β${NORMAL} Hooks are executable and ready"
else
echo -e " ${ERROR}β${NORMAL} Warning: Hooks may not be executable"
success=false
fi
if [ "$success" != true ]; then
return 1
fi
return 0
}
# Function to process a single git repository
function process_repo {
local repodir="$1"
cd "$repodir" 2>/dev/null || {
echo -e "${WARNING}β ${NORMAL} Cannot access repository: $repodir (permission denied)"
increment_stat "skipped"
return 1
}
increment_stat "found"
printf "%b\n" "${HIGHLIGHT}Found git repository: $(pwd)${NORMAL}"
# Dry run mode - just show what would be updated
if [ "$DRY_RUN" = "true" ]; then
echo -e " ${HIGHLIGHT}β${NORMAL} [DRY RUN] Would install hooks here"
increment_stat "updated"
return 0
fi
# Check if .git directory is writable
if [ ! -w ".git" ]; then
echo -e " ${ERROR}β${NORMAL} Cannot write to .git directory (permission denied)"
echo -e " ${WARNING}β ${NORMAL} Repository owned by: $(stat -c '%U:%G' .git 2>/dev/null || echo 'unknown')"
echo -e " ${WARNING}β ${NORMAL} Current user: $(whoami)"
if [ "$EUID" -ne 0 ]; then
echo -e " ${HIGHLIGHT}β${NORMAL} Tip: Run script with sudo to update system repositories"
fi
increment_stat "failed"
return 1
fi
# CRITICAL: Check if core.hooksPath is configured but directory doesn't exist
HOOKS_PATH=$(git config core.hooksPath 2>/dev/null || echo "")
if [ -n "$HOOKS_PATH" ] && [ ! -d "$HOOKS_PATH" ]; then
echo -e " ${ERROR}π¨ CRITICAL${NORMAL}: Git is configured to use hooks from '$HOOKS_PATH' but directory doesn't exist!"
echo -e " ${WARNING}β ${NORMAL} This means NO hooks are running - security bypass!"
echo -e " ${HIGHLIGHT}β${NORMAL} Fixing: Unsetting core.hooksPath and installing native hooks"
git config --unset core.hooksPath
if install_native_hooks; then
increment_stat "updated"
return 0
else
increment_stat "failed"
return 1
fi
fi
# Detect if this repo uses Husky
if [ -d ".husky" ]; then
echo -e " ${HIGHLIGHT}β${NORMAL} Detected Husky repository"
# Check if pre-commit exists
if [ -f ".husky/pre-commit" ]; then
if inject_gitleaks_husky ".husky/pre-commit"; then
increment_stat "updated"
return 0
else
increment_stat "failed"
return 1
fi
else
# Check if husky.sh exists to confirm it's a valid Husky setup
if [ -f ".husky/_/husky.sh" ] || [ -f ".husky/husky.sh" ]; then
if create_husky_precommit ".husky/pre-commit"; then
increment_stat "updated"
return 0
else
increment_stat "failed"
return 1
fi
else
echo -e " ${WARNING}β ${NORMAL} Husky directory exists but appears incomplete"
echo -e " ${HIGHLIGHT}β${NORMAL} Installing native Git hooks as fallback"
if install_native_hooks; then
increment_stat "updated"
return 0
else
increment_stat "failed"
return 1
fi
fi
fi
else
# Check if core.hooksPath points to .husky but .husky doesn't exist
if [ "$HOOKS_PATH" = ".husky/_" ] || [ "$HOOKS_PATH" = ".husky" ]; then
echo -e " ${WARNING}β ${NORMAL} Repo was using Husky but .husky/ is missing"
echo -e " ${HIGHLIGHT}β${NORMAL} Unsetting core.hooksPath and using native hooks"
git config --unset core.hooksPath
fi
# Standard git hooks installation
echo -e " ${HIGHLIGHT}β${NORMAL} Using native Git hooks"
if install_native_hooks; then
increment_stat "updated"
return 0
else
increment_stat "failed"
return 1
fi
fi
}
function update_directory {
local target_dir="$1"
# Use current directory if none specified
if [ -z "$target_dir" ]; then
target_dir="$PWD"
fi
# Convert to absolute path
if [[ "$target_dir" != /* ]]; then
target_dir="$PWD/$target_dir"
fi
# Check if directory exists and is accessible
if [ ! -d "$target_dir" ]; then
echo -e "${ERROR}β${NORMAL} Directory does not exist: $target_dir"
return 1
fi
if [ ! -r "$target_dir" ]; then
echo -e "${ERROR}β${NORMAL} Cannot read directory: $target_dir (permission denied)"
if [ "$EUID" -ne 0 ]; then
echo -e "${HIGHLIGHT}β${NORMAL} Try running with sudo: sudo ./update-all-repos.sh $target_dir"
fi
return 1
fi
# Warn if trying to update system directories
if [[ "$target_dir" == "/var"* ]] || [[ "$target_dir" == "/etc"* ]] || [[ "$target_dir" == "/sys"* ]] || [[ "$target_dir" == "/proc"* ]]; then
echo -e "${WARNING}β ${NORMAL} WARNING: Scanning system directory: ${target_dir}"
if [ "$EUID" -ne 0 ]; then
echo -e "${ERROR}β${NORMAL} ERROR: System directories require root privileges"
echo -e "${HIGHLIGHT}β${NORMAL} Please run with sudo: sudo ./update-all-repos.sh \"$target_dir\""
return 1
fi
echo -e "${SUCCESS}β${NORMAL} Running with root privileges"
fi
printf "%b\n" "${HIGHLIGHT}Scanning ${target_dir} recursively for git repositories...${NORMAL}"
# Build find command with optional depth limit
local find_cmd="find \"$target_dir\""
if [ -n "$MAX_DEPTH" ]; then
find_cmd="$find_cmd -maxdepth $MAX_DEPTH"
echo -e "${HIGHLIGHT}β${NORMAL} Maximum depth: $MAX_DEPTH levels"
else
echo -e "${WARNING}β ${NORMAL} WARNING: Unlimited depth - this may take a very long time for large directories!"
echo -e "${HIGHLIGHT}β${NORMAL} Tip: Set MAX_DEPTH to limit recursion (e.g., MAX_DEPTH=3 ./update-all-repos.sh ~)"
fi
echo -e "${HIGHLIGHT}β${NORMAL} Press Ctrl+C to cancel if this takes too long"
echo ""
# Use find to recursively locate all .git directories
# Exclude common large directories to speed up search
# The -print0 and read -d '' handle filenames with spaces and special characters
local count=0
while IFS= read -r -d '' gitdir; do
local repodir=$(dirname "$gitdir")
# Skip submodules (git repos inside .git directories)
if [[ "$repodir" == *"/.git/"* ]] || [[ "$repodir" == *"/.git" ]]; then
continue
fi
count=$((count + 1))
echo -e "${HIGHLIGHT}[$count]${NORMAL} Found: $repodir"
# Process this repository in current shell (not subshell) to track stats
(process_repo "$repodir")
echo ""
done < <(find "$target_dir" \
${MAX_DEPTH:+-maxdepth $MAX_DEPTH} \
-type d \
\( \
-name "node_modules" -o -name ".npm" -o -name ".cache" -o -name "__pycache__" \
-o -name ".venv" -o -name "venv" -o -name ".local" -o -name ".cargo" \
-o -name ".rustup" -o -name ".m2" -o -name ".gradle" -o -name "target" \
-o -name "build" -o -name "dist" -o -name "vendor" -o -name ".bundle" \
-o -path "*/var/lib/*" -o -path "*/var/cache/*" -o -path "*/var/log/*" \
-o -path "*/var/run/*" -o -path "*/var/lock/*" -o -path "*/var/spool/*" \
-o -path "*/var/mail/*" -o -path "*/var/backups/*" -o -path "*/var/crash/*" \
-o -path "*/var/snap/*" -o -path "*/var/metrics/*" \
\) -prune -o \
-type d -name ".git" -print0 2>/dev/null)
}
# Main execution
echo -e "${HIGHLIGHT}========================================${NORMAL}"
echo -e "${HIGHLIGHT}Gitleaks Hook Installer${NORMAL}"
echo -e "${HIGHLIGHT}========================================${NORMAL}\n"
# Check for --all flag (treat it same as passing home directory)
if [ "$1" = "--all" ]; then
# Replace --all with home directory
if [ "$EUID" -eq 0 ] && [ -n "$SUDO_USER" ]; then
# Running as root, use SUDO_USER's home
set -- "$(eval echo ~$SUDO_USER)"
else
set -- "$HOME"
fi
fi
if [ "$EUID" -eq 0 ]; then
echo -e "${WARNING}β ${NORMAL} Running as root (sudo)"
echo -e "${HIGHLIGHT}β${NORMAL} Will be able to update system-owned repositories"
echo ""
fi
# Sync global gitleaks config from repository
echo -e "${HIGHLIGHT}Syncing global gitleaks configuration...${NORMAL}"
sync_global_config
echo ""
if [ "$#" -eq 0 ]; then
# No arguments provided - use smart defaults
echo -e "${HIGHLIGHT}No directory specified - using smart detection${NORMAL}\n"
# Always scan current directory first
update_directory "$PWD"
echo ""
# Auto-scan system directories
AUTO_SCAN_SYSTEM=true
else
# Check if user provided home directory or similar
AUTO_SCAN_SYSTEM=false
for arg in "$@"; do
# Expand ~ to actual home path
expanded_arg=$(eval echo "$arg")
# If user specified home directory, also scan system dirs
if [ "$expanded_arg" = "$HOME" ] || [ "$expanded_arg" = "~" ]; then
AUTO_SCAN_SYSTEM=true
fi
done
# Process specified directories first
for dir in "$@"; do
update_directory "$dir"
echo ""
done
fi
# Auto-scan system directories if enabled
if [ "$AUTO_SCAN_SYSTEM" = true ]; then
echo -e "${HIGHLIGHT}Auto-detecting system project directories...${NORMAL}"
# Check if common project directories exist and scan them
SYSTEM_DIRS=("/var" "/opt" "/srv")
DIRS_TO_SCAN=()
for dir in "${SYSTEM_DIRS[@]}"; do
if [ -d "$dir" ] && [ -r "$dir" ]; then
DIRS_TO_SCAN+=("$dir")
fi
done
if [ ${#DIRS_TO_SCAN[@]} -eq 0 ]; then
echo -e "${HIGHLIGHT}β${NORMAL} No system directories found"
echo ""
else
echo -e "${HIGHLIGHT}β${NORMAL} Found system directories: ${DIRS_TO_SCAN[*]}"
echo ""
for dir in "${DIRS_TO_SCAN[@]}"; do
echo -e "${HIGHLIGHT}Scanning $dir for repositories...${NORMAL}"
# Check if we're already root
if [ "$EUID" -eq 0 ]; then
update_directory "$dir"
else
# Not root, need to run this part with sudo
echo -e "${WARNING}β ${NORMAL} System directory requires root privileges"
echo -e "${HIGHLIGHT}β${NORMAL} Running with sudo for $dir..."
echo -e "${HIGHLIGHT}β${NORMAL} You may be prompted for your password..."
sudo -E bash "$0" "$dir"
fi
echo ""
done
fi
fi
# Get final statistics
REPOS_FOUND=$(get_stat "found")
REPOS_UPDATED=$(get_stat "updated")
REPOS_FAILED=$(get_stat "failed")
REPOS_SKIPPED=$(get_stat "skipped")
echo -e "\n${SUCCESS}========================================${NORMAL}"
echo -e "${SUCCESS}Update Complete!${NORMAL}"
echo -e "${SUCCESS}========================================${NORMAL}\n"
echo -e "${HIGHLIGHT}Summary:${NORMAL}"
echo " β’ Git repositories found: $REPOS_FOUND"
echo " β’ Successfully updated: $REPOS_UPDATED"
echo " β’ Failed (permission denied): $REPOS_FAILED"
echo " β’ Skipped (inaccessible): $REPOS_SKIPPED"
echo " β’ Both Husky and native Git hooks are supported"
echo " β’ Future commits will be scanned for blockchain private keys and secrets"
echo ""
# Show warning if there were permission failures
if [ "$REPOS_FAILED" -gt 0 ] || [ "$REPOS_SKIPPED" -gt 0 ]; then
echo -e "${WARNING}β ${NORMAL} ${WARNING}WARNING: Some repositories could not be updated due to permission issues${NORMAL}"
if [ "$EUID" -ne 0 ]; then
echo -e "${HIGHLIGHT}β${NORMAL} To update system repositories (in /var, /etc, etc.), run with sudo:"
echo -e " ${HIGHLIGHT}sudo ./update-all-repos.sh /var${NORMAL}"
else
echo -e "${HIGHLIGHT}β${NORMAL} Some repositories may have additional access restrictions"
echo -e " Check ownership and permissions of failed repositories"
fi
echo ""
fi
echo -e "${HIGHLIGHT}Next time, you can use:${NORMAL}"
echo " β’ ${HIGHLIGHT}./update-all-repos.sh --all${NORMAL} (scans home + /var/systango, auto-handles sudo)"
echo " β’ ${HIGHLIGHT}./update-all-repos.sh ~/Projects${NORMAL} (specific directory)"
echo " β’ ${HIGHLIGHT}MAX_DEPTH=3 ./update-all-repos.sh ~${NORMAL} (limit depth for faster scan)"
echo ""
echo -e "${HIGHLIGHT}Test the hooks:${NORMAL}"
echo " cd /path/to/any/repo"
echo " echo 'const key = \"abc\"' > test.js"
echo " git add test.js"
echo " git commit -m 'test' # Add a real secret to test blocking"
echo ""