Skip to content

Commit bfe4194

Browse files
committed
wip
1 parent 543ebb8 commit bfe4194

1 file changed

Lines changed: 67 additions & 26 deletions

File tree

scripts/checkCopyrightPresent.sh

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#!/bin/bash
22
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
33
repo_root="$(cd "${script_dir}/.." && pwd)"
4+
rewrite_headers=false
5+
6+
for arg in "$@"; do
7+
if [ "$arg" = "--rewrite-header" ]; then
8+
rewrite_headers=true
9+
fi
10+
done
411

512
filesWithOutCopyright=()
613
filesWithOutSPDX=()
@@ -19,6 +26,15 @@ expectedHeader=(
1926
" *******************************************************************************/"
2027
)
2128

29+
normalize_line() {
30+
local line="$1"
31+
line="${line//\*/}"
32+
line=$(printf '%s' "$line" | tr -s '[:space:]' ' ')
33+
line="${line#"${line%%[! ]*}"}"
34+
line="${line%"${line##*[! ]}"}"
35+
printf '%s' "$line"
36+
}
37+
2238
rewrite_header() {
2339
local file="$1"
2440
local year_line="$2"
@@ -45,6 +61,41 @@ rewrite_header() {
4561
mv "$tmp_file" "$file"
4662
}
4763

64+
read_header_lines() {
65+
local file="$1"
66+
header_lines=()
67+
while IFS= read -r line; do
68+
line="${line%$'\r'}"
69+
header_lines+=("$line")
70+
done < <(head -n 10 "$file")
71+
}
72+
73+
compute_header_stats() {
74+
local year_line_normalized
75+
local expected_normalized
76+
local actual_normalized
77+
local idx
78+
match_count=0
79+
has_valid_year_line=false
80+
aduna_year=""
81+
82+
year_line_normalized="$(normalize_line "${header_lines[1]}")"
83+
if [[ "$year_line_normalized" =~ ^Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors\.$ ]]; then
84+
has_valid_year_line=true
85+
elif [[ "$year_line_normalized" =~ ^Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors,\ Aduna,\ and\ others\.$ ]]; then
86+
has_valid_year_line=true
87+
aduna_year="${BASH_REMATCH[1]}"
88+
fi
89+
90+
for idx in 0 2 3 4 5 6 7 8 9; do
91+
expected_normalized="$(normalize_line "${expectedHeader[$idx]}")"
92+
actual_normalized="$(normalize_line "${header_lines[$idx]}")"
93+
if [[ "$actual_normalized" == "$expected_normalized" ]]; then
94+
match_count=$((match_count + 1))
95+
fi
96+
done
97+
}
98+
4899
for i in $(find "${repo_root}" -name pom.xml); do
49100
if [[ "$i" == "${repo_root}/core/queryparser/sparql/JavaCC/"* ]]; then
50101
continue
@@ -66,11 +117,7 @@ for i in $(find "${repo_root}" -name pom.xml); do
66117
continue
67118
fi
68119

69-
header_lines=()
70-
while IFS= read -r line; do
71-
line="${line%$'\r'}"
72-
header_lines+=("$line")
73-
done < <(head -n 10 "$c")
120+
read_header_lines "$c"
74121

75122
has_copyright=false
76123
if grep -q "Copyright" "$c"; then
@@ -86,28 +133,16 @@ for i in $(find "${repo_root}" -name pom.xml); do
86133
continue
87134
fi
88135

89-
has_valid_year_line=false
90-
if [[ "${header_lines[1]}" =~ ^\ \*\ Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors\.$ ]]; then
91-
has_valid_year_line=true
92-
elif [[ "${header_lines[1]}" =~ ^\ \*\ Copyright\ \(c\)\ ([0-9]{4})\ Eclipse\ RDF4J\ contributors,\ Aduna,\ and\ others\.$ ]]; then
93-
has_valid_year_line=true
94-
aduna_year="${BASH_REMATCH[1]}"
95-
if [ "$aduna_year" -gt 2020 ]; then
96-
filesWithAdunaYearAfter2020+=("$c")
97-
fi
98-
fi
136+
compute_header_stats
99137

100-
match_count=0
101-
for idx in 0 2 3 4 5 6 7 8 9; do
102-
if [[ "${header_lines[$idx]}" == "${expectedHeader[$idx]}" ]]; then
103-
match_count=$((match_count + 1))
104-
fi
105-
done
138+
if [ "$rewrite_headers" = true ] && [ "$match_count" -ge 4 ] && [ "$match_count" -lt 9 ]; then
139+
rewrite_header "$c" "${header_lines[1]}"
140+
read_header_lines "$c"
141+
compute_header_stats
142+
fi
106143

107-
if [ "$match_count" -ge 2 ]; then
108-
if [ "$match_count" -lt 9 ]; then
109-
rewrite_header "$c" "${header_lines[1]}"
110-
fi
144+
if [ -n "$aduna_year" ] && [ "$aduna_year" -gt 2020 ]; then
145+
filesWithAdunaYearAfter2020+=("$c")
111146
fi
112147

113148
if [ "$has_valid_year_line" = false ]; then
@@ -116,7 +151,7 @@ for i in $(find "${repo_root}" -name pom.xml); do
116151
else
117152
filesWithOutCopyright+=("$c")
118153
fi
119-
elif [ "$match_count" -lt 4 ]; then
154+
elif [ "$match_count" -lt 9 ]; then
120155
filesWithInvalidHeader+=("$c")
121156
fi
122157
done
@@ -139,8 +174,14 @@ for f in "${filesWithOutSPDX[@]}"; do
139174
echo "Missing SPDX line in: $f"
140175
done
141176
if [ ${#filesWithOutCopyright[@]} -ne 0 ] || [ ${#filesWithInvalidHeader[@]} -ne 0 ] || [ ${#filesWithAdunaYearAfter2020[@]} -ne 0 ]; then
177+
if [ "$rewrite_headers" = false ]; then
178+
echo "Tip: rerun with --rewrite-header to auto-fix headers when possible."
179+
fi
142180
exit 1
143181
fi
144182
if [ ${#filesWithOutSPDX[@]} -ne 0 ]; then
183+
if [ "$rewrite_headers" = false ]; then
184+
echo "Tip: rerun with --rewrite-header to auto-fix headers when possible."
185+
fi
145186
exit 2
146187
fi

0 commit comments

Comments
 (0)