2025-06-27 23:23:25 +00:00
|
|
|
#!/bin/sh -e
|
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
# SPDX-FileCopyrightText: 2025 Eden Emulator Project
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
|
|
|
echo
|
|
|
|
echo "license-header.sh: Eden License Headers Accreditation Script"
|
|
|
|
echo
|
|
|
|
echo "This script checks and optionally fixes license headers in source and CMake files."
|
|
|
|
echo
|
|
|
|
echo "Environment Variables:"
|
|
|
|
echo " FIX=true Automatically add the correct license headers to offending files."
|
|
|
|
echo " COMMIT=true If FIX=true, commit the changes automatically."
|
|
|
|
echo
|
|
|
|
echo "Usage Examples:"
|
|
|
|
echo " # Just check headers (will fail if headers are missing)"
|
|
|
|
echo " .ci/license-header.sh"
|
|
|
|
echo
|
|
|
|
echo " # Fix headers only"
|
|
|
|
echo " FIX=true .ci/license-header.sh"
|
|
|
|
echo
|
|
|
|
echo " # Fix headers and commit changes"
|
|
|
|
echo " FIX=true COMMIT=true .ci/license-header.sh"
|
|
|
|
exit 0
|
|
|
|
fi
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
HEADER="$(cat "$PWD/.ci/license/header.txt")"
|
|
|
|
HEADER_HASH="$(cat "$PWD/.ci/license/header-hash.txt")"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
echo
|
|
|
|
echo "license-header.sh: Getting branch changes"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
BRANCH=$(git rev-parse --abbrev-ref HEAD)
|
|
|
|
COMMITS=$(git log "${BRANCH}" --not master --pretty=format:"%h")
|
|
|
|
if [ -z "$COMMITS" ]; then
|
|
|
|
echo
|
|
|
|
echo "license-header.sh: No commits on this branch different from master."
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
RANGE="$(echo "$COMMITS" | tail -n1)^..$(echo "$COMMITS" | head -n1)"
|
|
|
|
FILES=$(git diff-tree --no-commit-id --name-only "${RANGE}" -r)
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 19:21:38 +02:00
|
|
|
check_header() {
|
2025-09-08 23:24:36 -03:00
|
|
|
CONTENT=$(head -n3 < "$1")
|
2025-09-08 19:21:38 +02:00
|
|
|
case "$CONTENT" in
|
|
|
|
"$HEADER"*) ;;
|
|
|
|
*) BAD_FILES="$BAD_FILES $1" ;;
|
|
|
|
esac
|
|
|
|
}
|
|
|
|
|
|
|
|
check_cmake_header() {
|
2025-09-08 23:24:36 -03:00
|
|
|
CONTENT=$(head -n3 < "$1")
|
2025-09-08 19:21:38 +02:00
|
|
|
case "$CONTENT" in
|
|
|
|
"$HEADER_HASH"*) ;;
|
2025-09-08 23:24:36 -03:00
|
|
|
*) BAD_CMAKE="$BAD_CMAKE $1" ;;
|
2025-09-08 19:21:38 +02:00
|
|
|
esac
|
|
|
|
}
|
2025-09-08 23:24:36 -03:00
|
|
|
|
2025-06-27 23:23:25 +00:00
|
|
|
for file in $FILES; do
|
2025-08-06 07:48:11 +02:00
|
|
|
[ -f "$file" ] || continue
|
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
if [ "$(basename -- "$file")" = "CMakeLists.txt" ]; then
|
2025-09-08 19:21:38 +02:00
|
|
|
check_cmake_header "$file"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
2025-06-27 23:23:25 +00:00
|
|
|
EXTENSION="${file##*.}"
|
|
|
|
case "$EXTENSION" in
|
|
|
|
kts|kt|cpp|h)
|
2025-09-08 19:21:38 +02:00
|
|
|
check_header "$file"
|
|
|
|
;;
|
|
|
|
cmake)
|
|
|
|
check_cmake_header "$file"
|
2025-06-27 23:23:25 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
if [ -z "$BAD_FILES" ] && [ -z "$BAD_CMAKE" ]; then
|
2025-06-27 23:23:25 +00:00
|
|
|
echo
|
2025-09-08 23:24:36 -03:00
|
|
|
echo "license-header.sh: All good!"
|
|
|
|
exit 0
|
2025-06-27 23:23:25 +00:00
|
|
|
fi
|
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
if [ -n "$BAD_FILES" ]; then
|
2025-09-08 19:21:38 +02:00
|
|
|
echo
|
2025-09-08 23:24:36 -03:00
|
|
|
echo "license-header.sh: The following source files have incorrect license headers:"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
echo
|
|
|
|
for file in $BAD_FILES; do
|
|
|
|
echo " - $file"
|
|
|
|
done
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 19:21:38 +02:00
|
|
|
cat << EOF
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 19:21:38 +02:00
|
|
|
The following license header should be added to the start of all offending SOURCE files:
|
2025-06-27 23:23:25 +00:00
|
|
|
|
|
|
|
=== BEGIN ===
|
|
|
|
$HEADER
|
|
|
|
=== END ===
|
|
|
|
|
2025-09-08 19:21:38 +02:00
|
|
|
EOF
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
if [ -n "$BAD_CMAKE" ]; then
|
2025-09-08 19:21:38 +02:00
|
|
|
echo
|
2025-09-08 23:24:36 -03:00
|
|
|
echo "license-header.sh: The following CMake files have incorrect license headers:"
|
2025-09-08 19:21:38 +02:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
echo
|
|
|
|
for file in $BAD_CMAKE; do
|
|
|
|
echo " - $file"
|
|
|
|
done
|
2025-09-08 19:21:38 +02:00
|
|
|
|
|
|
|
cat << EOF
|
|
|
|
|
|
|
|
The following license header should be added to the start of all offending CMake files:
|
|
|
|
|
|
|
|
=== BEGIN ===
|
|
|
|
$HEADER_HASH
|
|
|
|
=== END ===
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
cat << EOF
|
2025-06-27 23:23:25 +00:00
|
|
|
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.
|
|
|
|
EOF
|
|
|
|
|
|
|
|
if [ "$FIX" = "true" ]; then
|
|
|
|
echo
|
2025-09-08 23:24:36 -03:00
|
|
|
echo "license-header.sh: FIX set to true, fixing headers..."
|
2025-06-27 23:23:25 +00:00
|
|
|
|
|
|
|
for file in $BAD_FILES; do
|
2025-09-08 23:24:36 -03:00
|
|
|
cp -- "$file" "$file.bak"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
cat .ci/license/header.txt > "$file"
|
|
|
|
echo >> "$file"
|
|
|
|
cat "$file.bak" >> "$file"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
rm -- "$file.bak"
|
|
|
|
git add "$file"
|
2025-06-27 23:23:25 +00:00
|
|
|
done
|
|
|
|
|
2025-09-08 19:21:38 +02:00
|
|
|
for file in $BAD_CMAKE; do
|
2025-09-08 23:24:36 -03:00
|
|
|
cp -- "$file" "$file.bak"
|
2025-09-08 19:21:38 +02:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
cat .ci/license/header-hash.txt > "$file"
|
|
|
|
echo >> "$file"
|
|
|
|
cat "$file.bak" >> "$file"
|
2025-09-08 19:21:38 +02:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
rm -- "$file.bak"
|
|
|
|
git add "$file"
|
2025-09-08 19:21:38 +02:00
|
|
|
done
|
2025-09-08 23:24:36 -03:00
|
|
|
|
|
|
|
echo
|
|
|
|
echo "license-header.sh: License headers fixed!"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-06-27 19:26:11 -04:00
|
|
|
if [ "$COMMIT" = "true" ]; then
|
|
|
|
echo
|
2025-09-08 23:24:36 -03:00
|
|
|
echo "license-header.sh: COMMIT set to true, committing changes..."
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
git commit -m "[license] Fix license headers"
|
2025-06-27 23:23:25 +00:00
|
|
|
|
2025-09-08 23:24:36 -03:00
|
|
|
echo
|
|
|
|
echo "license-header.sh: Changes committed. You may now push."
|
2025-06-27 19:26:11 -04:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
exit 1
|
2025-06-27 23:23:25 +00:00
|
|
|
fi
|