-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_exclusions.sh
More file actions
executable file
·65 lines (56 loc) · 1.87 KB
/
update_exclusions.sh
File metadata and controls
executable file
·65 lines (56 loc) · 1.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
#!/usr/bin/env bash
# update_exclusions.sh
#
# MIT License
# Copyright (c) 2025 Romathi
set -euo pipefail
CONFIG_FILE=".pre-commit-config.yaml"
DEFAULT_EXCLUSIONS_FILE=".pre-commit-default-exclusions"
CUSTOM_EXCLUSIONS_FILE=".pre-commit-exclusions"
TMP_FILE=$(mktemp)
if ! grep -q '# Start exclude' "$CONFIG_FILE" || ! grep -q '# End exclude' "$CONFIG_FILE"; then
echo "❌ ERROR: Markers '# Start exclude' and/or '# End exclude' not found in $CONFIG_FILE."
echo "Please add these lines around the exclude block before running this script."
exit 1
fi
# Read default exclusions (mandatory)
default_exclusions=$(< "$DEFAULT_EXCLUSIONS_FILE")
# Read custom exclusions (optional)
if [[ -f "$CUSTOM_EXCLUSIONS_FILE" && -s "$CUSTOM_EXCLUSIONS_FILE" ]]; then
custom_exclusions=$(< "$CUSTOM_EXCLUSIONS_FILE")
default_exclusions="${default_exclusions}|"
new=$(printf '%s\n%s' "$default_exclusions" "$custom_exclusions")
else
custom_exclusions=""
new="$default_exclusions"
fi
# Prepare the new exclusion block
new=$(printf '%s\n' "$default_exclusions" "$custom_exclusions" | sed 's/^/ /')
# Feed awk directly with the block via a file descriptor
awk -v block_file=<(echo "$new") '
BEGIN {
inside = 0
while ((getline line < block_file) > 0) {
block = block line "\n"
}
close(block_file)
}
/# Start exclude/ {
print; print "exclude: |"
printf "%s", block
inside = 1
next
}
/# End exclude/ { inside = 0 }
!inside
' "$CONFIG_FILE" > "$TMP_FILE"
if ! cmp -s "$TMP_FILE" "$CONFIG_FILE"; then
mv "$TMP_FILE" "$CONFIG_FILE"
echo "✅ Updated .pre-commit-config.yaml with new exclusions."
echo "⚠️ Exclusion list changed. Rerun pre-commit to apply changes."
exit 99
else
rm "$TMP_FILE"
echo "✅ Exclusions are already up to date. No changes made."
exit 0
fi