Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion scripts/add-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ is_ignored() {
has_header() {
local header
header="$(head -20 "$1")"
echo "$header" | grep -qF "$SPDX_REGEX" && echo "$header" | grep -qE "$COPYRIGHT_REGEX"
# Here-string, not `echo |`: grep -q exits on the first match, so echo can die of
# SIGPIPE (141) and pipefail turns that into a false "no header", which makes
# add_header prepend a second copyright block to a file that already has one.
grep -qF "$SPDX_REGEX" <<<"$header" && grep -qE "$COPYRIGHT_REGEX" <<<"$header"
}

add_header() {
Expand Down
10 changes: 7 additions & 3 deletions scripts/verify-license.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CURRENT_YEAR=$(date +%Y)
MIN_YEAR=2026
SPDX_REGEX="SPDX-License-Identifier: Apache-2.0"
COPYRIGHT_REGEX="Copyright [0-9]{4} alibaba/open-code-review Contributors"
YEAR_REGEX="Copyright ([0-9]{4})"

LICENSE_EXTS=(go sh js mjs ts tsx)

Expand Down Expand Up @@ -50,17 +51,20 @@ while IFS= read -r file; do

header="$(head -20 "$file")"

if ! echo "$header" | grep -qF "$SPDX_REGEX"; then
# Feed $header via here-string, not `echo |`: grep -q exits on the first match,
# so echo can die of SIGPIPE (141) and pipefail then reports a valid header as missing.
if ! grep -qF "$SPDX_REGEX" <<<"$header"; then
FAILED+=("$file (missing SPDX identifier)")
continue
fi

if ! echo "$header" | grep -qE "$COPYRIGHT_REGEX"; then
if ! grep -qE "$COPYRIGHT_REGEX" <<<"$header"; then
FAILED+=("$file (missing copyright notice)")
continue
fi

year="$(echo "$header" | grep -oE 'Copyright ([0-9]{4})' | grep -oE '[0-9]{4}' | head -1)"
year=""
[[ "$header" =~ $YEAR_REGEX ]] && year="${BASH_REMATCH[1]}"
if [ -z "$year" ] || [ "$year" -lt "$MIN_YEAR" ] || [ "$year" -gt "$CURRENT_YEAR" ]; then
FAILED+=("$file (invalid year: ${year:-none})")
continue
Expand Down
Loading