diff --git a/scripts/add-license.sh b/scripts/add-license.sh index ec07b8ba8..dfc07b73b 100755 --- a/scripts/add-license.sh +++ b/scripts/add-license.sh @@ -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() { diff --git a/scripts/verify-license.sh b/scripts/verify-license.sh index 84555247d..cc542d2fe 100755 --- a/scripts/verify-license.sh +++ b/scripts/verify-license.sh @@ -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) @@ -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