Compare commits

..

2 commits

Author SHA1 Message Date
2f0c9eb9ba
[license] keep hacky way under UPDATE
All checks were successful
eden-license / license-header (pull_request) Successful in 34s
Signed-off-by: Caio Oliveira <caiooliveirafarias0@gmail.com>
2025-09-14 20:42:49 -03:00
972279d823
[license] reduce usage of while and improve template usage
Signed-off-by: Caio Oliveira <caiooliveirafarias0@gmail.com>
2025-09-14 20:32:22 -03:00

View file

@ -14,8 +14,9 @@ if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo "This script checks and optionally fixes license headers in source, CMake and shell script files." echo "This script checks and optionally fixes license headers in source, CMake and shell script files."
echo echo
echo "Environment Variables:" echo "Environment Variables:"
echo " FIX=true Automatically add the correct license headers to offending files." echo " FIX=true | Automatically add the correct license headers to offending files."
echo " COMMIT=true If FIX=true, commit the changes automatically." echo " UPDATE=true | Automatically update current license headers of offending files."
echo " COMMIT=true | If FIX=true, commit the changes automatically."
echo echo
echo "Usage Examples:" echo "Usage Examples:"
echo " # Just check headers (will fail if headers are missing)" echo " # Just check headers (will fail if headers are missing)"
@ -24,14 +25,21 @@ if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo " # Fix headers only" echo " # Fix headers only"
echo " FIX=true .ci/license-header.sh" echo " FIX=true .ci/license-header.sh"
echo echo
echo " # Update headers only"
echo " # if COPYRIGHT_OWNER is '$COPYRIGHT_OWNER'"
echo " # or else will have 'FIX=true' behavior)"
echo " UPDATE=true .ci/license-header.sh"
echo
echo " # Fix headers and commit changes" echo " # Fix headers and commit changes"
echo " FIX=true COMMIT=true .ci/license-header.sh" echo " FIX=true COMMIT=true .ci/license-header.sh"
echo
echo " # Update headers and commit changes"
echo " # if COPYRIGHT_OWNER is '$COPYRIGHT_OWNER'"
echo " # or else will have 'FIX=true' behavior)"
echo " UPDATE=true COMMIT=true .ci/license-header.sh"
exit 0 exit 0
fi fi
HEADER_LINE1_TEMPLATE="{COMMENT_TEMPLATE} SPDX-FileCopyrightText: Copyright $COPYRIGHT_YEAR $COPYRIGHT_OWNER"
HEADER_LINE2_TEMPLATE="{COMMENT_TEMPLATE} SPDX-License-Identifier: $COPYRIGHT_LICENSE"
SRC_FILES="" SRC_FILES=""
OTHER_FILES="" OTHER_FILES=""
@ -43,30 +51,10 @@ if git diff --quiet "$BASE"..HEAD; then
fi fi
FILES=$(git diff --name-only "$BASE") FILES=$(git diff --name-only "$BASE")
check_header() { echo_header() {
COMMENT_TYPE="$1" COMMENT_TYPE="$1"
FILE="$2" echo "$COMMENT_TYPE SPDX-FileCopyrightText: Copyright $COPYRIGHT_YEAR $COPYRIGHT_OWNER"
echo "$COMMENT_TYPE SPDX-License-Identifier: $COPYRIGHT_LICENSE"
HEADER_LINE1=$(echo "$HEADER_LINE1_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
HEADER_LINE2=$(echo "$HEADER_LINE2_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
FOUND=0
while IFS= read -r line || [ -n "$line" ]; do
if [ "$line" = "$HEADER_LINE1" ]; then
IFS= read -r next_line || next_line=""
if [ "$next_line" = "$HEADER_LINE2" ]; then
FOUND=1
break
fi
fi
done < "$FILE"
if [ "$FOUND" -eq 0 ]; then
case "$COMMENT_TYPE" in
"//") SRC_FILES="$SRC_FILES $FILE" ;;
"#") OTHER_FILES="$OTHER_FILES $FILE" ;;
esac
fi
} }
for file in $FILES; do for file in $FILES; do
@ -84,7 +72,20 @@ for file in $FILES; do
esac ;; esac ;;
esac esac
check_header "$COMMENT_TYPE" "$file" HEADER=$(echo_header "$COMMENT_TYPE")
HEAD_LINES=$(head -n5 "$file")
CORRECT_COPYRIGHT=$(echo "$HEAD_LINES" | awk \
-v line1="$(echo "$HEADER" | sed -n '1p')" \
-v line2="$(echo "$HEADER" | sed -n '2p')" \
'($0==line1){getline; if($0==line2){f=1}else{f=0}} END{print (f?f:0)}')
if [ "$CORRECT_COPYRIGHT" != "1" ]; then
case "$COMMENT_TYPE" in
"//") SRC_FILES="$SRC_FILES $file" ;;
"#") OTHER_FILES="$OTHER_FILES $file" ;;
esac
fi
done done
if [ -z "$SRC_FILES" ] && [ -z "$OTHER_FILES" ]; then if [ -z "$SRC_FILES" ] && [ -z "$OTHER_FILES" ]; then
@ -121,8 +122,7 @@ for TYPE in "SRC" "OTHER"; do
echo " '$DESC' files is:" echo " '$DESC' files is:"
echo echo
echo "=== BEGIN ===" echo "=== BEGIN ==="
echo "$HEADER_LINE1_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g" echo_header "$COMMENT_TYPE"
echo "$HEADER_LINE2_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g"
echo "=== END ===" echo "=== END ==="
done done
@ -137,7 +137,7 @@ cat << EOF
EOF EOF
TMP_DIR=$(mktemp -d /tmp/license-header.XXXXXX) || exit 1 TMP_DIR=$(mktemp -d /tmp/license-header.XXXXXX) || exit 1
if [ "$FIX" = "true" ]; then if [ "$FIX" = "true" ] || [ "$UPDATE" = "true" ]; then
echo echo
echo "license-header.sh: FIX set to true, fixing headers..." echo "license-header.sh: FIX set to true, fixing headers..."
@ -156,32 +156,28 @@ if [ "$FIX" = "true" ]; then
;; ;;
esac esac
LINE1=$(echo "$HEADER_LINE1_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
LINE2=$(echo "$HEADER_LINE2_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
TMP="$TMP_DIR/$BASENAME.tmp" TMP="$TMP_DIR/$BASENAME.tmp"
UPDATED=0 UPDATED=0
cp -p "$file" "$TMP"
> "$TMP"
cp -p $file $TMP # this logic is bit hacky but sed don't work well with $VARIABLES
echo "" > $TMP # it's this or complete remove this logic and keep only the old way
if [ "$UPDATE" = "true" ]; then
while IFS= read -r line || [ -n "$line" ]; do while IFS= read -r line || [ -n "$line" ]; do
if [ "$UPDATED" -eq 0 ] && echo "$line" | grep "$COPYRIGHT_OWNER" >/dev/null 2>&1; then if [ "$UPDATED" -eq 0 ] && echo "$line" | grep "$COPYRIGHT_OWNER" >/dev/null 2>&1; then
{ echo_header "$COMMENT_TYPE" >> "$TMP"
echo "$LINE1"
echo "$LINE2"
} >> "$TMP"
IFS= read -r _ || true IFS= read -r _ || true
UPDATED=1 UPDATED=1
else else
echo "$line" >> "$TMP" echo "$line" >> "$TMP"
fi fi
done < "$file" done < "$file"
fi
if [ "$UPDATED" -eq 0 ]; then if [ "$UPDATED" -eq 0 ]; then
{ {
echo "$LINE1" echo_header "$COMMENT_TYPE"
echo "$LINE2"
echo echo
cat "$TMP" cat "$TMP"
} > "$file" } > "$file"