-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_codeowners_sections.sh
More file actions
executable file
·136 lines (121 loc) · 2.98 KB
/
sort_codeowners_sections.sh
File metadata and controls
executable file
·136 lines (121 loc) · 2.98 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
#!/usr/bin/env bash
set -euo pipefail
# Sort [section] blocks in CODEOWNERS-style files alphabetically
# Preserves header (everything before the first [section])
# Each section consists of a [filename] line followed by owner lines
#
# Example input:
#
# # Comment header
# set inherit = false
#
# @org/platform
#
# [zebra.yaml]
# @org/team-z
#
# [apple.yaml]
# @org/team-a
#
# Example output (sorted):
#
# # Comment header
# set inherit = false
#
# @org/platform
#
# [apple.yaml]
# @org/team-a
#
# [zebra.yaml]
# @org/team-z
fix_mode=false
if [[ "${1:-}" == "--fix" ]]; then
fix_mode=true
shift
fi
failed=0
sort_file() {
local file="$1"
local tmpdir
tmpdir=$(mktemp -d)
# shellcheck disable=SC2064 # We want $tmpdir to expand now, not at signal time
trap "rm -rf '$tmpdir'" RETURN
# Parse file: extract header and sections to separate files
# Normalize sections: strip trailing blank lines, we'll add them back consistently
awk -v tmpdir="$tmpdir" '
BEGIN {
in_header = 1
section_key = ""
section_lines[0] = ""
section_count = 0
}
/^\[.*\]$/ {
# Save previous section if any
if (section_key != "") {
fname = section_key
gsub(/[\[\]]/, "", fname)
# Trim trailing empty lines
while (section_count > 0 && section_lines[section_count] == "") {
section_count--
}
for (i = 1; i <= section_count; i++) {
print section_lines[i] > (tmpdir "/" fname)
}
close(tmpdir "/" fname)
}
in_header = 0
section_key = $0
section_count = 1
section_lines[1] = $0
next
}
in_header {
print >> (tmpdir "/000_HEADER")
next
}
{
section_count++
section_lines[section_count] = $0
}
END {
if (section_key != "") {
fname = section_key
gsub(/[\[\]]/, "", fname)
while (section_count > 0 && section_lines[section_count] == "") {
section_count--
}
for (i = 1; i <= section_count; i++) {
print section_lines[i] > (tmpdir "/" fname)
}
}
}
' "$file"
# Output header
[[ -f "$tmpdir/000_HEADER" ]] && cat "$tmpdir/000_HEADER" && rm "$tmpdir/000_HEADER"
# Sort section files by name and output with blank line between each
local first=true
for section_file in $(find "$tmpdir" -type f -print0 | sort -z | xargs -0 -n1 basename 2>/dev/null); do
if [[ "$first" != true ]]; then
echo ""
fi
first=false
cat "$tmpdir/$section_file"
done
}
for file in "$@"; do
[[ -f "$file" ]] || continue
sorted_content=$(sort_file "$file")
# Command substitution strips trailing newlines, so comparison is normalized
original_content=$(cat "$file")
if [[ "$sorted_content" != "$original_content" ]]; then
if [[ "$fix_mode" == true ]]; then
printf '%s\n' "$sorted_content" > "$file"
echo "Fixed: $file"
else
echo "Not sorted: $file"
failed=1
fi
fi
done
exit "$failed"