license-header.sh: This is complete refactor

* It now fix in-place header with the same owner, see next commit

Signed-off-by: Caio Oliveira <caiooliveirafarias0@gmail.com>
This commit is contained in:
Caio Oliveira 2025-09-09 20:26:47 -03:00 committed by crueter
parent 09a84c4f65
commit 73a26b2353
3 changed files with 122 additions and 90 deletions

View file

@ -1,13 +1,17 @@
#!/bin/sh -e #!/bin/sh -e
# SPDX-FileCopyrightText: 2025 Eden Emulator Project # SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
COPYRIGHT_YEAR="2025"
COPYRIGHT_OWNER="Eden Emulator Project"
COPYRIGHT_LICENSE="GPL-3.0-or-later"
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
echo echo
echo "license-header.sh: Eden License Headers Accreditation Script" echo "license-header.sh: Eden License Headers Accreditation Script"
echo echo
echo "This script checks and optionally fixes license headers in source and CMake 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."
@ -25,11 +29,11 @@ if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
exit 0 exit 0
fi fi
HEADER="$(cat "$PWD/.ci/license/header.txt")" HEADER_LINE1_TEMPLATE="{COMMENT_TEMPLATE} SPDX-FileCopyrightText: Copyright $COPYRIGHT_YEAR $COPYRIGHT_OWNER"
HEADER_HASH="$(cat "$PWD/.ci/license/header-hash.txt")" HEADER_LINE2_TEMPLATE="{COMMENT_TEMPLATE} SPDX-License-Identifier: $COPYRIGHT_LICENSE"
echo SRC_FILES=""
echo "license-header.sh: Getting branch changes" OTHER_FILES=""
BASE=$(git merge-base master HEAD) BASE=$(git merge-base master HEAD)
if git diff --quiet "$BASE"..HEAD; then if git diff --quiet "$BASE"..HEAD; then
@ -40,119 +44,151 @@ fi
FILES=$(git diff --name-only "$BASE") FILES=$(git diff --name-only "$BASE")
check_header() { check_header() {
CONTENT=$(head -n3 < "$1") COMMENT_TYPE="$1"
case "$CONTENT" in FILE="$2"
"$HEADER"*) ;;
*) BAD_FILES="$BAD_FILES $1" ;;
esac
}
check_cmake_header() { HEADER_LINE1=$(printf '%s\n' "$HEADER_LINE1_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
CONTENT=$(head -n3 < "$1") HEADER_LINE2=$(printf '%s\n' "$HEADER_LINE2_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
case "$CONTENT" in
"$HEADER_HASH"*) ;; FOUND=0
*) BAD_CMAKE="$BAD_CMAKE $1" ;; while IFS= read -r line || [ -n "$line" ]; do
esac 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
[ -f "$file" ] || continue [ -f "$file" ] || continue
if [ "$(basename "$file")" = "CMakeLists.txt" ]; then case "$(basename "$file")" in
check_cmake_header "$file" CMakeLists.txt)
continue COMMENT_TYPE="#" ;;
fi *)
EXT="${file##*.}"
EXTENSION="${file##*.}" case "$EXT" in
case "$EXTENSION" in kts|kt|cpp|h) COMMENT_TYPE="//" ;;
kts|kt|cpp|h) cmake|sh) COMMENT_TYPE="#" ;;
check_header "$file" *) continue ;;
;; esac ;;
cmake)
check_cmake_header "$file"
;;
esac esac
check_header "$COMMENT_TYPE" "$file"
done done
if [ -z "$BAD_FILES" ] && [ -z "$BAD_CMAKE" ]; then if [ -z "$SRC_FILES" ] && [ -z "$OTHER_FILES" ]; then
echo echo
echo "license-header.sh: All good!" echo "license-header.sh: All good!"
exit 0 exit 0
fi fi
if [ -n "$BAD_FILES" ]; then for TYPE in "SRC" "OTHER"; do
echo if [ "$TYPE" = "SRC" ] && [ -n "$SRC_FILES" ]; then
echo "license-header.sh: The following source files have incorrect license headers:" FILES_LIST="$SRC_FILES"
COMMENT_TYPE="//"
DESC="Source"
elif [ "$TYPE" = "OTHER" ] && [ -n "$OTHER_FILES" ]; then
FILES_LIST="$OTHER_FILES"
COMMENT_TYPE="#"
DESC="CMake and shell script"
else
continue
fi
echo echo
for file in $BAD_FILES; do echo "------------------------------------------------------------"
echo " - $file" echo "$DESC files"
echo "------------------------------------------------------------"
echo
echo " The following files contain incorrect license headers:"
for file in $FILES_LIST; do
echo " - $file"
done done
cat << EOF
The following license header should be added to the start of all offending SOURCE files:
=== BEGIN ===
$HEADER
=== END ===
EOF
fi
if [ -n "$BAD_CMAKE" ]; then
echo echo
echo "license-header.sh: The following CMake files have incorrect license headers:" echo " The correct license header to be added to all affected"
echo " '$DESC' files is:"
echo echo
for file in $BAD_CMAKE; do echo "=== BEGIN ==="
echo " - $file" printf '%s\n%s\n' \
done "$(printf '%s\n' "$HEADER_LINE1_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")" \
"$(printf '%s\n' "$HEADER_LINE2_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")"
cat << EOF echo "=== END ==="
done
The following license header should be added to the start of all offending CMake files:
=== BEGIN ===
$HEADER_HASH
=== END ===
EOF
fi
cat << EOF cat << EOF
If some of the code in this PR is not being contributed by the original author,
the files which have been exclusively changed by that code can be ignored. ------------------------------------------------------------
If this happens, this PR requirement can be bypassed once all other files are addressed.
If some of the code in this pull request was not contributed by the original
author, the files that have been modified exclusively by that code can be
safely ignored. In such cases, this PR requirement may be bypassed once all
other files have been reviewed and addressed.
EOF EOF
TMP_DIR=$(mktemp -d /tmp/license-header.XXXXXX) || exit 1
if [ "$FIX" = "true" ]; then if [ "$FIX" = "true" ]; then
echo echo
echo "license-header.sh: FIX set to true, fixing headers..." echo "license-header.sh: FIX set to true, fixing headers..."
for file in $BAD_FILES; do for file in $SRC_FILES $OTHER_FILES; do
cp "$file" "$file.bak" BASENAME=$(basename "$file")
cat .ci/license/header.txt > "$file" case "$BASENAME" in
echo >> "$file" CMakeLists.txt) COMMENT_TYPE="#" ;;
cat "$file.bak" >> "$file" *)
EXT="${file##*.}"
case "$EXT" in
kts|kt|cpp|h) COMMENT_TYPE="//" ;;
cmake|sh) COMMENT_TYPE="#" ;;
*) continue ;;
esac
;;
esac
LINE1=$(printf '%s\n' "$HEADER_LINE1_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
LINE2=$(printf '%s\n' "$HEADER_LINE2_TEMPLATE" | sed "s|{COMMENT_TEMPLATE}|$COMMENT_TYPE|g")
TMP="$TMP_DIR/$BASENAME.tmp"
UPDATED=0
cp -p $file $TMP
printf '' > $TMP
while IFS= read -r line || [ -n "$line" ]; do
if [ "$UPDATED" -eq 0 ] && echo "$line" | grep "$COPYRIGHT_OWNER" >/dev/null 2>&1; then
printf '%s\n%s\n' "$LINE1" "$LINE2" >> "$TMP"
IFS= read -r _ || true
UPDATED=1
else
printf '%s\n' "$line" >> "$TMP"
fi
done < "$file"
if [ "$UPDATED" -eq 0 ]; then
{
printf '%s\n%s\n\n' "$LINE1" "$LINE2"
cat "$TMP"
} > "$file"
else
mv "$TMP" "$file"
fi
rm "$file.bak"
git add "$file" git add "$file"
done done
for file in $BAD_CMAKE; do rm -rf "$TMP_DIR"
cp "$file" "$file.bak"
cat .ci/license/header-hash.txt > "$file"
echo >> "$file"
cat "$file.bak" >> "$file"
rm "$file.bak"
git add "$file"
done
echo echo
echo "license-header.sh: License headers fixed!" echo "license-header.sh: License headers fixed!"

View file

@ -1,2 +0,0 @@
# SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
# SPDX-License-Identifier: GPL-3.0-or-later

View file

@ -1,2 +0,0 @@
// SPDX-FileCopyrightText: Copyright 2025 Eden Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later